Skip to content

Commit 62a4923

Browse files
Merge branch 'master' into patch-1
2 parents d9241ce + 93a700c commit 62a4923

File tree

9 files changed

+207
-52
lines changed

9 files changed

+207
-52
lines changed

.github/workflows/approved-label.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ jobs:
55
name: Add "approved" label when approved
66
runs-on: ubuntu-latest
77
steps:
8-
- name: Add "approved" label when approved
9-
uses: abinoda/label-when-approved-action@v1.0.7
10-
env:
11-
APPROVALS: "1"
12-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13-
ADD_LABEL: "approved"
14-
REMOVE_LABEL: ""
8+
- name: Add "approved" label when approved
9+
uses: pullreminders/label-when-approved-action@master
10+
env:
11+
APPROVALS: "1"
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
ADD_LABEL: "approved"
14+
REMOVE_LABEL: ""

.github/workflows/awesome_workflow.yml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
name: Awesome CI Workflow
22
on: [push, pull_request]
33
permissions:
4-
pull-requests: write
54
contents: write
6-
issues: write
75

86
jobs:
97
MainSequence:
108
name: Code Formatter
119
runs-on: ubuntu-latest
1210
steps:
1311
- uses: actions/checkout@v4
14-
- uses: cpp-linter/cpp-linter-action@v2
15-
id: linter
16-
env:
17-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1812
with:
19-
style: "file"
20-
tidy-checks: ".clang-tidy"
21-
thread-comments: ${{ github.event_name == 'pull_request' && 'update' }}
13+
fetch-depth: 0
14+
- uses: actions/setup-python@v4
15+
- name: requirements
16+
run: |
17+
sudo apt-get -qq update
18+
sudo apt-get -qq install clang-tidy clang-format
19+
# checks are passing with less errors when used with this version.
20+
# The default installs v6.0 which did not work out well in my tests
21+
- name: Setup Git Specs
22+
run: |
23+
git config --global user.name github-actions[bot]
24+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
25+
- name: Filename Formatter
26+
run: |
27+
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/filename_formatter.sh
28+
chmod +x filename_formatter.sh
29+
./filename_formatter.sh . .cpp,.hpp
30+
- name: Get file changes
31+
run: |
32+
git branch
33+
git diff --diff-filter=dr --name-only origin/master > git_diff.txt
34+
echo "Files changed-- `cat git_diff.txt`"
35+
- name: Configure for static lint checks
36+
# compiling first gives clang-tidy access to all the header files and settings used to compile the programs.
37+
# This will check for macros, if any, on linux and not for Windows. But the use of portability checks should
38+
# be able to catch any errors for other platforms.
39+
run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
40+
- name: Lint modified files
41+
shell: bash
42+
run: python3 scripts/file_linter.py
43+
- name: Commit and push changes
44+
run: |
45+
git diff DIRECTORY.md
46+
git commit -am "clang-format and clang-tidy fixes for ${GITHUB_SHA::8}" || true
47+
git push origin HEAD:$GITHUB_REF || true
2248
2349
build:
2450
name: Compile checks
@@ -33,8 +59,6 @@ jobs:
3359
- uses: actions/checkout@v4
3460
with:
3561
submodules: true
36-
- name: GCC problem matcher
37-
uses: ammaraskar/[email protected]
3862
- run: |
3963
cmake -B ./build -S .
4064
cmake --build build --parallel 4

backtracking/graph_coloring.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <array> /// for std::array
2222
#include <iostream> /// for IO operations
23-
#include <vector> /// for std::vector
2423

2524
/**
2625
* @namespace backtracking

data_structures/trie_tree.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <iostream>
1313
#include <memory>
1414
#include <string>
15-
#include <vector>
1615

1716
/** \namespace data_structures
1817
* \brief Data-structure algorithms

divide_and_conquer/karatsuba_algorithm_for_fast_multiplication.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <cassert> /// for assert
1616
#include <cstring> /// for string
1717
#include <iostream> /// for IO operations
18-
#include <vector> /// for std::vector
1918

2019
/**
2120
* @namespace divide_and_conquer

graph/breadth_first_search.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include <map>
5353
#include <queue>
5454
#include <string>
55-
#include <vector>
5655

5756
/**
5857
* \namespace graph

math/fibonacci_fast.cpp

Lines changed: 149 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,178 @@
11
/**
22
* @file
3-
* @brief Faster computation of Fibonacci series
3+
* @brief Faster computation of Fibonacci series.
44
*
5+
* @details
56
* An efficient way to calculate nth fibonacci number faster and simpler than
6-
* \f$O(n\log n)\f$ method of matrix exponentiation This works by using both
7-
* recursion and dynamic programming. as 93rd fibonacci exceeds 19 digits, which
7+
* \f$O(n\log n)\f$ method of matrix exponentiation. This works by using both
8+
* recursion and dynamic programming. As 93rd fibonacci exceeds 19 digits, which
89
* cannot be stored in a single long long variable, we can only use it till 92nd
910
* fibonacci we can use it for 10000th fibonacci etc, if we implement
1011
* bigintegers. This algorithm works with the fact that nth fibonacci can easily
11-
* found if we have already found n/2th or (n+1)/2th fibonacci It is a property
12+
* found if we have already found \f$n/2\f$th or \f$(n+1)/2\f$th fibonacci. It is a property
1213
* of fibonacci similar to matrix exponentiation.
1314
*
14-
* \author [Krishna Vedala](https://github.com/kvedala)
15+
* @author [Krishna Vedala](https://github.com/kvedala)
1516
* @see fibonacci_large.cpp, fibonacci.cpp, string_fibonacci.cpp
1617
*/
17-
18-
#include <cinttypes>
19-
#include <cstdio>
20-
#include <iostream>
18+
#include <cinttypes> /// for uint64_t
19+
#include <cstdio> /// for standard IO
20+
#include <iostream> /// for IO operations
21+
#include <cassert> /// for assert
22+
#include <string> /// for std::to_string
23+
#include <stdexcept> /// for std::invalid_argument
2124

2225
/**
23-
* maximum number that can be computed - The result after 93 cannot be stored
24-
* in a `uint64_t` data type.
26+
* @brief Maximum Fibonacci number that can be computed
27+
*
28+
* @details
29+
* The result after 93 cannot be stored in a `uint64_t` data type.
2530
*/
31+
constexpr uint64_t MAX = 93;
2632

27-
#define MAX 93
28-
29-
/** Algorithm */
33+
/**
34+
* @brief Function to compute the nth Fibonacci number
35+
* @param n The index of the Fibonacci number to compute
36+
* @return uint64_t The nth Fibonacci number
37+
*/
3038
uint64_t fib(uint64_t n) {
31-
static uint64_t f1 = 1,
32-
f2 = 1; // using static keyword will retain the values of
33-
// f1 and f2 for the next function call.
39+
// Using static keyword will retain the values of
40+
// f1 and f2 for the next function call.
41+
static uint64_t f1 = 1, f2 = 1;
3442

35-
if (n <= 2)
43+
if (n <= 2) {
3644
return f2;
37-
if (n >= 93) {
38-
std::cerr
39-
<< "Cannot compute for n>93 due to limit of 64-bit integers\n";
45+
} if (n >= MAX) {
46+
throw std::invalid_argument("Cannot compute for n>=" + std::to_string(MAX) +
47+
" due to limit of 64-bit integers");
4048
return 0;
4149
}
42-
43-
uint64_t temp = f2; // we do not need temp to be static
50+
51+
// We do not need temp to be static.
52+
uint64_t temp = f2;
4453
f2 += f1;
4554
f1 = temp;
4655

4756
return f2;
4857
}
4958

50-
/** Main function */
51-
int main() {
52-
// Main Function
53-
for (uint64_t i = 1; i < 93; i++) {
54-
std::cout << i << " th fibonacci number is " << fib(i) << std::endl;
59+
/**
60+
* @brief Function to test the Fibonacci computation
61+
* @returns void
62+
*/
63+
static void test() {
64+
// Test for valid Fibonacci numbers
65+
assert(fib(1) == 1);
66+
assert(fib(2) == 1);
67+
assert(fib(3) == 2);
68+
assert(fib(4) == 3);
69+
assert(fib(5) == 5);
70+
assert(fib(6) == 8);
71+
assert(fib(7) == 13);
72+
assert(fib(8) == 21);
73+
assert(fib(9) == 34);
74+
assert(fib(10) == 55);
75+
assert(fib(11) == 89);
76+
assert(fib(12) == 144);
77+
assert(fib(13) == 233);
78+
assert(fib(14) == 377);
79+
assert(fib(15) == 610);
80+
assert(fib(16) == 987);
81+
assert(fib(17) == 1597);
82+
assert(fib(18) == 2584);
83+
assert(fib(19) == 4181);
84+
assert(fib(20) == 6765);
85+
assert(fib(21) == 10946);
86+
assert(fib(22) == 17711);
87+
assert(fib(23) == 28657);
88+
assert(fib(24) == 46368);
89+
assert(fib(25) == 75025);
90+
assert(fib(26) == 121393);
91+
assert(fib(27) == 196418);
92+
assert(fib(28) == 317811);
93+
assert(fib(29) == 514229);
94+
assert(fib(30) == 832040);
95+
assert(fib(31) == 1346269);
96+
assert(fib(32) == 2178309);
97+
assert(fib(33) == 3524578);
98+
assert(fib(34) == 5702887);
99+
assert(fib(35) == 9227465);
100+
assert(fib(36) == 14930352);
101+
assert(fib(37) == 24157817);
102+
assert(fib(38) == 39088169);
103+
assert(fib(39) == 63245986);
104+
assert(fib(40) == 102334155);
105+
assert(fib(41) == 165580141);
106+
assert(fib(42) == 267914296);
107+
assert(fib(43) == 433494437);
108+
assert(fib(44) == 701408733);
109+
assert(fib(45) == 1134903170);
110+
assert(fib(46) == 1836311903);
111+
assert(fib(47) == 2971215073);
112+
assert(fib(48) == 4807526976);
113+
assert(fib(49) == 7778742049);
114+
assert(fib(50) == 12586269025);
115+
assert(fib(51) == 20365011074);
116+
assert(fib(52) == 32951280099);
117+
assert(fib(53) == 53316291173);
118+
assert(fib(54) == 86267571272);
119+
assert(fib(55) == 139583862445);
120+
assert(fib(56) == 225851433717);
121+
assert(fib(57) == 365435296162);
122+
assert(fib(58) == 591286729879);
123+
assert(fib(59) == 956722026041);
124+
assert(fib(60) == 1548008755920);
125+
assert(fib(61) == 2504730781961);
126+
assert(fib(62) == 4052739537881);
127+
assert(fib(63) == 6557470319842);
128+
assert(fib(64) == 10610209857723);
129+
assert(fib(65) == 17167680177565);
130+
assert(fib(66) == 27777890035288);
131+
assert(fib(67) == 44945570212853);
132+
assert(fib(68) == 72723460248141);
133+
assert(fib(69) == 117669030460994);
134+
assert(fib(70) == 190392490709135);
135+
assert(fib(71) == 308061521170129);
136+
assert(fib(72) == 498454011879264);
137+
assert(fib(73) == 806515533049393);
138+
assert(fib(74) == 1304969544928657);
139+
assert(fib(75) == 2111485077978050);
140+
assert(fib(76) == 3416454622906707);
141+
assert(fib(77) == 5527939700884757);
142+
assert(fib(78) == 8944394323791464);
143+
assert(fib(79) == 14472334024676221);
144+
assert(fib(80) == 23416728348467685);
145+
assert(fib(81) == 37889062373143906);
146+
assert(fib(82) == 61305790721611591);
147+
assert(fib(83) == 99194853094755497);
148+
assert(fib(84) == 160500643816367088);
149+
assert(fib(85) == 259695496911122585);
150+
assert(fib(86) == 420196140727489673);
151+
assert(fib(87) == 679891637638612258);
152+
assert(fib(88) == 1100087778366101931);
153+
assert(fib(89) == 1779979416004714189);
154+
assert(fib(90) == 2880067194370816120);
155+
assert(fib(91) == 4660046610375530309);
156+
assert(fib(92) == 7540113804746346429);
157+
158+
// Test for invalid Fibonacci numbers
159+
try {
160+
fib(MAX + 1);
161+
assert(false && "Expected an invalid_argument exception to be thrown");
162+
} catch (const std::invalid_argument& e) {
163+
const std::string expected_message = "Cannot compute for n>=" + std::to_string(MAX) +
164+
" due to limit of 64-bit integers";
165+
assert(e.what() == expected_message);
55166
}
167+
168+
std::cout << "All Fibonacci tests have successfully passed!\n";
169+
}
170+
171+
/**
172+
* @brief Main Function
173+
* @returns 0 on exit
174+
*/
175+
int main() {
176+
test(); // run self-test implementations
56177
return 0;
57178
}

sorting/merge_sort.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* Merge Sort is an efficient, general purpose, comparison
1212
* based sorting algorithm.
1313
* Merge Sort is a divide and conquer algorithm
14-
*
14+
* Time Complexity: O(n log n)
15+
* It is same for all best case, worst case or average case
16+
* Merge Sort is very efficient when for the small data.
17+
* In built-in sort function merge sort along with quick sort is used.
1518
*/
1619
#include <iostream>
1720
#include <vector>

sorting/quick_sort.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,18 @@ namespace quick_sort {
5454
* @param low first point of the array (starting index)
5555
* @param high last point of the array (ending index)
5656
* @returns index of the smaller element
57-
*/
57+
*
58+
* ### Time Complexity
59+
* best case, average Case: O(nlog(n))
60+
* Worst Case: O(n^2) (Worst case occur when the partition
61+
* is consistently unbalanced.)
62+
63+
* ### Space Complexity
64+
* average Case: O(log(n))
65+
* Worst Case: O(n)
66+
* It's space complexity is due to the recursive function calls and partitioning process.
67+
*/
68+
5869
template <typename T>
5970
int partition(std::vector<T> *arr, const int &low, const int &high) {
6071
T pivot = (*arr)[high]; // taking the last element as pivot

0 commit comments

Comments
 (0)