-
-
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
Conversation
Please add tests for a negative number (-256) a floating point number (16.16) and a hex number (0xfffff). Thanks. |
Will do @cclauss. I might add an assert statement to weed out floats that don't equal an integer. I'll try to get it done tomorrow if I have time |
I would be OK with a test that fails...
Also to consider... The line decimal, remainder = divmod(decimal, 16) would allow you to eliminate three other lines in this script. |
@cclauss thank you for your suggestions. I believe that I may have seen divmod before but I honestly forgot it existed. Anyway, I think you will also find that with more extensive test cases and comments on said cases the exact capabilities of the program are more clear now. |
My sense is that the output could be clearer as 0xffff ranther than just ffff. Your thoughts? |
""" | ||
take integer decimal value, return hexadecimal representation as str | ||
>>> decimal_to_hexadecimal(5) | ||
'5' |
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.
>>> 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 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
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.
Got it.
As a novice programmer I trust your thoughts. I had never seen this notation before, but I looked it up and it seems commonly accepted so I added it. |
>>> # negatives work too | ||
>>> decimal_to_hexadecimal(-256) | ||
'-100' | ||
'0x-100' |
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.
Please add a test:
>>> decimal_to_hexadecimal(-256) == hex(-256)
True
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.
Done. This also made me realize that I was supposed to put '-' before '0x', which I fixed
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.
Thanks much for all of your nice work here!
The pleasure was all mine. Every time you suggested a feature, I had to look it up and learn how to implement it, which is helping me improve as a programmer. This was my first time contributing to an open source project, and I look forward to working on more in the future! |
Awesome! Working on open source can be very rewarding when done with an attitude of continuous learning and collaboration. You are off to a nice start here. Adding support for floats is still possible...https://docs.python.org/3/library/stdtypes.html#float.hex ;-) |
…heAlgorithms#1071) * added automated doctest to decimal_to_hexadecimal.py in conversions * improved error handling and added more test cases in decimal_to_hexadecimal.py * implemented 0x notation and simplified AssertionError * fixed negative notation and added comparison test against Python hex function
No description provided.