Thursday, October 25, 2012

Pronounceable Pins alphacode.py


"""
alphacode.py Assignment 1 (part 2), CIS 210, Fall 2012
Author:  Audrey Stroh
Credits: used the "starter code" file from Professor Michael Young from
http://www.cs.uoregon.edu/Classes/12F/cis210/assignments/base/alphacode.py
9/24/2012

Takes a 4-digit (integer) number and converts it to a word
that is easier to remember by splitting up the number into 2 2-digit halves,
then taking each 2-digit integer and finding the quotient and remainder after dividing by 5.
The quotient corresponds to a consonant, and the remainder corresponds to a vowel.

USAGE: python3 alphacode.py #### (enter 4 integers)
"""


## Constants used by this program
CONSONANTS = "bcdfghjklmnpqrstvwyz"  ## this is the selection of vowels and consonants to pick from that will correspond to numbers in the PIN
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

pincode_prefix = pincode//100 ## This splits the PIN into two integer parts: the first 2 and last 2 digits
pincode_suffix = pincode%100

quotient_prefix = pincode_prefix//5 # Get quotient of both 2-digit integers
remainder_prefix = pincode_prefix%5 # Should be in range 0,...,19

quotient_suffix = pincode_suffix//5 # Get remainder of both 2-digit integers
remainder_suffix = pincode_suffix%5 # Should be in range 0,...,4

quotient_1_consonant = CONSONANTS[quotient_prefix] #finds corresponding consonant from the "consonants" string above
remainder_1_vowel = VOWELS[remainder_prefix]

quotient_2_consonant = CONSONANTS[quotient_suffix]
remainder_2_vowel = VOWELS[remainder_suffix]

print("Encoding of", pincode, "is: ", quotient_1_consonant + remainder_1_vowel + quotient_2_consonant + remainder_2_vowel)
# Creates string of the 4 letters and returns to user

No comments:

Post a Comment