Last active
December 24, 2015 14:09
-
-
Save giovaneliberato/6810479 to your computer and use it in GitHub Desktop.
Globo.com puzzle at Python Brasil 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
numbers = {'1': 'um', '2': 'dois', '3': 'três', '4': 'quatro', '5': 'cinco', | |
'6': 'seis', '7': 'sete', '8': 'oito', '9': 'nove', '0': 'zero'} | |
units = ['milhão','milhar', 'centenas', 'dezenas', 'unidades'][::-1] | |
chars_incidence = {} | |
def puzzle(n): | |
fat = 1 | |
for i in range(1, n+1): | |
if i % 3 == 0 and i % 5 == 0: | |
print 'ping pong' | |
elif i % 3 == 0: | |
print 'ping' | |
elif i % 5 == 0: | |
print 'pong' | |
fat = fat * i | |
print 'fatorial de %d = %d' %(n, fat) | |
print str(fat) | |
result = [] | |
for i, ch in enumerate(str(fat)[::-1]): | |
result += "%s %s " % (numbers[ch], units[i]) | |
for ch in result: | |
if ch == ' ': | |
continue | |
if chars_incidence.get(ch, False): | |
chars_incidence[ch] = chars_incidence[ch] +1 | |
else: | |
chars_incidence[ch] = 1 | |
print result | |
print chars_incidence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment