Skip to content

Commit e862c87

Browse files
Merge branch 'master' into check_amicable_pair
2 parents bdd1cff + 2d49283 commit e862c87

File tree

18 files changed

+1409
-162
lines changed

18 files changed

+1409
-162
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @Panquesito7 @realstealthninja

.github/workflows/awesome_workflow.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ jobs:
2727
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/filename_formatter.sh
2828
chmod +x filename_formatter.sh
2929
./filename_formatter.sh . .cpp,.hpp
30-
- name: Update DIRECTORY.md
31-
run: |
32-
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/build_directory_md.py
33-
python3 build_directory_md.py C-Plus-Plus . .cpp,.hpp,.h > DIRECTORY.md
34-
git commit -m "updating DIRECTORY.md" DIRECTORY.md || true
3530
- name: Get file changes
3631
run: |
3732
git branch
@@ -94,12 +89,26 @@ jobs:
9489
name: Compile checks
9590
runs-on: ${{ matrix.os }}
9691
needs: [MainSequence]
92+
permissions:
93+
pull-requests: write
9794
strategy:
9895
matrix:
9996
os: [ubuntu-latest, windows-latest, macOS-latest]
10097
steps:
10198
- uses: actions/checkout@v3
10299
with:
103100
submodules: true
104-
- run: cmake -B ./build -S .
105-
- run: cmake --build build
101+
- run: |
102+
cmake -B ./build -S .
103+
cmake --build build
104+
- name: Label on PR fail
105+
uses: actions/github-script@v6
106+
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
107+
with:
108+
script: |
109+
github.rest.issues.addLabels({
110+
issue_number: context.issue.number,
111+
owner: context.repo.owner,
112+
repo: context.repo.repo,
113+
labels: ['automated tests are failing']
114+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Directory writer
2+
on:
3+
push:
4+
branches:
5+
- main
6+
schedule:
7+
# ┌───────────── minute (0 - 59)
8+
# │ ┌───────────── hour (0 - 23)
9+
# │ │ ┌───────────── day of the month (1 - 31)
10+
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
11+
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
12+
# │ │ │ │ │
13+
# │ │ │ │ │
14+
# │ │ │ │ │
15+
# * * * * *
16+
- cron: '0 0 * * 1'
17+
jobs:
18+
build:
19+
if: github.repository == 'TheAlgorithms/C-Plus-Plus' # We only need this to run in our repository.
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v3
23+
with:
24+
fetch-depth: 0
25+
- name: Build directory
26+
uses: TheAlgorithms/scripts/directory_md@main
27+
with:
28+
language: C-Plus-Plus
29+
working-directory: .
30+
filetypes: .cpp,.hpp,.h
31+
ignored-directories: doc/

.vscode/settings.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@
5959
"stdexcept": "cpp",
6060
"streambuf": "cpp",
6161
"typeinfo": "cpp",
62-
"valarray": "cpp"
62+
"valarray": "cpp",
63+
"bit": "cpp",
64+
"charconv": "cpp",
65+
"compare": "cpp",
66+
"concepts": "cpp",
67+
"format": "cpp",
68+
"forward_list": "cpp",
69+
"ios": "cpp",
70+
"locale": "cpp",
71+
"queue": "cpp",
72+
"stack": "cpp",
73+
"xfacet": "cpp",
74+
"xhash": "cpp",
75+
"xiosbase": "cpp",
76+
"xlocale": "cpp",
77+
"xlocbuf": "cpp",
78+
"xlocinfo": "cpp",
79+
"xlocmes": "cpp",
80+
"xlocmon": "cpp",
81+
"xlocnum": "cpp",
82+
"xloctime": "cpp",
83+
"xmemory": "cpp",
84+
"xstddef": "cpp",
85+
"xstring": "cpp",
86+
"xtr1common": "cpp",
87+
"xtree": "cpp",
88+
"xutility": "cpp",
89+
"climits": "cpp"
6390
}
6491
}

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.9)
1+
cmake_minimum_required(VERSION 3.26.4)
22
project(Algorithms_in_C++
33
LANGUAGES CXX
44
VERSION 1.0.0
@@ -43,6 +43,9 @@ add_subdirectory(machine_learning)
4343
add_subdirectory(numerical_methods)
4444
add_subdirectory(graph)
4545
add_subdirectory(divide_and_conquer)
46+
add_subdirectory(games)
47+
add_subdirectory(cpu_scheduling_algorithms)
48+
add_subdirectory(physics)
4649

4750
cmake_policy(SET CMP0054 NEW)
4851
cmake_policy(SET CMP0057 NEW)

DIRECTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
* [Tree Height](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/tree_height.cpp)
121121
* [Word Break](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/word_break.cpp)
122122

123+
## Games
124+
* [Memory Game](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/games/memory_game.cpp)
125+
123126
## Geometry
124127
* [Graham Scan Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/geometry/graham_scan_algorithm.cpp)
125128
* [Graham Scan Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/geometry/graham_scan_functions.hpp)
@@ -168,6 +171,7 @@
168171
* [Md5](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/md5.cpp)
169172
* [Quadratic Probing Hash Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/quadratic_probing_hash_table.cpp)
170173
* [Sha1](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/sha1.cpp)
174+
* [Sha256](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/hashing/sha256.cpp)
171175

172176
## Machine Learning
173177
* [A Star Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/machine_learning/a_star_search.cpp)
@@ -289,6 +293,7 @@
289293
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/happy_number.cpp)
290294
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
291295
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
296+
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
292297
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
293298
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
294299
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This repository is a collection of open-source implementation of a variety of al
2121
* 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).
2222
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
2323
* 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.
24-
* 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.
24+
* 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.
2525
* 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.
2626
* Self-checks within programs ensure correct implementations with confidence.
2727
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. The RELATIVE flag makes it easier to extract an executable's name
3+
# automatically.
4+
5+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
6+
foreach( testsourcefile ${APP_SOURCES} )
7+
string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp`
8+
add_executable( ${testname} ${testsourcefile} )
9+
10+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
11+
if(OpenMP_CXX_FOUND)
12+
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
13+
endif()
14+
install(TARGETS ${testname} DESTINATION "bin/cpu_scheduling_algorithms") # Folder name. Do NOT include `<>`
15+
16+
endforeach( testsourcefile ${APP_SOURCES} )

dynamic_programming/longest_palindromic_subsequence.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file
3-
* @brief Program to find the Longest Palindormic
4-
* Subsequence of a string
3+
* @brief Program to find the [Longest Palindormic
4+
* Subsequence](https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/) of a string
55
*
66
* @details
77
* [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of
@@ -13,26 +13,33 @@
1313
* @author [Anjali Jha](https://github.com/anjali1903)
1414
*/
1515

16-
#include <algorithm>
17-
#include <cassert>
18-
#include <iostream>
19-
#include <vector>
16+
#include <cassert> /// for assert
17+
#include <string> /// for std::string
18+
#include <vector> /// for std::vector
2019

2120
/**
22-
* Function that returns the longest palindromic
21+
* @namespace
22+
* @brief Dynamic Programming algorithms
23+
*/
24+
namespace dynamic_programming {
25+
/**
26+
* @brief Function that returns the longest palindromic
2327
* subsequence of a string
28+
* @param a string whose longest palindromic subsequence is to be found
29+
* @returns longest palindromic subsequence of the string
2430
*/
25-
std::string lps(std::string a) {
26-
std::string b = a;
27-
reverse(b.begin(), b.end());
28-
int m = a.length();
29-
std::vector<std::vector<int> > res(m + 1);
31+
std::string lps(const std::string& a) {
32+
const auto b = std::string(a.rbegin(), a.rend());
33+
const auto m = a.length();
34+
using ind_type = std::string::size_type;
35+
std::vector<std::vector<ind_type> > res(m + 1,
36+
std::vector<ind_type>(m + 1));
3037

3138
// Finding the length of the longest
3239
// palindromic subsequence and storing
3340
// in a 2D array in bottoms-up manner
34-
for (int i = 0; i <= m; i++) {
35-
for (int j = 0; j <= m; j++) {
41+
for (ind_type i = 0; i <= m; i++) {
42+
for (ind_type j = 0; j <= m; j++) {
3643
if (i == 0 || j == 0) {
3744
res[i][j] = 0;
3845
} else if (a[i - 1] == b[j - 1]) {
@@ -43,10 +50,10 @@ std::string lps(std::string a) {
4350
}
4451
}
4552
// Length of longest palindromic subsequence
46-
int idx = res[m][m];
53+
auto idx = res[m][m];
4754
// Creating string of index+1 length
48-
std::string ans(idx + 1, '\0');
49-
int i = m, j = m;
55+
std::string ans(idx, '\0');
56+
ind_type i = m, j = m;
5057

5158
// starting from right-most bottom-most corner
5259
// and storing them one by one in ans
@@ -70,19 +77,22 @@ std::string lps(std::string a) {
7077

7178
return ans;
7279
}
80+
} // namespace dynamic_programming
7381

74-
/** Test function */
75-
void test() {
76-
// lps("radar") return "radar"
77-
assert(lps("radar") == "radar");
78-
// lps("abbcbaa") return "abcba"
79-
assert(lps("abbcbaa") == "abcba");
80-
// lps("bbbab") return "bbbb"
81-
assert(lps("bbbab") == "bbbb");
82+
/**
83+
* @brief Self-test implementations
84+
* @returns void
85+
*/
86+
static void test() {
87+
assert(dynamic_programming::lps("radar") == "radar");
88+
assert(dynamic_programming::lps("abbcbaa") == "abcba");
89+
assert(dynamic_programming::lps("bbbab") == "bbbb");
90+
assert(dynamic_programming::lps("") == "");
8291
}
8392

8493
/**
85-
* Main Function
94+
* @brief Main Function
95+
* @returns 0 on exit
8696
*/
8797
int main() {
8898
test(); // execute the tests

games/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. The RELATIVE flag makes it easier to extract an executable's name
3+
# automatically.
4+
5+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
6+
foreach( testsourcefile ${APP_SOURCES} )
7+
string( REPLACE ".cpp" "" testname ${testsourcefile} ) # File type. Example: `.cpp`
8+
add_executable( ${testname} ${testsourcefile} )
9+
10+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
11+
if(OpenMP_CXX_FOUND)
12+
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
13+
endif()
14+
install(TARGETS ${testname} DESTINATION "bin/games") # Folder name. Do NOT include `<>`
15+
16+
endforeach( testsourcefile ${APP_SOURCES} )

0 commit comments

Comments
 (0)