added automated doctest to decimal_to_hexadecimal.py in conversions by jasper256 · Pull Request #1071 · TheAlgorithms/Python · GitHub
Skip to content

added automated doctest to decimal_to_hexadecimal.py in conversions - #1071

Merged
cclauss merged 4 commits into
TheAlgorithms:masterfrom
jasper256:master
Jul 26, 2019
Merged

cclauss merged 4 commits into
TheAlgorithms:masterfrom
jasper256:master

Conversation

@jasper256

Copy link
Copy Markdown
Contributor

No description provided.

@cclauss

cclauss commented Jul 25, 2019

Copy link
Copy Markdown
Member

@jasper256

Copy link
Copy Markdown
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

cclauss commented Jul 25, 2019

Copy link
Copy Markdown
Member

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
Copy Markdown
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

cclauss commented Jul 26, 2019

Copy link
Copy Markdown
Member

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

Comment thread conversions/decimal_to_hexadecimal.py Outdated
"""
take integer decimal value, return hexadecimal representation as str
>>> decimal_to_hexadecimal(5)
'5'

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread conversions/decimal_to_hexadecimal.py Outdated
>>> decimal_to_hexadecimal(17.0)
'11'
>>> # other floats will error
>>> decimal_to_hexadecimal(16.16)

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread conversions/decimal_to_hexadecimal.py Outdated
>>> # negatives work too
>>> decimal_to_hexadecimal(-256)
'-100'
'0x-100'

@cclauss cclauss Jul 26, 2019

Copy link
Copy Markdown
Member

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
Copy Markdown
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

@cclauss cclauss left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

cclauss commented Jul 26, 2019

Copy link
Copy Markdown
Member

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