From 47af70eb684888dcb44a6c2fadca96771cbaffa3 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 10:13:16 +0500 Subject: [PATCH 01/35] Digit Separation Algorithm added --- greedy_algorithms/digit_separation.cpp | 129 +++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 greedy_algorithms/digit_separation.cpp diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp new file mode 100644 index 00000000000..49b3b4260d6 --- /dev/null +++ b/greedy_algorithms/digit_separation.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +/** + * @brief A class that provides methods to separate the digits of a large + * positive number. + */ +class DigitSeparation { + public: + /** + * @brief Default constructor for the DigitSeparation class. + */ + DigitSeparation() {} + + /** + * @brief Separates the digits of a large positive number into a vector in + * reverse order. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in reverse order. + */ + std::vector digitSeparationReverseOrder( + long long largeNumber) const; + + /** + * @brief Separates the digits of a large positive number into a vector in + * forward order. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in forward order. + */ + std::vector digitSeparationForwardOrder( + long long largeNumber) const; +}; + +/** + * @brief Implementation of digitSeparationReverseOrder method. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in reverse order. + */ +std::vector DigitSeparation::digitSeparationReverseOrder( + long long largeNumber) const { + std::vector result; + if (largeNumber != 0) { + while (largeNumber != 0) { + result.push_back(std::abs(largeNumber % 10)); + largeNumber /= 10; + } + } else { + result.push_back(0); + } + return result; +} + +/** + * @brief Implementation of digitSeparationForwardOrder method. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in forward order. + */ +std::vector DigitSeparation::digitSeparationForwardOrder( + long long largeNumber) const { + std::vector result = digitSeparationReverseOrder(largeNumber); + std::reverse(result.begin(), result.end()); + return result; +} + +int main() { + DigitSeparation ds; + + // Test case: Positive number + long long number = 1234567890; + std::vector expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + std::vector expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + + // Testing reverse order + std::vector reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); + + // Testing forward order + std::vector forwardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); + + // Test case: Single digit number + number = 5; + expectedReverse = {5}; + expectedForward = {5}; + + reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); + + forwardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); + + // Test case: Zero + number = 0; + expectedReverse = {0}; + expectedForward = {0}; + + reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); + + forwardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); + + // Test case: Large number + number = 987654321012345; + expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; + + reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); + + forwardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); + + // Test case: Negative number + number = -987654321012345; + expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; + + std::cout << "All tests passed!" << std::endl; + + return 0; +} From 011c4e2d80e9abf72831ed320047dc48e94f6b64 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 10:33:58 +0500 Subject: [PATCH 02/35] feat: Added namespace greedy_algoriithms --- greedy_algorithms/digit_separation.cpp | 97 +++++++++++++------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 49b3b4260d6..e3ac0091896 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -4,73 +4,76 @@ #include #include +namespace greedy_algorithms{ + /** * @brief A class that provides methods to separate the digits of a large * positive number. */ -class DigitSeparation { - public: - /** - * @brief Default constructor for the DigitSeparation class. - */ - DigitSeparation() {} + class DigitSeparation { + public: + /** + * @brief Default constructor for the DigitSeparation class. + */ + DigitSeparation() {} + + /** + * @brief Separates the digits of a large positive number into a vector in + * reverse order. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in reverse order. + */ + std::vector digitSeparationReverseOrder( + long long largeNumber) const; + + /** + * @brief Separates the digits of a large positive number into a vector in + * forward order. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in forward order. + */ + std::vector digitSeparationForwardOrder( + long long largeNumber) const; + }; /** - * @brief Separates the digits of a large positive number into a vector in - * reverse order. + * @brief Implementation of digitSeparationReverseOrder method. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector digitSeparationReverseOrder( - long long largeNumber) const; + std::vector DigitSeparation::digitSeparationReverseOrder( + long long largeNumber) const { + std::vector result; + if (largeNumber != 0) { + while (largeNumber != 0) { + result.push_back(std::abs(largeNumber % 10)); + largeNumber /= 10; + } + } else { + result.push_back(0); + } + return result; + } /** - * @brief Separates the digits of a large positive number into a vector in - * forward order. + * @brief Implementation of digitSeparationForwardOrder method. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector digitSeparationForwardOrder( - long long largeNumber) const; -}; - -/** - * @brief Implementation of digitSeparationReverseOrder method. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in reverse order. - */ -std::vector DigitSeparation::digitSeparationReverseOrder( - long long largeNumber) const { - std::vector result; - if (largeNumber != 0) { - while (largeNumber != 0) { - result.push_back(std::abs(largeNumber % 10)); - largeNumber /= 10; - } - } else { - result.push_back(0); + std::vector DigitSeparation::digitSeparationForwardOrder( + long long largeNumber) const { + std::vector result = digitSeparationReverseOrder(largeNumber); + std::reverse(result.begin(), result.end()); + return result; } - return result; -} - -/** - * @brief Implementation of digitSeparationForwardOrder method. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in forward order. - */ -std::vector DigitSeparation::digitSeparationForwardOrder( - long long largeNumber) const { - std::vector result = digitSeparationReverseOrder(largeNumber); - std::reverse(result.begin(), result.end()); - return result; } int main() { - DigitSeparation ds; + greedy_algorithms::DigitSeparation ds; // Test case: Positive number long long number = 1234567890; From 76edb15be4b63d371faad72101be20347df66c92 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 12:32:23 +0500 Subject: [PATCH 03/35] "Updated digit separation code: added comments and docstrings, changed data types to std::int64_t, and reformatted code." --- greedy_algorithms/digit_separation.cpp | 55 ++++++++++++++++---------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index e3ac0091896..3d2476b0350 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -1,9 +1,24 @@ -#include -#include -#include -#include -#include +/** + * @file digit_separation.cpp + * @brief This file contains the DigitSeparation class for separating digits of large numbers. + * https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html + * @details + * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) + * @see related_file.cpp, another_file.cpp + */ + +#include /// For reveresing the vector +#include /// For abs() function +#include /// For input/output operations +#include /// For std::vector to store separated digits +#include /// For assert() function to check for errors +#include /// For int64_t data type to handle large numbers + +/** + * @namespace + * @brief Greedy Algorithms + */ namespace greedy_algorithms{ /** @@ -24,8 +39,8 @@ namespace greedy_algorithms{ * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector digitSeparationReverseOrder( - long long largeNumber) const; + std::vector digitSeparationReverseOrder( + std::int64_t largeNumber) const; /** * @brief Separates the digits of a large positive number into a vector in @@ -34,8 +49,8 @@ namespace greedy_algorithms{ * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector digitSeparationForwardOrder( - long long largeNumber) const; + std::vector digitSeparationForwardOrder( + std::int64_t largeNumber) const; }; /** @@ -44,9 +59,9 @@ namespace greedy_algorithms{ * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector DigitSeparation::digitSeparationReverseOrder( - long long largeNumber) const { - std::vector result; + std::vector DigitSeparation::digitSeparationReverseOrder( + std::int64_t largeNumber) const { + std::vector result; if (largeNumber != 0) { while (largeNumber != 0) { result.push_back(std::abs(largeNumber % 10)); @@ -64,9 +79,9 @@ namespace greedy_algorithms{ * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector DigitSeparation::digitSeparationForwardOrder( - long long largeNumber) const { - std::vector result = digitSeparationReverseOrder(largeNumber); + std::vector DigitSeparation::digitSeparationForwardOrder( + std::int64_t largeNumber) const { + std::vector result = digitSeparationReverseOrder(largeNumber); std::reverse(result.begin(), result.end()); return result; } @@ -76,16 +91,16 @@ int main() { greedy_algorithms::DigitSeparation ds; // Test case: Positive number - long long number = 1234567890; - std::vector expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; - std::vector expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + std::int64_t number = 1234567890; + std::vector expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + std::vector expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // Testing reverse order - std::vector reverseOrder = ds.digitSeparationReverseOrder(number); + std::vector reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); // Testing forward order - std::vector forwardOrder = ds.digitSeparationForwardOrder(number); + std::vector forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); // Test case: Single digit number From 57f61a6760d4105126504bad685879456839e47d Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 16:21:49 +0500 Subject: [PATCH 04/35] feat: Made test function and removed extra whitespaces --- greedy_algorithms/digit_separation.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 3d2476b0350..1b091d8c7c3 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -4,7 +4,6 @@ * https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html * @details * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) - * @see related_file.cpp, another_file.cpp */ #include /// For reveresing the vector @@ -87,19 +86,15 @@ namespace greedy_algorithms{ } } -int main() { +void tests(){ greedy_algorithms::DigitSeparation ds; // Test case: Positive number std::int64_t number = 1234567890; std::vector expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; std::vector expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; - - // Testing reverse order std::vector reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - - // Testing forward order std::vector forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); @@ -107,10 +102,8 @@ int main() { number = 5; expectedReverse = {5}; expectedForward = {5}; - reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); @@ -118,10 +111,8 @@ int main() { number = 0; expectedReverse = {0}; expectedForward = {0}; - reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); @@ -129,10 +120,8 @@ int main() { number = 987654321012345; expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; - reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); @@ -140,8 +129,14 @@ int main() { number = -987654321012345; expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; + std::vector reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); + std::vector forwardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); +} - std::cout << "All tests passed!" << std::endl; +int main() { + tests(); return 0; } From 9c0034c50f33dfe57a58d9743b8fbffe30079a5e Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 16:24:01 +0500 Subject: [PATCH 05/35] removed some more whitespaces --- greedy_algorithms/digit_separation.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 1b091d8c7c3..e828ba272c7 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -13,7 +13,6 @@ #include /// For assert() function to check for errors #include /// For int64_t data type to handle large numbers - /** * @namespace * @brief Greedy Algorithms From cba0eb960322febf87eb72df2890c5ed314d1a83 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:38:55 +0500 Subject: [PATCH 06/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index e828ba272c7..b8df1a47161 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -85,6 +85,9 @@ namespace greedy_algorithms{ } } +/** + * @brief main function + * @return 0 on sucessful exit void tests(){ greedy_algorithms::DigitSeparation ds; From d3edb52ebe54345c3d94d93666671dd0e259b557 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Sat, 5 Oct 2024 17:09:48 +0530 Subject: [PATCH 07/35] fix: terminate multiline comment --- greedy_algorithms/digit_separation.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index b8df1a47161..e2eb09469a4 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -88,6 +88,7 @@ namespace greedy_algorithms{ /** * @brief main function * @return 0 on sucessful exit + */ void tests(){ greedy_algorithms::DigitSeparation ds; From 1fb86dbe99ce5fd785f088d2bbd99b2e2a38968d Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:41:43 +0500 Subject: [PATCH 08/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index e2eb09469a4..0ed2c8ca959 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -138,6 +138,8 @@ void tests(){ assert(forwardOrder == expectedForward); } + reverseOrder = ds.digitSeparationReverseOrder(number); + assert(reverseOrder == expectedReverse); int main() { tests(); From ad034743eab41a715e622adef282e116f115a676 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:43:28 +0500 Subject: [PATCH 09/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 0ed2c8ca959..8e845aa079d 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -134,8 +134,6 @@ void tests(){ expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; std::vector reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - std::vector forwardOrder = ds.digitSeparationForwardOrder(number); - assert(forwardOrder == expectedForward); } reverseOrder = ds.digitSeparationReverseOrder(number); From 5f392b9960ee4c06227a4d0d9ce23cefa9874d72 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:43:43 +0500 Subject: [PATCH 10/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 8e845aa079d..e9e0969b996 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -139,7 +139,7 @@ void tests(){ reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); int main() { - tests(); + tests(); // run self test implementation return 0; } From 373e1df6019d99b516bd09871e1a1129492d426e Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:44:09 +0500 Subject: [PATCH 11/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index e9e0969b996..7f307e99627 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -138,6 +138,10 @@ void tests(){ reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); +/** + * @brief main function + * @return 0 on successful exit + */ int main() { tests(); // run self test implementation From af077d24948bfa63133fa063f6fe14d99ab87220 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 5 Oct 2024 16:46:15 +0500 Subject: [PATCH 12/35] Corrected test function --- greedy_algorithms/digit_separation.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 7f307e99627..23bb1ecbbe9 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -134,10 +134,11 @@ void tests(){ expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; std::vector reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); -} - reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); +} + + /** * @brief main function * @return 0 on successful exit From 18b65460e0ded7b1994b10a5baec77cebf9709cb Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:49:24 +0500 Subject: [PATCH 13/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 23bb1ecbbe9..7a195b5a35f 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -86,8 +86,8 @@ namespace greedy_algorithms{ } /** - * @brief main function - * @return 0 on sucessful exit + * @brief self test implementation + * @return void */ void tests(){ greedy_algorithms::DigitSeparation ds; From 87b955b831734996a58f7aa6c648b4eff42fb962 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 16:49:36 +0500 Subject: [PATCH 14/35] Update greedy_algorithms/digit_separation.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 7a195b5a35f..55a82293923 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -89,7 +89,7 @@ namespace greedy_algorithms{ * @brief self test implementation * @return void */ -void tests(){ +static void tests(){ greedy_algorithms::DigitSeparation ds; // Test case: Positive number From a585834c95244d17bfa6604544c89ce03869cb8e Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 17:22:04 +0500 Subject: [PATCH 15/35] remove redundant declaration Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 55a82293923..b44fbc2864a 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -132,7 +132,7 @@ static void tests(){ number = -987654321012345; expectedReverse = {5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; - std::vector reverseOrder = ds.digitSeparationReverseOrder(number); + reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); From ef954a8673c826d7b8ff7dcf343add5111817846 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 5 Oct 2024 17:22:37 +0500 Subject: [PATCH 16/35] Corrected tests Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index b44fbc2864a..bc913782d63 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -134,8 +134,8 @@ static void tests(){ expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - reverseOrder = ds.digitSeparationReverseOrder(number); - assert(reverseOrder == expectedReverse); + fowardOrder = ds.digitSeparationForwardOrder(number); + assert(forwardOrder == expectedForward); } From 14274747bd2dc9c02eabdb479ee4f48582952c0e Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 07:31:37 +0500 Subject: [PATCH 17/35] file clang linted --- greedy_algorithms/digit_separation.cpp | 122 +++++++++++++------------ 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index bc913782d63..7fc56aa8908 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -1,104 +1,107 @@ /** * @file digit_separation.cpp - * @brief This file contains the DigitSeparation class for separating digits of large numbers. + * @brief This file contains the DigitSeparation class for separating digits of + * large numbers. * https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html * @details * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) */ -#include /// For reveresing the vector -#include /// For abs() function -#include /// For input/output operations -#include /// For std::vector to store separated digits -#include /// For assert() function to check for errors -#include /// For int64_t data type to handle large numbers +#include /// For reveresing the vector +#include /// For assert() function to check for errors +#include /// For abs() function +#include /// For int64_t data type to handle large numbers +#include /// For input/output operations +#include /// For std::vector to store separated digits /** * @namespace * @brief Greedy Algorithms */ -namespace greedy_algorithms{ +namespace greedy_algorithms { /** * @brief A class that provides methods to separate the digits of a large * positive number. */ - class DigitSeparation { - public: - /** - * @brief Default constructor for the DigitSeparation class. - */ - DigitSeparation() {} - - /** - * @brief Separates the digits of a large positive number into a vector in - * reverse order. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in reverse order. - */ - std::vector digitSeparationReverseOrder( - std::int64_t largeNumber) const; - - /** - * @brief Separates the digits of a large positive number into a vector in - * forward order. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in forward order. - */ - std::vector digitSeparationForwardOrder( - std::int64_t largeNumber) const; - }; +class DigitSeparation { + public: + /** + * @brief Default constructor for the DigitSeparation class. + */ + DigitSeparation() {} /** - * @brief Implementation of digitSeparationReverseOrder method. + * @brief Separates the digits of a large positive number into a vector in + * reverse order. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector DigitSeparation::digitSeparationReverseOrder( - std::int64_t largeNumber) const { - std::vector result; - if (largeNumber != 0) { - while (largeNumber != 0) { - result.push_back(std::abs(largeNumber % 10)); - largeNumber /= 10; - } - } else { - result.push_back(0); - } - return result; - } + std::vector digitSeparationReverseOrder( + std::int64_t largeNumber) const; /** - * @brief Implementation of digitSeparationForwardOrder method. + * @brief Separates the digits of a large positive number into a vector in + * forward order. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector DigitSeparation::digitSeparationForwardOrder( - std::int64_t largeNumber) const { - std::vector result = digitSeparationReverseOrder(largeNumber); - std::reverse(result.begin(), result.end()); - return result; + std::vector digitSeparationForwardOrder( + std::int64_t largeNumber) const; +}; + +/** + * @brief Implementation of digitSeparationReverseOrder method. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in reverse order. + */ +std::vector DigitSeparation::digitSeparationReverseOrder( + std::int64_t largeNumber) const { + std::vector result; + if (largeNumber != 0) { + while (largeNumber != 0) { + result.push_back(std::abs(largeNumber % 10)); + largeNumber /= 10; + } + } else { + result.push_back(0); } + return result; +} + +/** + * @brief Implementation of digitSeparationForwardOrder method. + * + * @param largeNumber The large number to separate digits from. + * @return A vector of digits in forward order. + */ +std::vector DigitSeparation::digitSeparationForwardOrder( + std::int64_t largeNumber) const { + std::vector result = digitSeparationReverseOrder(largeNumber); + std::reverse(result.begin(), result.end()); + return result; } +} // namespace greedy_algorithms /** * @brief self test implementation * @return void */ -static void tests(){ +static void tests() { greedy_algorithms::DigitSeparation ds; // Test case: Positive number std::int64_t number = 1234567890; std::vector expectedReverse = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; std::vector expectedForward = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; - std::vector reverseOrder = ds.digitSeparationReverseOrder(number); + std::vector reverseOrder = + ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - std::vector forwardOrder = ds.digitSeparationForwardOrder(number); + std::vector forwardOrder = + ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); // Test case: Single digit number @@ -138,13 +141,12 @@ static void tests(){ assert(forwardOrder == expectedForward); } - /** * @brief main function * @return 0 on successful exit */ int main() { - tests(); // run self test implementation + tests(); // run self test implementation return 0; } From 3f0489232d514dfd3bd0803f89e8abc642bfe175 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 07:53:29 +0500 Subject: [PATCH 18/35] "Moved method implementations from outside to inside class definition" --- greedy_algorithms/digit_separation.cpp | 62 +++++++++----------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 7fc56aa8908..6aa73f87eb6 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -32,58 +32,40 @@ class DigitSeparation { DigitSeparation() {} /** - * @brief Separates the digits of a large positive number into a vector in - * reverse order. + * @brief Implementation of digitSeparationReverseOrder method. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector digitSeparationReverseOrder( - std::int64_t largeNumber) const; + std::vector DigitSeparation::digitSeparationReverseOrder( + std::int64_t largeNumber) const { + std::vector result; + if (largeNumber != 0) { + while (largeNumber != 0) { + result.push_back(std::abs(largeNumber % 10)); + largeNumber /= 10; + } + } else { + result.push_back(0); + } + return result; + } /** - * @brief Separates the digits of a large positive number into a vector in - * forward order. + * @brief Implementation of digitSeparationForwardOrder method. * * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector digitSeparationForwardOrder( - std::int64_t largeNumber) const; -}; - -/** - * @brief Implementation of digitSeparationReverseOrder method. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in reverse order. - */ -std::vector DigitSeparation::digitSeparationReverseOrder( - std::int64_t largeNumber) const { - std::vector result; - if (largeNumber != 0) { - while (largeNumber != 0) { - result.push_back(std::abs(largeNumber % 10)); - largeNumber /= 10; - } - } else { - result.push_back(0); + std::vector DigitSeparation::digitSeparationForwardOrder( + std::int64_t largeNumber) const { + std::vector result = + digitSeparationReverseOrder(largeNumber); + std::reverse(result.begin(), result.end()); + return result; } - return result; -} +}; -/** - * @brief Implementation of digitSeparationForwardOrder method. - * - * @param largeNumber The large number to separate digits from. - * @return A vector of digits in forward order. - */ -std::vector DigitSeparation::digitSeparationForwardOrder( - std::int64_t largeNumber) const { - std::vector result = digitSeparationReverseOrder(largeNumber); - std::reverse(result.begin(), result.end()); - return result; -} } // namespace greedy_algorithms /** From ba5688080645088482b5002ab07d9dcd2a4d9fb9 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sun, 6 Oct 2024 08:02:08 +0500 Subject: [PATCH 19/35] fowardOrder to forwardOrder on line 122 Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 6aa73f87eb6..37fa6a9f577 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -119,7 +119,7 @@ static void tests() { expectedForward = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5}; reverseOrder = ds.digitSeparationReverseOrder(number); assert(reverseOrder == expectedReverse); - fowardOrder = ds.digitSeparationForwardOrder(number); + forwardOrder = ds.digitSeparationForwardOrder(number); assert(forwardOrder == expectedForward); } From 40ac10334dcf69837b035fba930003b4a1382af3 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sun, 6 Oct 2024 08:03:14 +0500 Subject: [PATCH 20/35] Removed Class scope resolution form digitSeparationReverseOrder function Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 37fa6a9f577..ac59294f6f5 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -37,7 +37,7 @@ class DigitSeparation { * @param largeNumber The large number to separate digits from. * @return A vector of digits in reverse order. */ - std::vector DigitSeparation::digitSeparationReverseOrder( + std::vector digitSeparationReverseOrder( std::int64_t largeNumber) const { std::vector result; if (largeNumber != 0) { From 12d8d53bd6b46ec2e55895783dac902949505af0 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sun, 6 Oct 2024 08:03:36 +0500 Subject: [PATCH 21/35] Removed Class scope resolution form digitSeparationForwardOrderfunction Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/digit_separation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index ac59294f6f5..c8e44652fbd 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -57,7 +57,7 @@ class DigitSeparation { * @param largeNumber The large number to separate digits from. * @return A vector of digits in forward order. */ - std::vector DigitSeparation::digitSeparationForwardOrder( + std::vector digitSeparationForwardOrder( std::int64_t largeNumber) const { std::vector result = digitSeparationReverseOrder(largeNumber); From 28f690187ac85bfdd9b790fbde4ded99c8edcbd0 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sun, 6 Oct 2024 08:22:44 +0500 Subject: [PATCH 22/35] docs: documentation updated --- greedy_algorithms/digit_separation.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/greedy_algorithms/digit_separation.cpp b/greedy_algorithms/digit_separation.cpp index 6aa73f87eb6..03a0719a0d2 100644 --- a/greedy_algorithms/digit_separation.cpp +++ b/greedy_algorithms/digit_separation.cpp @@ -1,9 +1,17 @@ /** * @file digit_separation.cpp - * @brief This file contains the DigitSeparation class for separating digits of - * large numbers. - * https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html - * @details + * @brief Separates digits from numbers in forward and reverse order + * @see https://www.log2base2.com/c-examples/loop/split-a-number-into-digits-in-c.html + * @details The DigitSeparation class provides two methods to separate the + * digits of large integers: digitSeparationReverseOrder and + * digitSeparationForwardOrder. The digitSeparationReverseOrder method extracts + * digits by repeatedly applying the modulus operation (% 10) to isolate the + * last digit, then divides the number by 10 to remove it. This process + * continues until the entire number is broken down into its digits, which are + * stored in reverse order. If the number is zero, the method directly returns a + * vector containing {0} to handle this edge case. Negative numbers are handled + * by taking the absolute value, ensuring consistent behavior regardless of the + * sign. * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) */ From 9772468f6e5ff3104094fd2e5f3381e06ce2aa89 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Mon, 7 Oct 2024 19:01:23 +0500 Subject: [PATCH 23/35] initial commit of Binary Addition --- greedy_algorithms/binary_addition.cpp | 92 +++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 greedy_algorithms/binary_addition.cpp diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp new file mode 100644 index 00000000000..6b166119c9b --- /dev/null +++ b/greedy_algorithms/binary_addition.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +/** + * A class to perform binary addition of two binary strings. + */ +class Solution { +public: + /** + * Adds two binary strings and returns the result as a binary string. + * + * @param a The first binary string. + * @param b The second binary string. + * @return The sum of the two binary strings as a binary string. + */ + std::string addBinary(const std::string& a, const std::string& b) { + std::string result; + int carry = 0; + int maxLength = std::max(a.size(), b.size()); + + // Traverse both strings from the end to the beginning + for (int i = 0; i < maxLength; ++i) { + // Get the current bits from both strings, if available + int bitA = (i < a.size()) ? (a[a.size() - 1 - i] - '0') : 0; + int bitB = (i < b.size()) ? (b[b.size() - 1 - i] - '0') : 0; + + // Calculate the sum of bits and carry + int sum = bitA + bitB + carry; + carry = sum / 2; // Determine the carry for the next bit + result.push_back((sum % 2) + '0'); // Append the sum's current bit to result + } + + // If there's still a carry left, append it + if (carry) { + result.push_back('1'); + } + + // The result is built in reverse order, so reverse it before returning + std::reverse(result.begin(), result.end()); + return result; + } +}; + +/** + * Function to run tests for the addBinary method. + */ +void runTests() { + Solution solution; + + // Test case for two binary strings of equal length without any carry over. + assert(solution.addBinary("1010", "1101") == "10111"); + + // Test case for two binary strings of equal length with a carry over. + assert(solution.addBinary("1111", "1111") == "11110"); + + // Test case for two binary strings where one is longer than the other. + assert(solution.addBinary("101", "11") == "1000"); + + // Test case for a binary string of all zeros. + assert(solution.addBinary("0", "0") == "0"); + + // Test case where both binary strings consist of all ones. + assert(solution.addBinary("1111", "1111") == "11110"); + + // Test case where one binary string is zero and the other is non-zero. + assert(solution.addBinary("0", "10101") == "10101"); + assert(solution.addBinary("10101", "0") == "10101"); + + // Test case for large binary numbers with many digits. + assert(solution.addBinary("101010101010101010101010101010", "110110110110110110110110110110") == "1100001100001100001100001100000"); + + // Test case where one binary string is much longer than the other. + assert(solution.addBinary("1", "11111111") == "100000000"); + + // Test case for adding empty strings (edge case). + assert(solution.addBinary("", "") == "0"); + + // Test case where both binary strings consist of alternating ones and zeros. + assert(solution.addBinary("10101010", "01010101") == "11111111"); + + std::cout << "All tests passed!" << std::endl; +} + +/** + * Main function to execute the program. + */ +int main() { + runTests(); + return 0; +} From 846118425b7e555886c85b97a4532c2c1e09ad2b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Mon, 7 Oct 2024 19:06:42 +0500 Subject: [PATCH 24/35] "Formatting changes and whitespace adjustments in binary_addition.cpp" --- greedy_algorithms/binary_addition.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 6b166119c9b..2a9e07ee899 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -1,13 +1,13 @@ -#include -#include #include #include +#include +#include /** * A class to perform binary addition of two binary strings. */ class Solution { -public: + public: /** * Adds two binary strings and returns the result as a binary string. * @@ -28,8 +28,9 @@ class Solution { // Calculate the sum of bits and carry int sum = bitA + bitB + carry; - carry = sum / 2; // Determine the carry for the next bit - result.push_back((sum % 2) + '0'); // Append the sum's current bit to result + carry = sum / 2; // Determine the carry for the next bit + result.push_back((sum % 2) + + '0'); // Append the sum's current bit to result } // If there's still a carry left, append it @@ -69,15 +70,18 @@ void runTests() { assert(solution.addBinary("10101", "0") == "10101"); // Test case for large binary numbers with many digits. - assert(solution.addBinary("101010101010101010101010101010", "110110110110110110110110110110") == "1100001100001100001100001100000"); + assert(solution.addBinary("101010101010101010101010101010", + "110110110110110110110110110110") == + "1100001100001100001100001100000"); // Test case where one binary string is much longer than the other. assert(solution.addBinary("1", "11111111") == "100000000"); // Test case for adding empty strings (edge case). - assert(solution.addBinary("", "") == "0"); + assert(solution.addBinary("", "") == ""); - // Test case where both binary strings consist of alternating ones and zeros. + // Test case where both binary strings consist of alternating ones and + // zeros. assert(solution.addBinary("10101010", "01010101") == "11111111"); std::cout << "All tests passed!" << std::endl; From c846d8b270d7c4d121db108bc4b55f86ec340123 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Mon, 7 Oct 2024 21:16:17 +0500 Subject: [PATCH 25/35] Documentation added --- greedy_algorithms/binary_addition.cpp | 49 +++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 2a9e07ee899..a75ab2aea8e 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -1,12 +1,32 @@ +/** + * @file binary_addition.cpp + * @brief Adds two binary numbers and outputs resulting string + * @see https://www.geeksforgeeks.org/cpp-program-to-add-two-binary-strings/ + * @details The algorithm for adding two binary strings works by processing them + * from right to left, similar to manual addition. It starts by determining the + * longer string's length to ensure both strings are fully traversed. For each + * pair of corresponding bits and any carry from the previous addition, it + * calculates the sum. If the sum exceeds 1, a carry is generated for the next + * bit. The results for each bit are collected in a result string, which is + * reversed at the end to present the final binary sum correctly. This method + * efficiently handles varying string lengths and carrying during addition. + * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) + */ + #include #include #include #include +/** + * @namespace + * @brief Greedy Algorithms + */ +namespace greedy_algorithms { /** * A class to perform binary addition of two binary strings. */ -class Solution { +class BinaryAddition { public: /** * Adds two binary strings and returns the result as a binary string. @@ -43,46 +63,47 @@ class Solution { return result; } }; +} // namespace greedy_algorithms /** * Function to run tests for the addBinary method. */ void runTests() { - Solution solution; + BinaryAddition binaryAddition; // Test case for two binary strings of equal length without any carry over. - assert(solution.addBinary("1010", "1101") == "10111"); + assert(binaryAddition.addBinary("1010", "1101") == "10111"); // Test case for two binary strings of equal length with a carry over. - assert(solution.addBinary("1111", "1111") == "11110"); + assert(binaryAddition.addBinary("1111", "1111") == "11110"); // Test case for two binary strings where one is longer than the other. - assert(solution.addBinary("101", "11") == "1000"); + assert(binaryAddition.addBinary("101", "11") == "1000"); // Test case for a binary string of all zeros. - assert(solution.addBinary("0", "0") == "0"); + assert(binaryAddition.addBinary("0", "0") == "0"); // Test case where both binary strings consist of all ones. - assert(solution.addBinary("1111", "1111") == "11110"); + assert(binaryAddition.addBinary("1111", "1111") == "11110"); // Test case where one binary string is zero and the other is non-zero. - assert(solution.addBinary("0", "10101") == "10101"); - assert(solution.addBinary("10101", "0") == "10101"); + assert(binaryAddition.addBinary("0", "10101") == "10101"); + assert(binaryAddition.addBinary("10101", "0") == "10101"); // Test case for large binary numbers with many digits. - assert(solution.addBinary("101010101010101010101010101010", - "110110110110110110110110110110") == + assert(binaryAddition.addBinary("101010101010101010101010101010", + "110110110110110110110110110110") == "1100001100001100001100001100000"); // Test case where one binary string is much longer than the other. - assert(solution.addBinary("1", "11111111") == "100000000"); + assert(binaryAddition.addBinary("1", "11111111") == "100000000"); // Test case for adding empty strings (edge case). - assert(solution.addBinary("", "") == ""); + assert(binaryAddition.addBinary("", "") == ""); // Test case where both binary strings consist of alternating ones and // zeros. - assert(solution.addBinary("10101010", "01010101") == "11111111"); + assert(binaryAddition.addBinary("10101010", "01010101") == "11111111"); std::cout << "All tests passed!" << std::endl; } From 47685a5d88670ad2c1d199a01327ff64870f3b3c Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 9 Oct 2024 19:07:31 +0500 Subject: [PATCH 26/35] fix: removed unnecessary whitespaces --- greedy_algorithms/binary_addition.cpp | 38 +++++---------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index a75ab2aea8e..7d3456259ad 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -13,10 +13,10 @@ * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) */ -#include -#include -#include -#include +#include /// for reverse function +#include /// for tests +#include /// for input and outputs +#include /// for sting class /** * @namespace @@ -52,13 +52,9 @@ class BinaryAddition { result.push_back((sum % 2) + '0'); // Append the sum's current bit to result } - - // If there's still a carry left, append it if (carry) { result.push_back('1'); } - - // The result is built in reverse order, so reverse it before returning std::reverse(result.begin(), result.end()); return result; } @@ -68,50 +64,28 @@ class BinaryAddition { /** * Function to run tests for the addBinary method. */ -void runTests() { +void tests() { BinaryAddition binaryAddition; - // Test case for two binary strings of equal length without any carry over. assert(binaryAddition.addBinary("1010", "1101") == "10111"); - - // Test case for two binary strings of equal length with a carry over. assert(binaryAddition.addBinary("1111", "1111") == "11110"); - - // Test case for two binary strings where one is longer than the other. assert(binaryAddition.addBinary("101", "11") == "1000"); - - // Test case for a binary string of all zeros. assert(binaryAddition.addBinary("0", "0") == "0"); - - // Test case where both binary strings consist of all ones. assert(binaryAddition.addBinary("1111", "1111") == "11110"); - - // Test case where one binary string is zero and the other is non-zero. assert(binaryAddition.addBinary("0", "10101") == "10101"); assert(binaryAddition.addBinary("10101", "0") == "10101"); - - // Test case for large binary numbers with many digits. assert(binaryAddition.addBinary("101010101010101010101010101010", "110110110110110110110110110110") == "1100001100001100001100001100000"); - - // Test case where one binary string is much longer than the other. assert(binaryAddition.addBinary("1", "11111111") == "100000000"); - - // Test case for adding empty strings (edge case). assert(binaryAddition.addBinary("", "") == ""); - - // Test case where both binary strings consist of alternating ones and - // zeros. assert(binaryAddition.addBinary("10101010", "01010101") == "11111111"); - - std::cout << "All tests passed!" << std::endl; } /** * Main function to execute the program. */ int main() { - runTests(); + tests(); return 0; } From e62242eac0f683d12bbf26d997aa9a1cafb46d28 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 10 Oct 2024 17:35:51 +0500 Subject: [PATCH 27/35] fix: Fixed the case of non binary strings --- greedy_algorithms/binary_addition.cpp | 40 +++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 7d3456259ad..3e6b34f1429 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -8,15 +8,17 @@ * pair of corresponding bits and any carry from the previous addition, it * calculates the sum. If the sum exceeds 1, a carry is generated for the next * bit. The results for each bit are collected in a result string, which is - * reversed at the end to present the final binary sum correctly. This method - * efficiently handles varying string lengths and carrying during addition. + * reversed at the end to present the final binary sum correctly. Additionally, + * the function validates the input to ensure that only valid binary strings + * (containing only '0' and '1') are processed. If invalid input is detected, + * it returns an empty string. * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) */ #include /// for reverse function #include /// for tests #include /// for input and outputs -#include /// for sting class +#include /// for string class /** * @namespace @@ -33,9 +35,14 @@ class BinaryAddition { * * @param a The first binary string. * @param b The second binary string. - * @return The sum of the two binary strings as a binary string. + * @return The sum of the two binary strings as a binary string, or an empty string + * if either input string contains non-binary characters. */ std::string addBinary(const std::string& a, const std::string& b) { + if (!isValidBinaryString(a) || !isValidBinaryString(b)) { + return ""; // Return empty string if input contains non-binary characters + } + std::string result; int carry = 0; int maxLength = std::max(a.size(), b.size()); @@ -58,6 +65,18 @@ class BinaryAddition { std::reverse(result.begin(), result.end()); return result; } + + private: + /** + * Validates whether a string contains only binary characters (0 or 1). + * @param str The string to validate. + * @return true if the string is binary, false otherwise. + */ + bool isValidBinaryString(const std::string& str) const { + return std::all_of(str.begin(), str.end(), [](char c) { + return c == '0' || c == '1'; + }); + } }; } // namespace greedy_algorithms @@ -65,8 +84,9 @@ class BinaryAddition { * Function to run tests for the addBinary method. */ void tests() { - BinaryAddition binaryAddition; + greedy_algorithms::BinaryAddition binaryAddition; + // Valid binary string tests assert(binaryAddition.addBinary("1010", "1101") == "10111"); assert(binaryAddition.addBinary("1111", "1111") == "11110"); assert(binaryAddition.addBinary("101", "11") == "1000"); @@ -78,8 +98,15 @@ void tests() { "110110110110110110110110110110") == "1100001100001100001100001100000"); assert(binaryAddition.addBinary("1", "11111111") == "100000000"); - assert(binaryAddition.addBinary("", "") == ""); assert(binaryAddition.addBinary("10101010", "01010101") == "11111111"); + + // Invalid binary string tests (should return empty string) + assert(binaryAddition.addBinary("10102", "1101") == ""); + assert(binaryAddition.addBinary("ABC", "1101") == ""); + assert(binaryAddition.addBinary("1010", "1102") == ""); + assert(binaryAddition.addBinary("111", "1x1") == ""); + assert(binaryAddition.addBinary("1x1", "111") == ""); + assert(binaryAddition.addBinary("1234", "1101") == ""); } /** @@ -87,5 +114,6 @@ void tests() { */ int main() { tests(); + std::cout << "All tests passed.\n"; return 0; } From b172e058f6d357bf26cb832df6c33b7577d4ab16 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 10 Oct 2024 17:38:41 +0500 Subject: [PATCH 28/35] clang linted --- greedy_algorithms/binary_addition.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 3e6b34f1429..0155e147bc6 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -9,8 +9,8 @@ * calculates the sum. If the sum exceeds 1, a carry is generated for the next * bit. The results for each bit are collected in a result string, which is * reversed at the end to present the final binary sum correctly. Additionally, - * the function validates the input to ensure that only valid binary strings - * (containing only '0' and '1') are processed. If invalid input is detected, + * the function validates the input to ensure that only valid binary strings + * (containing only '0' and '1') are processed. If invalid input is detected, * it returns an empty string. * @author [Muhammad Junaid Khalid](https://github.com/mjk22071998) */ @@ -35,12 +35,13 @@ class BinaryAddition { * * @param a The first binary string. * @param b The second binary string. - * @return The sum of the two binary strings as a binary string, or an empty string - * if either input string contains non-binary characters. + * @return The sum of the two binary strings as a binary string, or an empty + * string if either input string contains non-binary characters. */ std::string addBinary(const std::string& a, const std::string& b) { if (!isValidBinaryString(a) || !isValidBinaryString(b)) { - return ""; // Return empty string if input contains non-binary characters + return ""; // Return empty string if input contains non-binary + // characters } std::string result; @@ -73,9 +74,8 @@ class BinaryAddition { * @return true if the string is binary, false otherwise. */ bool isValidBinaryString(const std::string& str) const { - return std::all_of(str.begin(), str.end(), [](char c) { - return c == '0' || c == '1'; - }); + return std::all_of(str.begin(), str.end(), + [](char c) { return c == '0' || c == '1'; }); } }; } // namespace greedy_algorithms From aece5a24b37a89662d171a2c05b0584daa50b3fc Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 12 Oct 2024 15:04:36 +0500 Subject: [PATCH 29/35] docs: Documentation updated --- greedy_algorithms/binary_addition.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 0155e147bc6..7aa3429ab97 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -26,13 +26,12 @@ */ namespace greedy_algorithms { /** - * A class to perform binary addition of two binary strings. + * @brief A class to perform binary addition of two binary strings. */ class BinaryAddition { public: /** - * Adds two binary strings and returns the result as a binary string. - * + * @brief Adds two binary strings and returns the result as a binary string. * @param a The first binary string. * @param b The second binary string. * @return The sum of the two binary strings as a binary string, or an empty @@ -69,7 +68,7 @@ class BinaryAddition { private: /** - * Validates whether a string contains only binary characters (0 or 1). + * @brief Validates whether a string contains only binary characters (0 or 1). * @param str The string to validate. * @return true if the string is binary, false otherwise. */ @@ -81,7 +80,7 @@ class BinaryAddition { } // namespace greedy_algorithms /** - * Function to run tests for the addBinary method. + * @brief Function to run tests for the addBinary method. */ void tests() { greedy_algorithms::BinaryAddition binaryAddition; @@ -110,10 +109,9 @@ void tests() { } /** - * Main function to execute the program. + * @brief main function */ int main() { - tests(); - std::cout << "All tests passed.\n"; + tests(); /// To execute tests return 0; } From 592c0d90052a2d02819e37ecce08f6abe4457571 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Sat, 12 Oct 2024 15:05:59 +0500 Subject: [PATCH 30/35] Comments updated --- greedy_algorithms/binary_addition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 7aa3429ab97..0d3405c1678 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -80,7 +80,7 @@ class BinaryAddition { } // namespace greedy_algorithms /** - * @brief Function to run tests for the addBinary method. + * @brief run self test implementation. */ void tests() { greedy_algorithms::BinaryAddition binaryAddition; From 12d0b2970a545839b068cfe14d6eba03b6db85cd Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Sat, 19 Oct 2024 16:08:41 +0500 Subject: [PATCH 31/35] Removed link Removed @see link in header documentation Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/binary_addition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 0d3405c1678..f4cbe0c56a5 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -1,7 +1,7 @@ /** * @file binary_addition.cpp * @brief Adds two binary numbers and outputs resulting string - * @see https://www.geeksforgeeks.org/cpp-program-to-add-two-binary-strings/ + * * @details The algorithm for adding two binary strings works by processing them * from right to left, similar to manual addition. It starts by determining the * longer string's length to ensure both strings are fully traversed. For each From e0fc3d85e1e634616f2d82015cfe4b9c6a105626 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 19 Oct 2024 11:09:19 +0000 Subject: [PATCH 32/35] clang-format and clang-tidy fixes for cd8a5953 --- ...cpp => longest_increasing_subsequence_using_binary_search.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename search/{Longest_Increasing_Subsequence_using_binary_search.cpp => longest_increasing_subsequence_using_binary_search.cpp} (100%) diff --git a/search/Longest_Increasing_Subsequence_using_binary_search.cpp b/search/longest_increasing_subsequence_using_binary_search.cpp similarity index 100% rename from search/Longest_Increasing_Subsequence_using_binary_search.cpp rename to search/longest_increasing_subsequence_using_binary_search.cpp From 916ead7fa365cf4972f18d051ff09d0b33cc47e9 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Tue, 22 Oct 2024 23:34:54 +0500 Subject: [PATCH 33/35] made `tests()` static Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/binary_addition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index f4cbe0c56a5..233846e2fed 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -82,7 +82,7 @@ class BinaryAddition { /** * @brief run self test implementation. */ -void tests() { +static void tests() { greedy_algorithms::BinaryAddition binaryAddition; // Valid binary string tests From 3f07ca466ca24b83149cca11e192ca848fa3bf82 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Tue, 22 Oct 2024 23:35:36 +0500 Subject: [PATCH 34/35] added @returns in documentation of tests function Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/binary_addition.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index 233846e2fed..bf3089a1ce7 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -81,6 +81,7 @@ class BinaryAddition { /** * @brief run self test implementation. + * @returns void */ static void tests() { greedy_algorithms::BinaryAddition binaryAddition; From d039369aedc241fe6cb0bef562b6fdd25fc8b791 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Tue, 22 Oct 2024 23:36:12 +0500 Subject: [PATCH 35/35] added @return in documentation of main function Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- greedy_algorithms/binary_addition.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_algorithms/binary_addition.cpp b/greedy_algorithms/binary_addition.cpp index bf3089a1ce7..4a365323815 100644 --- a/greedy_algorithms/binary_addition.cpp +++ b/greedy_algorithms/binary_addition.cpp @@ -111,6 +111,7 @@ static void tests() { /** * @brief main function + * @returns 0 on successful exit */ int main() { tests(); /// To execute tests