Skip to content

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 4 commits into from
Jul 26, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions conversions/decimal_to_hexadecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,34 @@

def decimal_to_hexadecimal(decimal):
"""
take integer decimal value, return hexadecimal representation as str
take integer decimal value, return hexadecimal representation as str beginning with 0x
>>> decimal_to_hexadecimal(5)
'5'
'0x5'
>>> decimal_to_hexadecimal(15)
'f'
'0xf'
>>> decimal_to_hexadecimal(37)
'25'
'0x25'
>>> decimal_to_hexadecimal(255)
'ff'
'0xff'
>>> decimal_to_hexadecimal(4096)
'1000'
'0x1000'
>>> decimal_to_hexadecimal(999098)
'f3eba'
'0xf3eba'
>>> # negatives work too
>>> decimal_to_hexadecimal(-256)
'-100'
'0x-100'
Copy link
Member

@cclauss cclauss Jul 26, 2019

Choose a reason for hiding this comment

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

Please add a test:

>>> decimal_to_hexadecimal(-256) == hex(-256)
True

https://docs.python.org/3/library/functions.html#hex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. This also made me realize that I was supposed to put '-' before '0x', which I fixed

>>> # floats are acceptable if equivalent to an int
>>> decimal_to_hexadecimal(17.0)
'11'
'0x11'
>>> # other floats will error
>>> decimal_to_hexadecimal(16.16)
>>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS
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')
>>> decimal_to_hexadecimal('0xfffff') # doctest: +ELLIPSIS
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)
Expand All @@ -73,6 +63,7 @@ def decimal_to_hexadecimal(decimal):
hexadecimal = values[remainder] + hexadecimal
if negative:
hexadecimal = '-' + hexadecimal
hexadecimal = '0x' + hexadecimal
return hexadecimal

if __name__ == '__main__':
Expand Down