Last active
August 29, 2015 14:23
-
-
Save joaoventura/2a9b593fb70c7f8cf0d8 to your computer and use it in GitHub Desktop.
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
# 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