Skip to content

Commit f8ac592

Browse files
Merge branch 'master' into iterative_quick_sort
2 parents 8982255 + 9702903 commit f8ac592

File tree

14 files changed

+375
-165
lines changed

14 files changed

+375
-165
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 .
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the [Trapped Rainwater
4+
* Problem](https://www.geeksforgeeks.org/trapping-rain-water/)
5+
* @details
6+
* This implementation calculates the amount of rainwater that can be trapped
7+
* between walls represented by an array of heights.
8+
* @author [SOZEL](https://github.com/TruongNhanNguyen)
9+
*/
10+
11+
#include <algorithm> /// For std::min and std::max
12+
#include <cassert> /// For assert
13+
#include <cstddef> /// For std::size_t
14+
#include <cstdint> /// For integral typedefs
15+
#include <vector> /// For std::vector
16+
17+
/*
18+
* @namespace
19+
* @brief Dynamic Programming Algorithms
20+
*/
21+
namespace dynamic_programming {
22+
/**
23+
* @brief Function to calculate the trapped rainwater
24+
* @param heights Array representing the heights of walls
25+
* @return The amount of trapped rainwater
26+
*/
27+
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
28+
std::size_t n = heights.size();
29+
if (n <= 2)
30+
return 0; // No water can be trapped with less than 3 walls
31+
32+
std::vector<uint32_t> leftMax(n), rightMax(n);
33+
34+
// Calculate the maximum height of wall to the left of each wall
35+
leftMax[0] = heights[0];
36+
for (std::size_t i = 1; i < n; ++i) {
37+
leftMax[i] = std::max(leftMax[i - 1], heights[i]);
38+
}
39+
40+
// Calculate the maximum height of wall to the right of each wall
41+
rightMax[n - 1] = heights[n - 1];
42+
for (std::size_t i = n - 2; i < n; --i) {
43+
rightMax[i] = std::max(rightMax[i + 1], heights[i]);
44+
}
45+
46+
// Calculate the trapped rainwater between walls
47+
uint32_t trappedWater = 0;
48+
for (std::size_t i = 0; i < n; ++i) {
49+
trappedWater +=
50+
std::max(0u, std::min(leftMax[i], rightMax[i]) - heights[i]);
51+
}
52+
53+
return trappedWater;
54+
}
55+
56+
} // namespace dynamic_programming
57+
58+
/**
59+
* @brief Self-test implementations
60+
* @returns void
61+
*/
62+
static void test() {
63+
std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
64+
assert(dynamic_programming::trappedRainwater(test_basic) == 6);
65+
66+
std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4};
67+
assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7);
68+
69+
std::vector<uint32_t> test_bucket = {5, 1, 5};
70+
assert(dynamic_programming::trappedRainwater(test_bucket) == 4);
71+
72+
std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
73+
assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3);
74+
75+
std::vector<uint32_t> test_empty = {};
76+
assert(dynamic_programming::trappedRainwater(test_empty) == 0);
77+
78+
std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
79+
assert(dynamic_programming::trappedRainwater(test_flat) == 0);
80+
81+
std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0};
82+
assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0);
83+
84+
std::vector<uint32_t> test_single_elevation = {5};
85+
assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0);
86+
87+
std::vector<uint32_t> test_two_point_elevation = {5, 1};
88+
assert(dynamic_programming::trappedRainwater(test_two_point_elevation) ==
89+
0);
90+
91+
std::vector<uint32_t> test_large_elevation_map_difference = {5, 1, 6, 1,
92+
7, 1, 8};
93+
assert(dynamic_programming::trappedRainwater(
94+
test_large_elevation_map_difference) == 15);
95+
}
96+
97+
/**
98+
* @brief Main function
99+
* @returns 0 on exit
100+
*/
101+
int main() {
102+
test(); // run self-test implementations
103+
return 0;
104+
}

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.

machine_learning/neural_network.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class DenseLayer {
136136
* @param neurons number of neurons
137137
* @param activation activation function for layer
138138
* @param kernel_shape shape of kernel
139-
* @param random_kernel flag for whether to intialize kernel randomly
139+
* @param random_kernel flag for whether to initialize kernel randomly
140140
*/
141141
DenseLayer(const int &neurons, const std::string &activation,
142142
const std::pair<size_t, size_t> &kernel_shape,
@@ -502,7 +502,7 @@ class NeuralNetwork {
502502
auto start =
503503
std::chrono::high_resolution_clock::now(); // Start clock
504504
double loss = 0,
505-
acc = 0; // Intialize performance metrics with zero
505+
acc = 0; // Initialize performance metrics with zero
506506
// For each starting index of batch
507507
for (size_t batch_start = 0; batch_start < X.size();
508508
batch_start += batch_size) {
@@ -515,7 +515,7 @@ class NeuralNetwork {
515515
// They will be averaged and applied to kernel
516516
std::vector<std::vector<std::valarray<double>>> gradients;
517517
gradients.resize(this->layers.size());
518-
// First intialize gradients to zero
518+
// First initialize gradients to zero
519519
for (size_t i = 0; i < gradients.size(); i++) {
520520
zeroes_initialization(
521521
gradients[i], get_shape(this->layers[i].kernel));
@@ -606,7 +606,7 @@ class NeuralNetwork {
606606
void evaluate(const std::vector<std::vector<std::valarray<double>>> &X,
607607
const std::vector<std::vector<std::valarray<double>>> &Y) {
608608
std::cout << "INFO: Evaluation Started" << std::endl;
609-
double acc = 0, loss = 0; // intialize performance metrics with zero
609+
double acc = 0, loss = 0; // initialize performance metrics with zero
610610
for (size_t i = 0; i < X.size(); i++) { // For every sample in input
611611
// Get predictions
612612
std::vector<std::valarray<double>> pred =

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)