This notebook was prepared by [Thunder Shiviah](https://github.com/ThunderShiviah). Source and license info is on [GitHub](https://github.com/ThunderShiviah/code_guild).
We have to remember too many numbers: credit card PINs, student ID number, codes to open doors, and on and on. We're warned that we should remember these numbers instead of writing them down, but really the human brain is not well-suited to remembering a lot of meaningless numbers. I'll admit: I write them down. But could I remember more? People aren't any better at remembering random sequences of letters than remembering random sequences of digits. However, we are much better at remembering sounds we can pronounce. The word “fesi” doesn't mean anything to me, but it's easier to remember than the number 2354 or the harder-to-pronounce word “iksf”. It turns out to be pretty easy to convert numbers like 2354 into pronounceable nonsense words like “fesi” To create words that are easy to pronounce, we can build them out of consonant-vowel pairs like “ka” or “te”. As it turns out, if we omit ‘x’ and treat ‘y’ as a consonant, the English alphabet has 20 consonants (bcdfghjklmnpqrstvwyz) and 5 vowels (aeiou). 20×5=10×10=100, so one consonant and a vowel (20×5 combinations) is just enough to represent a pair of decimal digits (10×10 combinations). To convert a number (like my office phone number, 346-4140) into an pronounceable string, we'll need to divide it into two-digit chunks.
We're dividing it up in base 10; the underlying representation in base 2 is not relevant to us in this program. To get the last two (low order) digits in base 10, we can take the remainder when divided by 100, using the ‘%’ operator. To get the rest of the digits, we'll use integer division ‘//’, because we want an integer result (34641, not 34641.40).
Just right for picking one of 20 consonants and one of 5 vowels!
You will create a Python program to automate the process described above. To keep it simple, your program need convert only 4-digit numbers, like credit card PINs. The input to your program is a 4-digit decimal number. Your output should be a pronounceable word following the process above. If the input is not a 4-digit word the code should throw a ValueError exception (with a useful error message) using a try/except (read about error handling here).
Furthermore, code should follow proper PEP guidelines (see here for a rough overview) and include an adequate docstring (read the PEP about docstrings).
If you finish early try the following challenges:
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
In [14]:
## Constants used by this program
CONSONANTS = "bcdfghjklmnpqrstvwyz"
VOWELS = "aeiou"
def convert_pin(pin):
pin1 = pin.pop()
pass
pin1 = pi
In [5]:
pin = '2363'
pin1 = list(pin)
def remove_end(g):
Out[5]:
The following unit test is expected to fail until you solve the challenge.
In [1]:
# %load test_foo.py
from nose.tools import assert_equal
class Testconvert_pin(object):
def test_convert_pin(self):
assert_equal(convert_pin(None), ValueError)
assert_equal(convert_pin('absd'), ValueError)
assert_equal(convert_pin(0), ValueError)
assert_equal(convert_pin(4327), lohi)
assert_equal(convert_pin(1298), dizo)
print('Success: test_convert_pin')
def main():
test = Testconvert_pin()
test.test_convert_pin()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.