-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added automated doctest to decimal_to_hexadecimal.py in conversions #1071
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
34c1907
added automated doctest to decimal_to_hexadecimal.py in conversions
jasper256 2d5a64d
improved error handling and added more test cases in decimal_to_hexad…
jasper256 f299daa
implemented 0x notation and simplified AssertionError
jasper256 ea565f4
fixed negative notation and added comparison test against Python hex …
jasper256 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,23 +21,60 @@ | |
} | ||
|
||
def decimal_to_hexadecimal(decimal): | ||
""" take decimal value, return hexadecimal representation as str """ | ||
""" | ||
take integer decimal value, return hexadecimal representation as str | ||
>>> decimal_to_hexadecimal(5) | ||
'5' | ||
>>> decimal_to_hexadecimal(15) | ||
'f' | ||
>>> decimal_to_hexadecimal(37) | ||
'25' | ||
>>> decimal_to_hexadecimal(255) | ||
'ff' | ||
>>> decimal_to_hexadecimal(4096) | ||
'1000' | ||
>>> decimal_to_hexadecimal(999098) | ||
'f3eba' | ||
>>> # negatives work too | ||
>>> decimal_to_hexadecimal(-256) | ||
'-100' | ||
>>> # floats are acceptable if equivalent to an int | ||
>>> decimal_to_hexadecimal(17.0) | ||
'11' | ||
>>> # other floats will error | ||
>>> decimal_to_hexadecimal(16.16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's simplify to: >>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AssertionError There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. |
||
Traceback (most recent call last): | ||
File "doctest.py", line 1329, in __run | ||
compileflags, 1), test.globs) | ||
File "<doctest __main__.decimal_to_hexadecimal[8]>", line 1, in <module> | ||
decimal_to_hexadecimal(16.16) | ||
File "decimal_to_hexadecimal.py", line 51, in decimal_to_hexadecimal | ||
assert type(decimal) in (int, float) and decimal == int(decimal) | ||
AssertionError | ||
>>> # strings will error as well | ||
>>> decimal_to_hexadecimal('0xfffff') | ||
Traceback (most recent call last): | ||
File "doctest.py", line 1329, in __run | ||
compileflags, 1), test.globs) | ||
File "<doctest __main__.decimal_to_hexadecimal[9]>", line 1, in <module> | ||
decimal_to_hexadecimal('0xfffff') | ||
File "decimal_to_hexadecimal.py", line 58, in decimal_to_hexadecimal | ||
assert type(decimal) in (int, float) and decimal == int(decimal) | ||
AssertionError | ||
""" | ||
assert type(decimal) in (int, float) and decimal == int(decimal) | ||
hexadecimal = '' | ||
negative = False | ||
if decimal < 0: | ||
negative = True | ||
decimal *= -1 | ||
while decimal > 0: | ||
remainder = decimal % 16 | ||
decimal -= remainder | ||
decimal, remainder = divmod(decimal, 16) | ||
hexadecimal = values[remainder] + hexadecimal | ||
decimal /= 16 | ||
if negative: | ||
hexadecimal = '-' + hexadecimal | ||
return hexadecimal | ||
|
||
def main(): | ||
""" print test cases """ | ||
print("5 in hexadecimal is", decimal_to_hexadecimal(5)) | ||
print("15 in hexadecimal is", decimal_to_hexadecimal(15)) | ||
print("37 in hexadecimal is", decimal_to_hexadecimal(37)) | ||
print("255 in hexadecimal is", decimal_to_hexadecimal(255)) | ||
print("4096 in hexadecimal is", decimal_to_hexadecimal(4096)) | ||
print("999098 in hexadecimal is", decimal_to_hexadecimal(999098)) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
import doctest | ||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that you can drop the quotes if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I drop the quotes, the doctest errors. This is my first time using doctest so I do not know if there is a workaround.