Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Binary Addition Algorithm #2802

Merged
merged 49 commits into from
Oct 24, 2024
Merged
Changes from 42 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
47af70e
Digit Separation Algorithm added
mjk22071998 Oct 5, 2024
48f42a7
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 5, 2024
011c4e2
feat: Added namespace greedy_algoriithms
mjk22071998 Oct 5, 2024
76edb15
"Updated digit separation code: added comments and docstrings, change…
mjk22071998 Oct 5, 2024
57f61a6
feat: Made test function and removed extra whitespaces
mjk22071998 Oct 5, 2024
9c0034c
removed some more whitespaces
mjk22071998 Oct 5, 2024
cba0eb9
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
d3edb52
fix: terminate multiline comment
realstealthninja Oct 5, 2024
1fb86db
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
ad03474
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
5f392b9
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
373e1df
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
af077d2
Corrected test function
mjk22071998 Oct 5, 2024
18b6546
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
87b955b
Update greedy_algorithms/digit_separation.cpp
mjk22071998 Oct 5, 2024
a585834
remove redundant declaration
mjk22071998 Oct 5, 2024
ef954a8
Corrected tests
mjk22071998 Oct 5, 2024
1427474
file clang linted
mjk22071998 Oct 6, 2024
a0f488b
Merge branch 'master' into master
mjk22071998 Oct 6, 2024
051448f
Merge branch 'master' into master
mjk22071998 Oct 6, 2024
3f04892
"Moved method implementations from outside to inside class definition"
mjk22071998 Oct 6, 2024
ba56880
fowardOrder to forwardOrder on line 122
mjk22071998 Oct 6, 2024
40ac103
Removed Class scope resolution form digitSeparationReverseOrder function
mjk22071998 Oct 6, 2024
12d8d53
Removed Class scope resolution form digitSeparationForwardOrderfunction
mjk22071998 Oct 6, 2024
28f6901
docs: documentation updated
mjk22071998 Oct 6, 2024
c9cd9a3
Merge branch 'master' of https://github.com/mjk22071998/C-Plus-Plus
mjk22071998 Oct 6, 2024
6c8305b
Merge branch 'master' into master
realstealthninja Oct 7, 2024
cffb85a
Merge branch 'master' into master
realstealthninja Oct 7, 2024
481e5db
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 7, 2024
9772468
initial commit of Binary Addition
mjk22071998 Oct 7, 2024
8461184
"Formatting changes and whitespace adjustments in binary_addition.cpp"
mjk22071998 Oct 7, 2024
c846d8b
Documentation added
mjk22071998 Oct 7, 2024
47685a5
fix: removed unnecessary whitespaces
mjk22071998 Oct 9, 2024
d9113db
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 9, 2024
206758b
Merge branch 'master' into master
mjk22071998 Oct 10, 2024
e62242e
fix: Fixed the case of non binary strings
mjk22071998 Oct 10, 2024
5236bd0
Merge branch 'master' of https://github.com/mjk22071998/C-Plus-Plus
mjk22071998 Oct 10, 2024
b172e05
clang linted
mjk22071998 Oct 10, 2024
19dc886
Merge branch 'master' into master
mjk22071998 Oct 11, 2024
aece5a2
docs: Documentation updated
mjk22071998 Oct 12, 2024
592c0d9
Comments updated
mjk22071998 Oct 12, 2024
074583a
Merge branch 'master' into master
mjk22071998 Oct 16, 2024
12d0b29
Removed link
mjk22071998 Oct 19, 2024
cd8a595
Merge branch 'master' into master
mjk22071998 Oct 19, 2024
e0fc3d8
clang-format and clang-tidy fixes for cd8a5953
github-actions[bot] Oct 19, 2024
916ead7
made `tests()` static
mjk22071998 Oct 22, 2024
3f07ca4
added @returns in documentation of tests function
mjk22071998 Oct 22, 2024
d039369
added @return in documentation of main function
mjk22071998 Oct 22, 2024
b3e829e
Merge branch 'master' into master
mjk22071998 Oct 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions greedy_algorithms/binary_addition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* @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/
mjk22071998 marked this conversation as resolved.
Show resolved Hide resolved
mjk22071998 marked this conversation as resolved.
Show resolved Hide resolved
* @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. 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 <algorithm> /// for reverse function
#include <cassert> /// for tests
#include <iostream> /// for input and outputs
#include <string> /// for string class

/**
* @namespace
* @brief Greedy Algorithms
*/
namespace greedy_algorithms {
/**
* @brief A class to perform binary addition of two binary strings.
*/
class BinaryAddition {
public:
/**
* @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
* 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());

// 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 (carry) {
result.push_back('1');
}
std::reverse(result.begin(), result.end());
return result;
}

private:
/**
* @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.
*/
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

/**
* @brief run self test implementation.
mjk22071998 marked this conversation as resolved.
Show resolved Hide resolved
*/
void tests() {
mjk22071998 marked this conversation as resolved.
Show resolved Hide resolved
greedy_algorithms::BinaryAddition binaryAddition;

// Valid binary string tests
assert(binaryAddition.addBinary("1010", "1101") == "10111");
assert(binaryAddition.addBinary("1111", "1111") == "11110");
realstealthninja marked this conversation as resolved.
Show resolved Hide resolved
assert(binaryAddition.addBinary("101", "11") == "1000");
assert(binaryAddition.addBinary("0", "0") == "0");
assert(binaryAddition.addBinary("1111", "1111") == "11110");
assert(binaryAddition.addBinary("0", "10101") == "10101");
assert(binaryAddition.addBinary("10101", "0") == "10101");
assert(binaryAddition.addBinary("101010101010101010101010101010",
"110110110110110110110110110110") ==
"1100001100001100001100001100000");
assert(binaryAddition.addBinary("1", "11111111") == "100000000");
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") == "");
}

/**
* @brief main function
mjk22071998 marked this conversation as resolved.
Show resolved Hide resolved
*/
int main() {
tests(); /// To execute tests
return 0;
}
Loading