Skip to content

Add decimal_to_binary.py#4251

Closed
ayushbisht2001 wants to merge 4 commits into
TheAlgorithms:masterfrom
ayushbisht2001:my-contrib-5
Closed

Add decimal_to_binary.py#4251
ayushbisht2001 wants to merge 4 commits into
TheAlgorithms:masterfrom
ayushbisht2001:my-contrib-5

Conversation

@ayushbisht2001

Copy link
Copy Markdown
Contributor

Describe your change:

I have added an algorithm to find the binary string representation of decimal numbers.

  • Add an algorithm?

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. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Mar 2, 2021
@algobytewise

Copy link
Copy Markdown
Contributor

It seems to me that this performs the same function as the built-in function "bin".

@ayushbisht2001

Copy link
Copy Markdown
Contributor Author

It seems to me that this performs the same function as the built-in function "bin".

Exactly,

@@ -0,0 +1,43 @@
def decimal_to_binary(number: int) -> str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see any decimal number in the tests. Please provide tests for numbers like 13.321312312

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Mar 24, 2021
@@ -0,0 +1,43 @@
def decimal_to_binary(number: int) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

number need not be int, it can be float too since you have named your function as decimal_to_binary.
1 and 1.5 are both decimal numbers because 1 can also be written as 1.0 or 0.01 * 10**2.
if you are confused about decimal numbers check this Wikipedia link

if number < 0:
number *= -1
flag = 1
answer = ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please write code that can actually convert decimal to binary, your code just converts int to binary. Even the build-in function bin can do that.

I have written a sample code to do so since I only have view permission, I want you to make some amendments.

a = ""
r = ""
count = 0
d = number - int(number)
number = int(number)
while(d and count<10):
     d = d*2
     r+=str(int(d))
     if int(d)==d:
            break
     else:
            d = d -int(d)
            count+=1
while number:
     a = str(number%2) + a
     number = number >> 1
a = a+"."+r
print(a)

@stale

stale Bot commented Jun 9, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale Used to mark an issue or pull request stale. label Jun 9, 2021
@stale

stale Bot commented Jun 16, 2021

Copy link
Copy Markdown

Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions!

@stale stale Bot closed this Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes A maintainer has requested changes to this PR stale Used to mark an issue or pull request stale.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants