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

added automated doctest to decimal_to_hexadecimal.py in conversions #1071

merged 4 commits into from
Jul 26, 2019

Conversation

jasper256
Copy link
Contributor

No description provided.

@cclauss
Copy link
Member

cclauss commented Jul 25, 2019

Please add tests for a negative number (-256) a floating point number (16.16) and a hex number (0xfffff). Thanks.

@jasper256
Copy link
Contributor Author

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

@cclauss
Copy link
Member

cclauss commented Jul 25, 2019

I would be OK with a test that fails...

>>> decimal_to_hexadecimal(16.16)  # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        KeyError: ...

Also to consider...

The line decimal, remainder = divmod(decimal, 16) would allow you to eliminate three other lines in this script.

@jasper256
Copy link
Contributor Author

@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.

@cclauss
Copy link
Member

cclauss commented Jul 26, 2019

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'
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(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.

@jasper256
Copy link
Contributor Author

My sense is that the output could be clearer as 0xffff ranther than just ffff. Your thoughts?

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'
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

Copy link
Member

@cclauss cclauss left a 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!

@cclauss cclauss merged commit 3b63857 into TheAlgorithms:master Jul 26, 2019
@jasper256
Copy link
Contributor Author

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!

@cclauss
Copy link
Member

cclauss commented Jul 26, 2019

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 ;-)

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants