Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Educational , Clear solution in Clear category for Ghosts Age by foromer4
def checkio(opacity):
ghost = Ghost(10000 , 0 , None , None)
while ghost.opacity != opacity:
ghost = next(ghost)
return ghost.age
def next(ghost):
if ghost.age == 0:
return Ghost(9999, 1 , None , None)
if ghost.age == 1:
return Ghost(9997 , 2 , 1 , 2)
if ghost.age + 1 == ghost.fib1 + ghost.fib2:
new_fib = ghost.fib1 + ghost.fib2
return Ghost(ghost.opacity - new_fib, ghost.age + 1, ghost.fib2 , new_fib)
return Ghost(ghost.opacity + 1, ghost.age + 1 , ghost.fib1 , ghost.fib2)
class Ghost:
def __init__(self, opacity, age , fib1 , fib2):
self.opacity = opacity
self.age = age
self.fib1 = fib1
self.fib2 = fib2
def __str__(self):
return "opacity: {0} , age: {1}, fib1: {2}, fib2: {3}".format(self.opacity, self.age, self.fib1, self.fib2)
#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"
Nov. 3, 2016