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 2 commits
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
65 changes: 51 additions & 14 deletions conversions/decimal_to_hexadecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

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.

Copy link
Contributor Author

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.

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

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()