Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
GenerateRepeatProgram solution in Uncategorized category for Ghosts Age by agdk26
# migrated from python 2.7
def checkio(opacity):
program=generate_repeat_program()
program_dict={}
exec(program,program_dict)
get_age=program_dict['get_age']
return get_age(opacity)
def generate_repeat_program():
"""
returns program with get_age function definition as string
get_age has full precalculated dictionary of ages for all possible opacities
def get_age(opacity):
opacities={
3219: 4181
3220: 4182
3221: 4183
...
9997: 2
9999: 1
10000: 0
}
return opacities[opacity]
"""
cur_opacity=10000
cur_age=0
fibonacci_numbers=get_fibonacci_numbers(cur_opacity)
opacities={}
while cur_opacity>=0:
assert cur_opacity not in list(opacities.keys()),'{} {}'.format(cur_opacity,cur_age)
opacities[cur_opacity]=cur_age
cur_age+=1
if cur_age in fibonacci_numbers:
cur_opacity-=cur_age
else:
cur_opacity+=1
program=[]
program.append( 'def get_age(opacity):')
program.append( ' opacities={')
for cur_opacity in sorted(opacities.keys()):
program.append( ' {}: {},'.format(cur_opacity,opacities[cur_opacity]))
program.append( ' }')
program.append( ' return opacities[opacity]')
return '\n'.join(program)
def get_fibonacci_numbers(maxn):
result=[1,2]
i=2
while 1:
next=result[i-1]+result[i-2]
if next>maxn: break
result.append(next)
i+=1
return result
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(10000) == 0, "Newborn"
assert checkio(9999) == 1, "1 year"
assert checkio(9997) == 2, "2 years"
assert checkio(9994) == 3, "3 years"
assert checkio(9995) == 4, "4 years"
assert checkio(9990) == 5, "5 years"
Oct. 31, 2013