Skip to content

Commit 0d7f61b

Browse files
Merge branch 'master' into patch-1
2 parents c4219a3 + 1d69222 commit 0d7f61b

File tree

5 files changed

+83
-73
lines changed

5 files changed

+83
-73
lines changed

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

sorting/bogo_sort.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <algorithm>
1919
#include <array>
2020
#include <cassert>
21+
#include <random>
2122

2223

2324
/**
@@ -50,8 +51,10 @@ std::array <T, N> shuffle (std::array <T, N> arr) {
5051
template <typename T, size_t N>
5152
std::array <T, N> randomized_bogosort (std::array <T, N> arr) {
5253
// Untill array is not sorted
54+
std::random_device random_device;
55+
std::mt19937 generator(random_device());
5356
while (!std::is_sorted(arr.begin(), arr.end())) {
54-
std::random_shuffle(arr.begin(), arr.end());// Shuffle the array
57+
std::shuffle(arr.begin(), arr.end(), generator);// Shuffle the array
5558
}
5659
return arr;
5760
}

0 commit comments

Comments
 (0)