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

Added a hex-bin.py file in conversion.py #4433

Merged
merged 28 commits into from May 20, 2021
Merged

Conversation

@ktsrivastava29
Copy link
Contributor

@ktsrivastava29 ktsrivastava29 commented May 17, 2021

Describe your change:

I made a slight modification in the conversion file, now it is also able to convert hexadecimal to binary

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

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 their comments that points to Wikipedia or other similar explanations.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 left a comment

Sir, I've made changes as per you advised.
Could you approve it now

@ktsrivastava29 ktsrivastava29 requested a review from onlinejudge95 May 17, 2021
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
@onlinejudge95
Copy link
Collaborator

@onlinejudge95 onlinejudge95 commented May 17, 2021

@ktsrivastava29 I see you have not addressed all the requested changes. Sit back and fix all the required changes before asking for a review. Also, feel free to ask for help.

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

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.

conversions/hex-bin.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 left a comment

Sir, worked on your suggestion

  • I've modified the code that catches the exception
  • I removed the lines which you asked to omit
  • I've also improved the doctests

Sir, the only thing I didn't do is I didn't remove the input statement,
sir my rest of the code gets greatly affected if I try to remove the input statement.
Therefore, I've not removed it.
rest of the thing I modified as you suggested.

@onlinejudge95
Copy link
Collaborator

@onlinejudge95 onlinejudge95 commented May 18, 2021

@ktsrivastava29 still you have not addressed all requested changes. Let's start one by one.

1st and foremost I want you to push a change that handles the conversion of a string to hexadecimal no. inside your function.

You need to understand that all your code should be encapsulated within a wrapper function, which in turn can call any other function. Roll out a commit doing just this and nothing else then we will move to other areas of improvement.

@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 18, 2021

Sir, now I've modified it as you advised.

Copy link
Collaborator

@onlinejudge95 onlinejudge95 left a comment

Good work on completing a major change. Now I expect you to go over these review comments and update the code in the same manner. Please ask out for help in this PR if you are stuck somewhere.

conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
conversions/hex-bin.py Outdated Show resolved Hide resolved
@ktsrivastava29 ktsrivastava29 requested a review from onlinejudge95 May 18, 2021
bin_str = num3 + bin_str
num2 = num2 >> 1

if flag:

This comment has been minimized.

@onlinejudge95

onlinejudge95 May 18, 2021
Collaborator

One last change, maybe we can make this if, else clause more pythonic.
Something like

return x if condition else y
import math

def convert(num: str) -> str:

This comment has been minimized.

@onlinejudge95

onlinejudge95 May 18, 2021
Collaborator

Also remove this whitespace

@onlinejudge95
Copy link
Collaborator

@onlinejudge95 onlinejudge95 commented May 18, 2021

@cclauss I am unable to push to his branch for correcting some changes related to typing and some formatting changes. Help required here. Also what do i need to do for setting up permissions on someone else's fork

@cclauss
Copy link
Member

@cclauss cclauss commented May 19, 2021

As documented in CONTRIBUTING.md, on your local machine, please run: python3 -m doctest -v conversions/hex-bin.py

@ktsrivastava29 ktsrivastava29 requested a review from onlinejudge95 May 19, 2021
@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 19, 2021

Sir, I really want to contribute something... I'm putting all my efforts to do something concrete but I see it's taking too much of your time... I genuenly apologize for this. Sir I've made some changes could you please review it and make me aware of what else should be done.

from typing import Union


def convert(num: str) -> Union[bool, int]:

This comment has been minimized.

@cclauss

cclauss May 19, 2021
Member

I know that this was recommended in a previous review but I would like to request a change.

When someone calls this function, they expect to get back a binary number which they may then use in a mathematical calculation. Returning a bool or an int in this case is not considered Pythonic because it breaks their expectations so let's agree that this function should return an int or should raise an Exception if garbage data is provided as input. This is the way that Python's builtin functions behave:

>>> hex(123)
'0x7b'
>>> hex("dog")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer

Also, the name convert() is not very self-documenting and neither is num so let's go for:

Suggested change
def convert(num: str) -> Union[bool, int]:
def hex_to_bin(hex_num: str) -> int:

This comment has been minimized.

@onlinejudge95

onlinejudge95 May 20, 2021
Collaborator

Apologies to the contributor for derailing but this makes more sense.

@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 20, 2021

Sir, I changed the names, and no doubt it has become very appealing now. Sir are there any further changes required, if so please let me know. I'll be happy to do that.

try:
int_num: int = int(hex_str, 16)
except ValueError:
return False
Comment on lines 41 to 44

This comment has been minimized.

@onlinejudge95

onlinejudge95 May 20, 2021
Collaborator

Instead of implicitly silencing the error as mentioned by @cclauss please use the previous format of int_num: int = int(hex_str, 16) as this explicitly raises the error.
Sorry for the turnaround, but it makes more sense. I would have pushed this change myself but I can't due to some permisions.

if not hex_str:
return False
Comment on lines 34 to 35

This comment has been minimized.

@onlinejudge95

onlinejudge95 May 20, 2021
Collaborator

Same as below. Simply raise a ValueError instead of returning a boolean

1111111111111111
>>> convert("F-f")
>>> hex_to_bin("F-f")
False

This comment has been minimized.

@cclauss

cclauss May 20, 2021
Member

The function returns an int or it raises an Exception.

False
>>> convert("")
>>> hex_to_bin("")
False

This comment has been minimized.

@cclauss

cclauss May 20, 2021
Member

The function returns an int or it raises an Exception.

False
"""

bin_str: str = ""
hex_str: str = num.strip()
hex_str: str = hex_num.strip()

if not hex_str:
return False

This comment has been minimized.

@cclauss

cclauss May 20, 2021
Member

The function returns an int or it raises an Exception.

@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 20, 2021

Sirs, I've modified the code again, could you please look into it and see if any further modification is demanded.

@ktsrivastava29 ktsrivastava29 requested a review from onlinejudge95 May 20, 2021
Copy link
Member

@cclauss cclauss left a comment

LGTM... Cool algorithm. Thanks for doing this.

@cclauss cclauss merged commit 368ce7a into TheAlgorithms:master May 20, 2021
2 checks passed
2 checks passed
@github-actions
build
Details
@github-actions
pre-commit
Details
@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 20, 2021

Sir, I'm glad you merged it. It is my first contribution, although it took some 3 days for me to present an algorithm that can get merged but yeah finally I did it. Thank you so much @cclauss and @onlinejudge95 .... you people are really amazing... You kept suggesting improvement without being rude.... and your suggestions helped me learn some exciting things.

@onlinejudge95
Copy link
Collaborator

@onlinejudge95 onlinejudge95 commented May 20, 2021

Nice work @ktsrivastava29 👍🏻

@cclauss
Copy link
Member

@cclauss cclauss commented May 20, 2021

You can see that I removed type hints inside the function like:

  1. is_negative = hex_num[0] == "-"
  2. int_num = int(hex_num, 16)
  3. bin_str = ""

because both Python and mypy are smart enough to recognize that these are bool, int, and str respectively.

The place where type hints are super important are on the function parameters and return value:
def hex_to_bin(hex_num: str) -> int:
so that mypy can catch it if someone tries to pass in the wrong datatype or treat the return value as the wrong datatype.

@ktsrivastava29
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 commented May 20, 2021

Thank you sir, you added a new thing to my reasoning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants