Skip to content

[pull] master from TheAlgorithms:master #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
33750ec
fix: cover the cases `n == 0` and `m < n` in `N_bonacci` (#2468)
vil02 May 31, 2023
ea4100e
chore: add codeowners (#2474)
Panquesito7 May 31, 2023
c876e50
feat: add Kelvin to Celsius conversion algorithm (#2475)
Panquesito7 Jun 8, 2023
26f6e98
chore: add `cpu_scheduling_algorithms` to CMake (#2477)
Panquesito7 Jun 9, 2023
b0b183d
feat: add memory game and a games folder (#2471)
Panquesito7 Jun 12, 2023
4aa529c
chore: remove Taj from codeowners
Panquesito7 Jun 14, 2023
0953376
feat: use directory workflow from the... (#2485)
Panquesito7 Jun 16, 2023
1a6ca12
feat: add `physics` to CMake lists (#2481)
Panquesito7 Jun 16, 2023
1bfd46e
docs: update workflow versions in `README.md` (#2483)
Panquesito7 Jun 16, 2023
ff80be4
feat: Add SHA-256 hashing algorithm (#2470)
Jun 16, 2023
5704841
[feat/docs/fix]: fit `check_factorial.cpp` to guidelines (#2466)
ewd00010 Jun 16, 2023
5ef7ad5
[feat/docs/fix]: fit check_prime.cpp to guidelines (#2460)
ewd00010 Jun 16, 2023
d7a9869
feat: add Boyer Moore algorithm implementation (#2441)
stoychoX Jun 16, 2023
2d49283
feat: update CMake version to 3.26.4 (#2486)
heysujal Jun 16, 2023
5dc3467
Revert "feat: update CMake version" (#2489)
Panquesito7 Jun 20, 2023
2fd530c
[fix/docs]: fit `armstrong_number.cpp` to guidelines (#2457)
realstealthninja Jun 20, 2023
e203bfe
fix: use correct branch name
Panquesito7 Jun 23, 2023
d1ec37c
feat: add Next Higher Number with same set bits (#2484)
Kunal766 Jun 23, 2023
72cd2d0
fix: directory writer `on` event
Panquesito7 Jun 23, 2023
e3f0551
[fix/docs]: fit `check_amicable_pair.cpp` to guidelines (#2465)
ewd00010 Jun 23, 2023
32db99c
[fix/docs]: cleanup `longest_common_string.cpp` (#2462)
vil02 Jul 4, 2023
f4d1aa1
chore: add the linter to a separate Python script (#2500)
Panquesito7 Jul 5, 2023
baaece2
chore: add endline on PR template
Panquesito7 Jul 6, 2023
813175a
feat: upgrade FreeGlut to v3.4.0 (#2478)
Panquesito7 Jul 10, 2023
37aae7c
docs: fit `factorial.cpp` to contribution guidelines (#2487)
realstealthninja Jul 19, 2023
7003ba8
feat: improve the Armstrong Number algorithm (#2480)
Panquesito7 Jul 19, 2023
aaf84ab
fix: fit `approximate_pi.cpp` to guidelines (#2499)
ewd00010 Jul 19, 2023
25b9e0a
feat: update cmake to use a newer version (#2505)
rbevin777 Jul 20, 2023
17a5326
feat: add Iterative Factorial (#2453)
Renjian-buchai Jul 21, 2023
170f7e5
feat: Treap Data Structure (#2458)
fgmn Jul 21, 2023
a39892b
fix: wrong contributing guidelines link (#2507)
hexavik Jul 21, 2023
882ba11
[feat/docs]: improve Fenwick Tree algorithm (#2506)
Panquesito7 Jul 21, 2023
b480ddb
docs: add documentation in `kruskals_minimum_spanning_tree.cpp` (#2482)
Panquesito7 Jul 21, 2023
17c374d
[feat/docs/fix]: improve the Dijkstra algorithm (#2508)
Panquesito7 Jul 25, 2023
2c68b68
fix: add missing namespace in Dijkstra
Panquesito7 Jul 25, 2023
6daf701
chore: run directory workflow daily
Panquesito7 Aug 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @Panquesito7 @realstealthninja
4 changes: 2 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Thank you for your Pull Request. Please provide a description above and review
the requirements below.

Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/CONTRIBUTING.md
Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTING.md
-->

#### Checklist
Expand All @@ -18,4 +18,4 @@ Contributors guide: https://github.com/TheAlgorithms/C-Plus-Plus/CONTRIBUTING.md
- [ ] Search previous suggestions before making a new one, as yours may be a duplicate.
- [ ] I acknowledge that all my contributions will be made under the project's license.

Notes: <!-- Please add a one-line description for developers or pull request viewers -->
Notes: <!-- Please add a one-line description for developers or pull request viewers -->
48 changes: 2 additions & 46 deletions .github/workflows/awesome_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ jobs:
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/filename_formatter.sh
chmod +x filename_formatter.sh
./filename_formatter.sh . .cpp,.hpp
- name: Update DIRECTORY.md
run: |
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/build_directory_md.py
python3 build_directory_md.py C-Plus-Plus . .cpp,.hpp,.h > DIRECTORY.md
git commit -m "updating DIRECTORY.md" DIRECTORY.md || true
- name: Get file changes
run: |
git branch
Expand All @@ -43,47 +38,8 @@ jobs:
# be able to catch any errors for other platforms.
run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
- name: Lint modified files
shell: python
run: |
import os
import subprocess
import sys

print("Python {}.{}.{}".format(*sys.version_info)) # Python 3.8
with open("git_diff.txt") as in_file:
modified_files = sorted(in_file.read().splitlines())
print("{} files were modified.".format(len(modified_files)))

cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
print(f"{len(cpp_files)} C++ files were modified.")
if not cpp_files:
sys.exit(0)

subprocess.run(["clang-tidy", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"],
check=True, text=True, stderr=subprocess.STDOUT)

subprocess.run(["clang-format", "-i", "-style=file", *cpp_files],
check=True, text=True, stderr=subprocess.STDOUT)

upper_files = [file for file in cpp_files if file != file.lower()]
if upper_files:
print(f"{len(upper_files)} files contain uppercase characters:")
print("\n".join(upper_files) + "\n")

space_files = [file for file in cpp_files if " " in file or "-" in file]
if space_files:
print(f"{len(space_files)} files contain space or dash characters:")
print("\n".join(space_files) + "\n")

nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
if nodir_files:
print(f"{len(nodir_files)} files are not in one and only one directory:")
print("\n".join(nodir_files) + "\n")

bad_files = len(upper_files + space_files + nodir_files)
if bad_files:
sys.exit(bad_files)
shell: bash
run: python3 scripts/file_linter.py
- name: Commit and push changes
run: |
git diff DIRECTORY.md
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/directory_writer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Directory writer
on:
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
- cron: '0 0 * * *'
jobs:
build:
if: github.repository == 'TheAlgorithms/C-Plus-Plus' # We only need this to run in our repository.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Build directory
uses: TheAlgorithms/scripts/directory_md@main
with:
language: C-Plus-Plus
working-directory: .
filetypes: .cpp,.hpp,.h
ignored-directories: doc/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ a.out
*.app

build/
git_diff.txt
29 changes: 28 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"valarray": "cpp"
"valarray": "cpp",
"bit": "cpp",
"charconv": "cpp",
"compare": "cpp",
"concepts": "cpp",
"format": "cpp",
"forward_list": "cpp",
"ios": "cpp",
"locale": "cpp",
"queue": "cpp",
"stack": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"climits": "cpp"
}
}
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.9)
cmake_minimum_required(VERSION 3.22)
project(Algorithms_in_C++
LANGUAGES CXX
VERSION 1.0.0
Expand Down Expand Up @@ -43,6 +43,9 @@ add_subdirectory(machine_learning)
add_subdirectory(numerical_methods)
add_subdirectory(graph)
add_subdirectory(divide_and_conquer)
add_subdirectory(games)
add_subdirectory(cpu_scheduling_algorithms)
add_subdirectory(physics)

cmake_policy(SET CMP0054 NEW)
cmake_policy(SET CMP0057 NEW)
Expand Down
6 changes: 6 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
* [next higher number with same number of set bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp)
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
* [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp)
* [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp)
Expand Down Expand Up @@ -120,6 +121,9 @@
* [Tree Height](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/tree_height.cpp)
* [Word Break](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/word_break.cpp)

## Games
* [Memory Game](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/games/memory_game.cpp)

## Geometry
* [Graham Scan Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/geometry/graham_scan_algorithm.cpp)
* [Graham Scan Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/geometry/graham_scan_functions.hpp)
Expand Down Expand Up @@ -168,6 +172,7 @@
* [Md5](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/md5.cpp)
* [Quadratic Probing Hash Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/quadratic_probing_hash_table.cpp)
* [Sha1](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/sha1.cpp)
* [Sha256](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/sha256.cpp)

## Machine Learning
* [A Star Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/machine_learning/a_star_search.cpp)
Expand Down Expand Up @@ -289,6 +294,7 @@
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/happy_number.cpp)
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This repository is a collection of open-source implementation of a variety of al
* The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - [C++](https://en.wikipedia.org/wiki/C%2B%2B).
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS and Ubuntu (Linux) using MSVC 16 2019, AppleClang 11.0 and GNU 7.5.0 respectively.
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS, and Ubuntu (Linux) using MSVC 19 2022, AppleClang 14.0.0, and GNU 11.3.0 respectively.
* Strict adherence to [C++11](https://en.wikipedia.org/wiki/C%2B%2B11) standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
* Self-checks within programs ensure correct implementations with confidence.
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
Expand Down
100 changes: 100 additions & 0 deletions bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @file
* @brief [Next higher number with same number of set bits]
* (https://www.geeksforgeeks.org/next-higher-number-with-same-number-of-set-bits/)
* implementation
*
* @details
* Given a number x, find next number with same number of 1 bits in it’s binary
* representation. For example, consider x = 12, whose binary representation is
* 1100 (excluding leading zeros on 32 bit machine). It contains two logic 1
* bits. The next higher number with two logic 1 bits is 17 (100012).
*
* A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
* set bit in computer terms.
* @author [Kunal Nayak](https://github.com/Kunal766)
*/

#include <cassert> /// for assert
#include <iostream> /// for IO operations

/**
* @namespace bit_manipulation
* @brief Bit manipulation algorithms
*/
namespace bit_manipulation {

/**
* @brief The main function implements checking the next number
* @param x the number that will be calculated
* @returns a number
*/
uint64_t next_higher_number(uint64_t x) {
uint64_t rightOne = 0;
uint64_t nextHigherOneBit = 0;
uint64_t rightOnesPattern = 0;

uint64_t next = 0;

if (x) {
// right most set bit
rightOne = x & -static_cast<signed>(x);

// reset the pattern and set next higher bit
// left part of x will be here
nextHigherOneBit = x + rightOne;

// nextHigherOneBit is now part [D] of the above explanation.

// isolate the pattern
rightOnesPattern = x ^ nextHigherOneBit;

// right adjust pattern
rightOnesPattern = (rightOnesPattern) / rightOne;

// correction factor
rightOnesPattern >>= 2;

// rightOnesPattern is now part [A] of the above explanation.

// integrate new pattern (Add [D] and [A])
next = nextHigherOneBit | rightOnesPattern;
}

return next;
}

} // namespace bit_manipulation

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// x = 4 return 8
assert(bit_manipulation::next_higher_number(4) == 8);
// x = 6 return 9
assert(bit_manipulation::next_higher_number(6) == 9);
// x = 13 return 14
assert(bit_manipulation::next_higher_number(13) == 14);
// x = 64 return 128
assert(bit_manipulation::next_higher_number(64) == 128);
// x = 15 return 23
assert(bit_manipulation::next_higher_number(15) == 23);
// x= 32 return 64
assert(bit_manipulation::next_higher_number(32) == 64);
// x = 97 return 98
assert(bit_manipulation::next_higher_number(97) == 98);
// x = 1024 return 2048
assert(bit_manipulation::next_higher_number(1024) == 2048);

std::cout << "All test cases have successfully passed!" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}
16 changes: 16 additions & 0 deletions cpu_scheduling_algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. The RELATIVE flag makes it easier to extract an executable's name
# automatically.

file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
foreach( testsourcefile ${APP_SOURCES} )
string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp`
add_executable( ${testname} ${testsourcefile} )

set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
if(OpenMP_CXX_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
endif()
install(TARGETS ${testname} DESTINATION "bin/cpu_scheduling_algorithms") # Folder name. Do NOT include `<>`

endforeach( testsourcefile ${APP_SOURCES} )
Loading