2021年3月8日星期一

Wrong Answer in Google Kickstart Practice Round 2019 Question 1 (Number Guessing)

So I am still learning to code and use python as my primary language. The question is we have two numbers. A and B which we have to take as input. A will be 0 and B is given in two test cases as 30 and 10^9. The system will pick a number (say P) between A(exclusive) and B(inclusive). We have to write a program to guess the number. If our guess is higher than P, the system will output "TOO_BIG" and we have to adjust our next guess. If our guess is lower than P, then we'll get "TOO_SMALL". If it's right, then we'll get "CORRECT". We have N tries to guess the number and N = 30.

This is my code:

import sys    def solve(lower,upper):            guessed_right = False            for _ in range(no_of_guesses):          midpoint = (lower + upper)//2          guess = midpoint          print(guess)                    sys.stdout.flush()          judge = input()                    if judge == "CORRECT":              guessed_right = True              break                        elif judge == "TOO_BIG":              upper = midpoint                        elif judge == "TOO_SMALL":              lower = midpoint                    elif judge == "WRONG_ANSWER":              sys.exit()                      def run():      T = int(input())      for case in range(T):          lower, upper = map(int(input().split()))          no_of_guesses = int(input())          solve(lower + 1, upper)  

I am getting wrong answer for this and can't seem to find the problem

https://stackoverflow.com/questions/66540591/wrong-answer-in-google-kickstart-practice-round-2019-question-1-number-guessing March 09, 2021 at 11:55AM

没有评论:

发表评论