Skip to content

Commit ee40cbc

Browse files
authored
Merge branch 'master' into ds/stack
2 parents 9c55a9a + c84c7da commit ee40cbc

File tree

12 files changed

+267
-161
lines changed

12 files changed

+267
-161
lines changed

.github/workflows/awesome_workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
submodules: true
6262
- run: |
6363
cmake -B ./build -S .
64-
cmake --build build
64+
cmake --build build --parallel 4
6565
- name: Label on PR fail
6666
uses: actions/github-script@v6
6767
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}

.github/workflows/gh-pages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
clean: false
2626
- name: Move & Commit files
2727
run: |
28-
git config --global user.name github-actions
29-
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
28+
git config --global user.name "$GITHUB_ACTOR"
29+
git config --global user.email "$[email protected]"
3030
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
3131
rm -rf d* && rm *.html && rm *.svg && rm *.map && rm *.md5 && rm *.png && rm *.js && rm *.css
3232
git add .

greedy_algorithms/jump_game.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @file
3+
* @brief [Jumping Game](https://leetcode.com/problems/jump-game/)
4+
* algorithm implementation
5+
* @details
6+
*
7+
* Given an array of non-negative integers, you are initially positioned at the
8+
* first index of the array. Each element in the array represents your maximum
9+
* jump length at that position. Determine if you are able to reach the last
10+
* index. This solution takes in input as a vector and output as a boolean to
11+
* check if you can reach the last position. We name the indices good and bad
12+
* based on whether we can reach the destination if we start at that position.
13+
* We initialize the last index as lastPos.
14+
* Here, we start from the end of the array and check if we can ever reach the
15+
* first index. We check if the sum of the index and the maximum jump count
16+
* given is greater than or equal to the lastPos. If yes, then that is the last
17+
* position you can reach starting from the back. After the end of the loop, if
18+
* we reach the lastPos as 0, then the destination can be reached from the start
19+
* position.
20+
*
21+
* @author [Rakshaa Viswanathan](https://github.com/rakshaa2000)
22+
* @author [David Leal](https://github.com/Panquesito7)
23+
*/
24+
25+
#include <cassert> /// for assert
26+
#include <iostream> /// for std::cout
27+
#include <vector> /// for std::vector
28+
29+
/**
30+
* @namespace
31+
* @brief Greedy Algorithms
32+
*/
33+
namespace greedy_algorithms {
34+
/**
35+
* @brief Checks whether the given element (default is `1`) can jump to the last
36+
* index.
37+
* @param nums array of numbers containing the maximum jump (in steps) from that
38+
* index
39+
* @returns true if the index can be reached
40+
* @returns false if the index can NOT be reached
41+
*/
42+
bool can_jump(const std::vector<int> &nums) {
43+
size_t lastPos = nums.size() - 1;
44+
for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
45+
if (i + nums[i] >= lastPos) {
46+
lastPos = i;
47+
}
48+
}
49+
return lastPos == 0;
50+
}
51+
} // namespace greedy_algorithms
52+
53+
/**
54+
* @brief Function to test the above algorithm
55+
* @returns void
56+
*/
57+
static void test() {
58+
assert(greedy_algorithms::can_jump(std::vector<int>({4, 3, 1, 0, 5})));
59+
assert(!greedy_algorithms::can_jump(std::vector<int>({3, 2, 1, 0, 4})));
60+
assert(greedy_algorithms::can_jump(std::vector<int>({5, 9, 4, 7, 15, 3})));
61+
assert(!greedy_algorithms::can_jump(std::vector<int>({1, 0, 5, 8, 12})));
62+
assert(greedy_algorithms::can_jump(std::vector<int>({2, 1, 4, 7})));
63+
64+
std::cout << "All tests have successfully passed!\n";
65+
}
66+
67+
/**
68+
* @brief Main function
69+
* @returns 0 on exit
70+
*/
71+
int main() {
72+
test(); // run self-test implementations
73+
return 0;
74+
}

greedy_algorithms/jumpgame.cpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

math/extended_euclid_algorithm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
#include <algorithm> // for swap function
1313
#include <iostream>
14+
#include <cstdint>
1415

1516
/**
1617
* function to update the coefficients per iteration

math/iterative_factorial.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace math {
4646
*/
4747
uint64_t iterativeFactorial(uint8_t n) {
4848
if (n > 20) {
49-
throw new std::invalid_argument("Maximum n value is 20");
49+
throw std::invalid_argument("Maximum n value is 20");
5050
}
5151

5252
// 1 because it is the identity number of multiplication.
@@ -101,12 +101,14 @@ static void test() {
101101
std::cout << "Exception test \n"
102102
"Input: 21 \n"
103103
"Expected output: Exception thrown \n";
104+
105+
bool wasExceptionThrown = false;
104106
try {
105107
math::iterativeFactorial(21);
106-
} catch (std::invalid_argument* e) {
107-
std::cout << "Exception thrown successfully \nContent: " << e->what()
108-
<< "\n";
108+
} catch (const std::invalid_argument&) {
109+
wasExceptionThrown = true;
109110
}
111+
assert(wasExceptionThrown);
110112

111113
std::cout << "All tests have passed successfully.\n";
112114
}

0 commit comments

Comments
 (0)