Skip to content

Create add_binary.py#13268

Closed
Sujalgupta2p wants to merge 2 commits intoTheAlgorithms:masterfrom
Sujalgupta2p:patch-8
Closed

Create add_binary.py#13268
Sujalgupta2p wants to merge 2 commits intoTheAlgorithms:masterfrom
Sujalgupta2p:patch-8

Conversation

@Sujalgupta2p
Copy link
Copy Markdown

This program adds two binary numbers given as strings and returns their sum as a binary string. Based on LeetCode Problem 67 — Add Binary.
This program adds two binary numbers given as strings and returns their sum as a binary string.

About

This algorithm solves the problem of adding two binary numbers without using built-in base conversion functions.
It is based on LeetCode Problem 67 — Add Binary.

How it works

  1. Traverse both strings from right to left (least significant bit first).
  2. Add corresponding bits plus the carry.
  3. Append (sum % 2) to the result and update carry = sum // 2.
  4. Reverse the final result to get the correct binary sum.

Example

Reference

https://en.wikipedia.org/wiki/Binary_arithmetic

✅ Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work — I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are lowercase characters with underscores (no spaces or dashes).
  • All functions and variable names follow Python naming conventions (snake_case).
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another explanation.

This program adds two binary numbers given as strings and returns their sum as a binary string.
Based on LeetCode Problem 67 — Add Binary.
@algorithms-keeper algorithms-keeper Bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 6, 2025
Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@@ -0,0 +1,27 @@
def addBinary(a: str, b: str) -> str:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file bit_manipulation/add_binary.py, please provide doctest for the function addBinary

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: addBinary

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant