Skip to content

Instantly share code, notes, and snippets.

@joaoventura
Last active August 29, 2015 14:23
Show Gist options
  • Save joaoventura/2a9b593fb70c7f8cf0d8 to your computer and use it in GitHub Desktop.
Save joaoventura/2a9b593fb70c7f8cf0d8 to your computer and use it in GitHub Desktop.
# A solution to the problem stated in
# https://www.codeeval.com/browse/170/
import math
def middleNumber(a, b):
""" Finds the middle integer between a and b.
If it is not a round number it rounds up to the
next integer.
"""
middle = a + (b-a) / 2.0
return math.ceil(middle)
def play(num, steps):
""" Performs a guess number play.
'num' is the range number and 'steps'
is a string with a sequence of steps.
"""
interval = [0, num]
for step in steps.split():
middle = middleNumber(interval[0], interval[1])
if step == 'Lower':
interval[1] = middle - 1
elif step == 'Higher':
interval[0] = middle + 1
else:
print(middle)
# Perform the following plays
play(100, 'Lower Lower Higher Lower Lower Lower Yay!')
play(948, 'Higher Lower Yay!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment