Skip to content

Instantly share code, notes, and snippets.

@GuillermoPena
Created June 6, 2014 10:50
Show Gist options
  • Save GuillermoPena/f359216681fd941d4ad8 to your computer and use it in GitHub Desktop.
Save GuillermoPena/f359216681fd941d4ad8 to your computer and use it in GitHub Desktop.
CheckIO - O'Reilly Challenge 2 : Ghost Age
# CheckIO - O'Reilly Challenge 2 : Ghost Age
# http://checkio.org
# Nicola takes a moment to study the ghosts.
# He is trying to think up a new method to determine just how old these ghosts are.
# He knows that their age is somehow related to their opacity.
# To measure their opacity Nikola uses a scale of 10000 units to 0 units,
# where 10000 is a completely opaque newborn ghost (0 years old) and 0 is completely transparent.
# After some experimenting, Nikola thinks he has discovered the law of ghostly opacity.
# On every birthday, a ghost's opacity is reduced by a number of units
# equal to its age when its age is a Fibonacci number or increased by 1 if it is not.
# Precondition:age < 5000
# 0 < opacity ≤ 10000
# Get Fibonacci number n
def nextFibo(n):
if n<2: return n
else: return nextFibo(n-1) + nextFibo(n-2)
def checkio(limit):
age=0
lastFibo=1
indexFibo=2
opacity=10000
while opacity!=limit: # While I dont found transparency number...
age+=1
if age==lastFibo: # -age if age is a Fibonacci number
opacity-=age
indexFibo+=1
lastFibo=nextFibo(indexFibo)
else: # +1 in other case
if opacity<10000: # Keeping opacity value into the limis
opacity+=1
return age
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(9995) == 4, "4 years"
assert checkio(3703) == 4665, "4665 years"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment