Skip to content

Commit 3f3f524

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents cad4d9e + e841605 commit 3f3f524

File tree

10 files changed

+1147
-138
lines changed

10 files changed

+1147
-138
lines changed

DIRECTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
## Greedy Algorithms
163163
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
164+
* [Digit Separation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/digit_separation.cpp)
164165
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
165166
* [Gale Shapley](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/gale_shapley.cpp)
166167
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
@@ -300,6 +301,8 @@
300301
* [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/iterative_tree_traversals.cpp)
301302
* [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kadanes3.cpp)
302303
* [Kelvin To Celsius](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/kelvin_to_celsius.cpp)
304+
* [Lfu Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lfu_cache.cpp)
305+
* [Longest Substring Without Repeating Characters](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/longest_substring_without_repeating_characters.cpp)
303306
* [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/lru_cache.cpp)
304307
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/matrix_exponentiation.cpp)
305308
* [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/others/palindrome_of_number.cpp)
@@ -322,6 +325,7 @@
322325
* [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp)
323326
* [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp)
324327
* [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp)
328+
* [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp)
325329
* [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp)
326330
* [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp)
327331
* [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp)
@@ -369,6 +373,7 @@
369373
* [Gnome Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/gnome_sort.cpp)
370374
* [Heap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/heap_sort.cpp)
371375
* [Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort.cpp)
376+
* [Insertion Sort Recursive](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/insertion_sort_recursive.cpp)
372377
* [Library Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/library_sort.cpp)
373378
* [Merge Insertion Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_insertion_sort.cpp)
374379
* [Merge Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/sorting/merge_sort.cpp)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @file digit_separation.cpp
3+
* @brief Separates digits from numbers in forward and reverse order
4+
* @see https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html
5+
* @details The DigitSeparation class provides two methods to separate the
6+
* digits of large integers: digitSeparationReverseOrder and
7+
* digitSeparationForwardOrder. The digitSeparationReverseOrder method extracts
8+
* digits by repeatedly applying the modulus operation (% 10) to isolate the
9+
* last digit, then divides the number by 10 to remove it. This process
10+
* continues until the entire number is broken down into its digits, which are
11+
* stored in reverse order. If the number is zero, the method directly returns a
12+
* vector containing {0} to handle this edge case. Negative numbers are handled
13+
* by taking the absolute value, ensuring consistent behavior regardless of the
14+
* sign.
15+
* @author [Muhammad Junaid Khalid](https://github.com/mjk22071998)
16+
*/
17+
18+
#include <algorithm> /// For reveresing the vector
19+
#include <cassert> /// For assert() function to check for errors
20+
#include <cmath> /// For abs() function
21+
#include <cstdint> /// For int64_t data type to handle large numbers
22+
#include <iostream> /// For input/output operations
23+
#include <vector> /// For std::vector to store separated digits
24+
25+
/**
26+
* @namespace
27+
* @brief Greedy Algorithms
28+
*/
29+
namespace greedy_algorithms {
30+
31+
/**
32+
* @brief A class that provides methods to separate the digits of a large
33+
* positive number.
34+
*/
35+
class DigitSeparation {
36+
public:
37+
/**
38+
* @brief Default constructor for the DigitSeparation class.
39+
*/
40+
DigitSeparation() {}
41+
42+
/**
43+
* @brief Implementation of digitSeparationReverseOrder method.
44+
*
45+
* @param largeNumber The large number to separate digits from.
46+
* @return A vector of digits in reverse order.
47+
*/
48+
std::vector<std::int64_t> digitSeparationReverseOrder(
49+
std::int64_t largeNumber) const {
50+
std::vector<std::int64_t> result;
51+
if (largeNumber != 0) {
52+
while (largeNumber != 0) {
53+
result.push_back(std::abs(largeNumber % 10));
54+
largeNumber /= 10;
55+
}
56+
} else {
57+
result.push_back(0);
58+
}
59+
return result;
60+
}
61+
62+
/**
63+
* @brief Implementation of digitSeparationForwardOrder method.
64+
*
65+
* @param largeNumber The large number to separate digits from.
66+
* @return A vector of digits in forward order.
67+
*/
68+
std::vector<std::int64_t> digitSeparationForwardOrder(
69+
std::int64_t largeNumber) const {
70+
std::vector<std::int64_t> result =
71+
digitSeparationReverseOrder(largeNumber);
72+
std::reverse(result.begin(), result.end());
73+
return result;
74+
}
75+
};
76+
77+
} // namespace greedy_algorithms
78+
79+
/**
80+
* @brief self test implementation
81+
* @return void
82+
*/
83+
static void tests() {
84+
greedy_algorithms::DigitSeparation ds;
85+
86+
// Test case: Positive number
87+
std::int64_t number = 1234567890;
88+
std::vector<std::int64_t> expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
89+
std::vector<std::int64_t> expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
90+
std::vector<std::int64_t> reverseOrder =
91+
ds.digitSeparationReverseOrder(number);
92+
assert(reverseOrder == expectedReverse);
93+
std::vector<std::int64_t> forwardOrder =
94+
ds.digitSeparationForwardOrder(number);
95+
assert(forwardOrder == expectedForward);
96+
97+
// Test case: Single digit number
98+
number = 5;
99+
expectedReverse = {5};
100+
expectedForward = {5};
101+
reverseOrder = ds.digitSeparationReverseOrder(number);
102+
assert(reverseOrder == expectedReverse);
103+
forwardOrder = ds.digitSeparationForwardOrder(number);
104+
assert(forwardOrder == expectedForward);
105+
106+
// Test case: Zero
107+
number = 0;
108+
expectedReverse = {0};
109+
expectedForward = {0};
110+
reverseOrder = ds.digitSeparationReverseOrder(number);
111+
assert(reverseOrder == expectedReverse);
112+
forwardOrder = ds.digitSeparationForwardOrder(number);
113+
assert(forwardOrder == expectedForward);
114+
115+
// Test case: Large number
116+
number = 987654321012345;
117+
expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
118+
expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
119+
reverseOrder = ds.digitSeparationReverseOrder(number);
120+
assert(reverseOrder == expectedReverse);
121+
forwardOrder = ds.digitSeparationForwardOrder(number);
122+
assert(forwardOrder == expectedForward);
123+
124+
// Test case: Negative number
125+
number = -987654321012345;
126+
expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
127+
expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5};
128+
reverseOrder = ds.digitSeparationReverseOrder(number);
129+
assert(reverseOrder == expectedReverse);
130+
forwardOrder = ds.digitSeparationForwardOrder(number);
131+
assert(forwardOrder == expectedForward);
132+
}
133+
134+
/**
135+
* @brief main function
136+
* @return 0 on successful exit
137+
*/
138+
int main() {
139+
tests(); // run self test implementation
140+
141+
return 0;
142+
}

math/area.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ template <typename T>
109109
T cylinder_surface_area(T radius, T height) {
110110
return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
111111
}
112+
113+
/**
114+
* @brief surface area of a [hemi-sphere](https://en.wikipedia.org/wiki/Surface_area) ( 3 *
115+
* pi * r^2)
116+
* @param radius is the radius of the hemi-sphere
117+
* @tparam T datatype of radius
118+
* @returns surface area of the hemi-sphere
119+
*/
120+
template <typename T>
121+
T hemi_sphere_surface_area(T radius) {
122+
return 3 * M_PI * pow(radius, 2);
123+
}
112124
} // namespace math
113125

114126
/**
@@ -267,6 +279,18 @@ static void test() {
267279
std::cout << "Output: " << double_area << std::endl;
268280
assert(double_area == double_expected);
269281
std::cout << "TEST PASSED" << std::endl << std::endl;
282+
283+
// 11th test
284+
double_radius = 10.0;
285+
double_expected = 942.4777960769379;
286+
double_area = math::hemi_sphere_surface_area(double_radius);
287+
288+
std::cout << "SURFACE AREA OF A HEMI-SPHERE" << std::endl;
289+
std::cout << "Input Radius: " << double_radius << std::endl;
290+
std::cout << "Expected Output: " << double_expected << std::endl;
291+
std::cout << "Output: " << double_area << std::endl;
292+
assert(double_area == double_expected);
293+
std::cout << "TEST PASSED" << std::endl << std::endl;
270294
}
271295

272296
/**

math/fibonacci.cpp

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
11
/**
22
* @file
3-
* @brief Generate fibonacci sequence
3+
* @brief n-th [Fibonacci
4+
* number](https://en.wikipedia.org/wiki/Fibonacci_sequence).
45
*
5-
* Calculate the the value on Fibonacci's sequence given an
6-
* integer as input.
6+
* @details
7+
* Naive recursive implementation to calculate the n-th Fibonacci number.
78
* \f[\text{fib}(n) = \text{fib}(n-1) + \text{fib}(n-2)\f]
89
*
910
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
1011
*/
11-
#include <cassert>
12-
#include <iostream>
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for IO operations
1314

1415
/**
15-
* Recursively compute sequences
16-
* @param n input
17-
* @returns n-th element of the Fbinacci's sequence
16+
* @namespace math
17+
* @brief Math algorithms
18+
*/
19+
namespace math {
20+
/**
21+
* @namespace fibonacci
22+
* @brief Functions for Fibonacci sequence
23+
*/
24+
namespace fibonacci {
25+
/**
26+
* @brief Function to compute the n-th Fibonacci number
27+
* @param n the index of the Fibonacci number
28+
* @returns n-th element of the Fibonacci's sequence
1829
*/
1930
uint64_t fibonacci(uint64_t n) {
20-
/* If the input is 0 or 1 just return the same
21-
This will set the first 2 values of the sequence */
31+
// If the input is 0 or 1 just return the same (Base Case)
32+
// This will set the first 2 values of the sequence
2233
if (n <= 1) {
2334
return n;
2435
}
2536

26-
/* Add the last 2 values of the sequence to get next */
37+
// Add the preceding 2 values of the sequence to get next
2738
return fibonacci(n - 1) + fibonacci(n - 2);
2839
}
40+
} // namespace fibonacci
41+
} // namespace math
2942

3043
/**
31-
* Function for testing the fibonacci() function with a few
32-
* test cases and assert statement.
44+
* @brief Self-test implementation
3345
* @returns `void`
34-
*/
46+
*/
3547
static void test() {
36-
uint64_t test_case_1 = fibonacci(0);
37-
assert(test_case_1 == 0);
38-
std::cout << "Passed Test 1!" << std::endl;
39-
40-
uint64_t test_case_2 = fibonacci(1);
41-
assert(test_case_2 == 1);
42-
std::cout << "Passed Test 2!" << std::endl;
43-
44-
uint64_t test_case_3 = fibonacci(2);
45-
assert(test_case_3 == 1);
46-
std::cout << "Passed Test 3!" << std::endl;
47-
48-
uint64_t test_case_4 = fibonacci(3);
49-
assert(test_case_4 == 2);
50-
std::cout << "Passed Test 4!" << std::endl;
51-
52-
uint64_t test_case_5 = fibonacci(4);
53-
assert(test_case_5 == 3);
54-
std::cout << "Passed Test 5!" << std::endl;
55-
56-
uint64_t test_case_6 = fibonacci(15);
57-
assert(test_case_6 == 610);
58-
std::cout << "Passed Test 6!" << std::endl << std::endl;
48+
assert(math::fibonacci::fibonacci(0) == 0);
49+
assert(math::fibonacci::fibonacci(1) == 1);
50+
assert(math::fibonacci::fibonacci(2) == 1);
51+
assert(math::fibonacci::fibonacci(3) == 2);
52+
assert(math::fibonacci::fibonacci(4) == 3);
53+
assert(math::fibonacci::fibonacci(15) == 610);
54+
assert(math::fibonacci::fibonacci(20) == 6765);
55+
std::cout << "All tests have passed successfully!\n";
5956
}
6057

61-
/// Main function
58+
/**
59+
* @brief Main function
60+
* @returns 0 on exit
61+
*/
6262
int main() {
63-
test();
64-
int n = 0;
65-
std::cin >> n;
66-
assert(n >= 0);
67-
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
63+
test(); // run self-test implementations
64+
return 0;
6865
}

0 commit comments

Comments
 (0)