Skip to content

Commit 034a525

Browse files
Merge branch 'master' into master
2 parents d1ccf58 + 3aafade commit 034a525

File tree

4 files changed

+78
-101
lines changed

4 files changed

+78
-101
lines changed

data_structures/cll/cll.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Simple data structure CLL (Cicular Linear Linked List)
2+
* Simple data structure CLL (Circular Linear Linked List)
33
* */
44
#include <cctype>
55
#include <cstdlib>

graph/max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class Graph {
2727
visited.reset();
2828
std::queue<int> q;
2929
q.push(source);
30-
bool is_path_found = false;
31-
while (q.empty() == false && is_path_found == false) {
30+
while (q.empty() == false) {
3231
int current_node = q.front();
3332
visited.set(current_node);
3433
q.pop();

math/factorial_memoization.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,65 @@
11
/**
22
* @file
3-
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
3+
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using
4+
* recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
45
* @details
56
* This program computes the factorial of a non-negative integer using recursion
6-
* with memoization (top-down dynamic programming). It stores intermediate results
7-
* to avoid redundant calculations for improved efficiency.
8-
*
9-
* Memoization is a form of caching where the result to an expensive function call
10-
* is stored and returned.
11-
* Example:
12-
* Input: n = 5
13-
* Output: 120
14-
*
7+
* with memoization (top-down dynamic programming). It stores intermediate
8+
* results to avoid redundant calculations for improved efficiency.
9+
*
10+
* Memoization is a form of caching where the result to an expensive function
11+
* call is stored and returned. Example: Input: n = 5 Output: 120
12+
*
1513
* Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
16-
*
17-
* The program uses a recursive function fact_recursion which caches computed
18-
* results in a memo array to avoid recalculating factorials for the same numbers.
14+
*
15+
* The program uses a recursive function which caches computed
16+
* results in a memo array to avoid recalculating factorials for the same
17+
* numbers.
1918
*
2019
* Time Complexity: O(n)
2120
* Space Complexity: O(n)
22-
* @author [Vedant Mukhedkar](https://github.com/git5v)
2321
*/
2422

25-
#include <iostream> // for std::cout
2623
#include <cassert> // For test cases
27-
#include <array> // For std::array
2824
#include <cstdint> // For uint64_t
25+
#include <vector> // For std::vector
2926

30-
/// Array to store computed factorials for memoization
31-
std::array<uint64_t, 1000> memo{0};
27+
class MemorisedFactorial {
28+
std::vector<std::uint64_t> known_values = {1};
3229

33-
/**
34-
* @namespace math
35-
* @brief Math algorithms
36-
*/
37-
namespace math {
30+
public:
31+
/**
32+
* @note This function was intentionally written as recursive
33+
* and it does not handle overflows.
34+
* @returns factorial of n
35+
*/
36+
std::uint64_t operator()(std::uint64_t n) {
37+
if (n >= this->known_values.size()) {
38+
this->known_values.push_back(n * this->operator()(n - 1));
39+
}
40+
return this->known_values.at(n);
41+
}
42+
};
3843

39-
/**
40-
* @brief Computes the factorial of a non-negative integer using recursion and memoization.
41-
* @param n The integer whose factorial is to be computed
42-
* @returns The factorial of n
43-
*/
44-
uint64_t fact_recursion(uint64_t n) {
45-
if (n == 0) return 1; // Base case: 0! = 1
46-
if (memo[n] != 0) return memo[n]; // Return already computed value
47-
memo[n] = n * fact_recursion(n - 1); // Store and return the computed value
48-
return memo[n];
44+
void test_MemorisedFactorial_in_order() {
45+
auto factorial = MemorisedFactorial();
46+
assert(factorial(0) == 1);
47+
assert(factorial(1) == 1);
48+
assert(factorial(5) == 120);
49+
assert(factorial(10) == 3628800);
4950
}
5051

51-
} // namespace math
52-
/**
53-
* @brief Self-test implementations for the fact_recursion function.
54-
* @returns void
55-
*/
56-
void test_fact_recursion() {
57-
// Test cases for factorial computation
58-
assert(math::fact_recursion(0) == 1);
59-
assert(math::fact_recursion(1) == 1);
60-
assert(math::fact_recursion(5) == 120);
61-
assert(math::fact_recursion(10) == 3628800);
62-
std::cout << "All test cases passed!\n";
52+
void test_MemorisedFactorial_no_order() {
53+
auto factorial = MemorisedFactorial();
54+
assert(factorial(10) == 3628800);
6355
}
6456

6557
/**
66-
* @brief Main function to run test cases and interact with the user.
58+
* @brief Main function to run tests
6759
* @returns 0 on program success
6860
*/
6961
int main() {
70-
// Run test cases
71-
test_fact_recursion();
62+
test_MemorisedFactorial_in_order();
63+
test_MemorisedFactorial_no_order();
7264
return 0;
7365
}

others/postfix_evaluation.cpp

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief Evaluation of [Postfix Expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
3+
* @brief Evaluation of [Postfix
4+
* Expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
45
* @author [Darshana Sarma](https://github.com/Darshana-Sarma)
56
* @details
67
* Create a stack to store operands (or values).
@@ -11,9 +12,10 @@
1112
* When the expression is ended, the number in the stack is the final answer
1213
*/
1314
#include <algorithm> // for all_of
14-
#include <array> // for std::array
15+
#include <array> // for array
1516
#include <cassert> // for assert
1617
#include <iostream> // for io operations
18+
#include <stack> // for std::stack
1719
#include <string> // for stof
1820

1921
/**
@@ -26,36 +28,6 @@ namespace others {
2628
* @brief Functions for Postfix Expression algorithm
2729
*/
2830
namespace postfix_expression {
29-
/**
30-
* @brief Creates an array to be used as stack for storing values
31-
*/
32-
class Stack {
33-
public:
34-
std::array<float, 20> stack{}; ///< Array which will be used to store numbers in the input
35-
int stackTop = -1; ///< Represents the index of the last value added to array. -1 means array is empty
36-
};
37-
38-
/**
39-
* @brief Pushing operand, also called the number in the array to the stack
40-
* @param operand float value from the input array or evaluation
41-
* @param stack stack containing numbers
42-
* @returns none
43-
*/
44-
void push(float operand, Stack *stack) {
45-
stack->stackTop++;
46-
stack->stack[stack->stackTop] = operand;
47-
}
48-
49-
/**
50-
* @brief Popping operand, also called the number from the stack
51-
* @param stack stack containing numbers
52-
* @returns operand float on top of stack
53-
*/
54-
float pop(Stack *stack) {
55-
float operand = stack->stack[stack->stackTop];
56-
stack->stackTop--;
57-
return operand;
58-
}
5931

6032
/**
6133
* @brief Checks if scanned string is a number
@@ -74,28 +46,29 @@ bool is_number(const std::string &s) {
7446
* @param stack containing numbers
7547
* @returns none
7648
*/
77-
void evaluate(float a, float b, const std::string &operation, Stack *stack) {
49+
void evaluate(float a, float b, const std::string &operation,
50+
std::stack<float> &stack) {
7851
float c = 0;
7952
const char *op = operation.c_str();
8053
switch (*op) {
8154
case '+':
82-
c = a + b; // Addition of numbers
83-
others::postfix_expression::push(c, stack);
55+
c = a + b; // Addition of numbers
56+
stack.push(c);
8457
break;
8558

8659
case '-':
87-
c = a - b; // Subtraction of numbers
88-
others::postfix_expression::push(c, stack);
60+
c = a - b; // Subtraction of numbers
61+
stack.push(c);
8962
break;
9063

9164
case '*':
92-
c = a * b; // Multiplication of numbers
93-
others::postfix_expression::push(c, stack);
65+
c = a * b; // Multiplication of numbers
66+
stack.push(c);
9467
break;
9568

9669
case '/':
97-
c = a / b; // Division of numbers
98-
others::postfix_expression::push(c, stack);
70+
c = a / b; // Division of numbers
71+
stack.push(c);
9972
break;
10073

10174
default:
@@ -113,31 +86,32 @@ void evaluate(float a, float b, const std::string &operation, Stack *stack) {
11386
*/
11487
template <std::size_t N>
11588
float postfix_evaluation(std::array<std::string, N> input) {
116-
Stack stack;
89+
std::stack<float> stack;
11790
int j = 0;
11891

11992
while (j < N) {
12093
std::string scan = input[j];
12194
if (is_number(scan)) {
122-
push(std::stof(scan), &stack);
95+
stack.push(std::stof(scan));
12396

12497
} else {
125-
float op2 = pop(&stack);
126-
float op1 = pop(&stack);
98+
const float op2 = stack.top();
99+
stack.pop();
100+
const float op1 = stack.top();
101+
stack.pop();
127102

128-
evaluate(op1, op2, scan, &stack);
103+
evaluate(op1, op2, scan, stack);
129104
}
130105
j++;
131106
}
132107

133-
std::cout << stack.stack[stack.stackTop] << "\n";
108+
std::cout << stack.top() << "\n";
134109

135-
return stack.stack[stack.stackTop];
110+
return stack.top();
136111
}
137112
} // namespace postfix_expression
138113
} // namespace others
139114

140-
141115
/**
142116
* @brief Test function 1 with input array
143117
* {'2', '3', '1', '*', '+', '9', '-'}
@@ -153,7 +127,7 @@ static void test_function_1() {
153127

154128
/**
155129
* @brief Test function 2 with input array
156-
* {'1', '2', '+', '2', '/', '5', '*', '7', '+'}
130+
* {'100', '200', '+', '2', '/', '5', '*', '7', '+'}
157131
* @returns none
158132
*/
159133
static void test_function_2() {
@@ -164,13 +138,25 @@ static void test_function_2() {
164138
assert(answer == 757);
165139
}
166140

141+
static void test_function_3() {
142+
std::array<std::string, 43> input = {
143+
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
144+
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
145+
"+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
146+
"+", "+", "+", "+", "+", "+", "+", "+", "+", "+"};
147+
float answer = others::postfix_expression::postfix_evaluation(input);
148+
149+
assert(answer == 22);
150+
}
151+
167152
/**
168153
* @brief Main function
169154
* @returns 0 on exit
170155
*/
171156
int main() {
172157
test_function_1();
173158
test_function_2();
159+
test_function_3();
174160

175161
std::cout << "\nTest implementations passed!\n";
176162

0 commit comments

Comments
 (0)