python - converting numbers to consonant-vowel pairs -


the project asks divide initial set of numbers base 10, 6|30|45|10 using % 100 , // 100 operators, converting last 2 digits quotient between 0-19 , remainder between 0-4 using % 5 , // 5. have, appreciated, thanks!

integer = pin number_string = str(integer) number_string2 = str(integer) number_string % 100 number_string2 // 100  vowels = ["a", "e", "i", "o", "u"]  consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m",           "n", "p", "q", "r", "s", "t", "v", "w", "y", "z"] 

` code should produce in end

>>> pintoword(3463470) 'bomejusa' >>> pintoword(3464140) 'bomelela' 

you code bit strange. example, convert variable named integer string, , attempt perform arithmetic on it. , don't save result anywhere!

anyway, here's code want. uses built-in divmod function generate quotient , remainder in 1 function call.

vowels = "aeiou" consonants = "bcdfghjklmnpqrstvwyz"  def pintoword(n):     = []     while n:         n, r = divmod(n, 100)         c, v = divmod(r, 5)         a.append(vowels[v])         a.append(consonants[c])     return ''.join(reversed(a))  n in (3463470, 3464140):     print n, pintoword(n) 

output

3463470 bomejusa 3464140 bomelela 

we save letter pairs in list , join them in string @ end. divide 100 operation generates letter pairs in reverse order, need reverse list before join it.


fwiw, here's function performs inverse operation. rather doing .index calls on vowels , consonants strings uses pair of dictionaries indices, since that's faster.

def invert_string(s):     return dict((v, i) i, v in enumerate(s))  dvowels = invert_string(vowels) dconsonants = invert_string(consonants)  def wordtopin(s):     ''' convert string s of alternating consonants , vowels integer '''     num = 0     c, v in zip(*[iter(s)]*2):         num = 100 * num + 5 * dconsonants[c] + dvowels[v]     return num 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -