Advertisement
misingnoglic

Problem 4 MITx 6.00x midterm

Mar 28th, 2013
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. def jumpAndBackpedal(isMyNumber):
  2.     '''
  3.    isMyNumber: Procedure that hides a secret number.
  4.     It takes as a parameter one number and returns:
  5.     *  -1 if the number is less than the secret number
  6.     *  0 if the number is equal to the secret number
  7.     *  1 if the number is greater than the secret number
  8.  
  9.    returns: integer, the secret number
  10.    '''
  11.     guess = 1
  12.     if isMyNumber(guess) == 0:
  13.         return guess
  14.     foundNumber = False
  15.     while not foundNumber:
  16.         sign = isMyNumber(guess)
  17.         if sign == 0:
  18.             foundNumber = True
  19.         elif sign == -1:
  20.             guess += 1
  21.         else:
  22.             guess -= 1
  23.     return guess
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement