Skip to content

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

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

Merged
merged 28 commits into from
May 20, 2021

Conversation

ktsrivastava29
Copy link
Contributor

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}.

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label May 17, 2021
Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 left a comment

Choose a reason for hiding this comment

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

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

@onlinejudge95
Copy link
Collaborator

@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.

@ghost ghost added the require type hints https://docs.python.org/3/library/typing.html label May 17, 2021
Copy link

@ghost ghost 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.

Copy link
Contributor Author

@ktsrivastava29 ktsrivastava29 left a comment

Choose a reason for hiding this comment

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

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.

@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label May 17, 2021
@onlinejudge95
Copy link
Collaborator

@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

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

Copy link
Collaborator

@onlinejudge95 onlinejudge95 left a comment

Choose a reason for hiding this comment

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

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.

bin_str = num3 + bin_str
num2 = num2 >> 1

if flag:
Copy link
Collaborator

Choose a reason for hiding this comment

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

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

return x if condition else y

@ghost ghost added the tests are failing Do not merge until tests pass label May 18, 2021
import math

def convert(num: str) -> str:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also remove this whitespace

@onlinejudge95
Copy link
Collaborator

@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

@ktsrivastava29
Copy link
Contributor Author

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]:
Copy link
Member

Choose a reason for hiding this comment

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

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:

Copy link
Collaborator

Choose a reason for hiding this comment

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

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

@ktsrivastava29
Copy link
Contributor Author

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.

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

Choose a reason for hiding this comment

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

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.

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

Choose a reason for hiding this comment

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

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

1111111111111111
>>> convert("F-f")
>>> hex_to_bin("F-f")
False
Copy link
Member

Choose a reason for hiding this comment

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

The function returns an int or it raises an Exception.

False
>>> convert("")
>>> hex_to_bin("")
False
Copy link
Member

Choose a reason for hiding this comment

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

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
Copy link
Member

Choose a reason for hiding this comment

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

The function returns an int or it raises an Exception.

@ktsrivastava29
Copy link
Contributor Author

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

@ghost ghost removed the tests are failing Do not merge until tests pass label May 20, 2021
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

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

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label May 20, 2021
@cclauss cclauss merged commit 368ce7a into TheAlgorithms:master May 20, 2021
@ktsrivastava29
Copy link
Contributor Author

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

Nice work @ktsrivastava29 👍🏻

@cclauss
Copy link
Member

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

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

shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Added a file that converts hexa to binary

* Added file to convert hexadecimal to binary

* Update hex-bin.py

* added type hint in the code

* Added doctest

* Added code to handle exception

* Resolved doctest issue

* Update hex-bin.py

* Modified convert function

* Added WhiteSpace around operators.

* Made more pythonic

* removed whitespace

* Updated doctest command

* Removed whitespace

* imported union

* Replaced flag with is_negative

* updated return type

* removed pip command

* Resolved doctest issue

* Resolved doctest error

* Reformated the code

* Changes function name

* Changed exception handling statements

* Update and rename hex-bin.py to hex_to_bin.py

* Update newton_method.py

* Update matrix_operation.py

* Update can_string_be_rearranged_as_palindrome.py

* Update hex_to_bin.py

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants