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 on the command line, and the output should be formatted exactly as shown in these examples:
$ python3 alphacode.py 4327
Encoding of 4327 is lohi
$ python3 alphacode.py 1298
Encoding of 1298 is dizo
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 [1]:
## Constants used by this program
CONSONANTS = "bcdfghjklmnpqrstvwyz"
VOWELS = "aeiou"
## Get pin code from command line
import sys
if (len(sys.argv) > 1) :
pincode = int(sys.argv[1])
else :
print("Usage: python3 alphacode 9999")
exit(1) ## Quit the program right here, indicating a problem
##FIXME: Replace the rest with your code
print("Pin code from command line", pincode)
The following unit test is expected to fail until you solve the challenge.
In [ ]:
# %load test_foo.py
from nose.tools import assert_equal
class TestFoo(object):
def test_foo(self):
assert_equal(foo(None), None)
assert_equal(foo(0), 0)
assert_equal(foo('bar'), 'bar')
print('Success: test_foo')
def main():
test = TestFoo()
test.test_foo()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.