Computer Science

posted by .

Answer the following question without running the code. The procedure isMyNumber is used to hide a secret number (integer). It takes an integer x as a parameter and compares it to the secret number. It returns:

-1 if the parameter x is less than the secret number

0 if the parameter x is correct

1 if the parameter x is greater than the secret number

The following procedure, jumpAndBackPedal, attempts to guess a secret number. The only way it can interact with the secret number is through the isMyNumber procedure explained above.

def jumpAndBackpedal(isMyNumber):
'''
isMyNumber: Procedure that hides a secret number.
It takes as a parameter one number and returns:
* -1 if the number is less than the secret number
* 0 if the number is equal to the secret number
* 1 if the number is greater than the secret number

returns: integer, the secret number
'''
guess = 1
if isMyNumber(guess) == 1:
return guess
foundNumber = False
while not foundNumber:
sign = isMyNumber(guess)
if sign == -1:
guess *= 2
else:
guess -= 1
return guess
Unfortunately, the implementation given does not correctly return the secret number. Please fix the errors in the code such that jumpAndBackpedal correctly returns the secret number.

  • Computer Science -

    def jumpAndBackpedal(isMyNumber):
    '''
    isMyNumber: Procedure that hides a secret number.
    It takes as a parameter one number and returns:
    * -1 if the number is less than the secret number
    * 0 if the number is equal to the secret number
    * 1 if the number is greater than the secret number

    returns: integer, the secret number
    '''

    guess = 1
    foundNumber = False
    while not foundNumber:
    sign = isMyNumber(guess)
    if sign == -1:
    guess *= 2
    elif sign == 1:
    guess -= 1
    elif sign == 0:
    foundNumber =True
    return guess

  • Computer Science -

    def jumpAndBackpedal(isMyNumber):
    '''
    isMyNumber: Procedure that hides a secret number.
    It takes as a parameter one number and returns:
    * -1 if the number is less than the secret number
    * 0 if the number is equal to the secret number
    * 1 if the number is greater than the secret number

    returns: integer, the secret number
    '''
    if isMyNumber(0) == 1:
    return 0
    guess = 1;
    foundNumber = False
    while not foundNumber:
    sign = isMyNumber(guess)
    if sign == -1:
    guess *= 2
    elif sign == 1:
    guess -= 1
    else:
    foundNumber = True;
    return guess

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Cont. English

    Dimmesdale is not free since he does not live with his moral code, because he is concerned with society’s condemnation. He hides his secret of adultery, which is going against his moral code of honesty. Consequently, he is unable …
  2. Please help--algebra!

    Here's the question I'm having trouble with. Please walk me completely through the question and give me the answer so I can thoroughly understand it, so I can solve similar problems in the future. #1: Mary uses a secret code to write …
  3. computer science

    Create a method called countZeros, which takes an integer array as an input parameter and returns the number of zero entries in the array
  4. programming

    There are several types of room in a hotel. The HotelRoom Class of Previous Programming Problem was not specific regarding the type of room. Assume that a hotel has guest rooms and meeting rooms. Modify the HotelRoom class as follows: …
  5. English

    1. He kept a secret. 2. He broke a secret 3. He let out a secret. 4. He gave away a secret. 5. He betrayed a secret. ================= What is the opposite of #1?
  6. programming

    I have a general question. i was given a task to write a c code which gets an integer number as an input and prints the number of digits the number has. I have written a function that does it, but the problem is whenever the integer …
  7. Computer Science/Pseudo Code Python

    Hi, I'm confused about an assignment. I'm not sure what to do and would like some help. An example: Write a program that obtains two integer numbers from the user. It will print out the sum of those numbers. Pseudo code: Prompt user …
  8. Math-Measure's & area

    1. Find the area of a parallelogram with base b and height h. B=82cm H=16.6cm a.**1,361.2 cm^2 b.6,724 cm^2 c.137.78 cm^2 d.98.6 cm^2 2.Identify all the sets to which the number belongs. Choose from rational number, irrational number, …
  9. English

    1. Don't tell anyone about it. It's a top secret. ----------------------- If we want to talk about the next valuable secret, what expression can we use?
  10. Pre Algebra

    1.Which of the following numbers is an example of an integer?

More Similar Questions