added automated doctest to decimal_to_hexadecimal.py in conversions - #1071
Conversation
|
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.
I believe that you can drop the quotes if you want.
There was a problem hiding this comment.
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.
Let's simplify to:
>>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AssertionError
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.
Please add a test:
>>> decimal_to_hexadecimal(-256) == hex(-256)
True
There was a problem hiding this comment.
Done. This also made me realize that I was supposed to put '-' before '0x', which I fixed
cclauss
left a comment
There was a problem hiding this comment.
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! |
…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.