Advertisement
makispaiktis

Codejam2018 - Number Guesser

Mar 9th, 2021 (edited)
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. '''
  2. Analysis
  3. Test set 1
  4. Since A = 0 and B = 30 in this test set, and since we get N = 30 tries per test case, we can simply guess every number
  5. from 1 to 30 until the judge sends back CORRECT.
  6. Test set 2
  7. In test set 2, since the answer could be anywhere in the range (0, 109] and we still have only 30 guesses, we will use binary search.
  8. Initially, we know the answer P is in [1, 109], which is a big range! To cut that range by half, our first guess will be (1 + 109) / 2 = 5×108.
  9. If the judge sends back TOO_SMALL, we will know that P is in [1, 5×108).
  10. Similarly, if the judge sends back TOO_BIG, P is in (5×108, 109]. Otherwise, P is 5×108 and we are done.
  11. '''
  12.  
  13. from random import randrange
  14. from math import log2
  15.  
  16. def help(guess, r):
  17.     if guess > r:
  18.         print("Your guess (" + str(guess) + ") is greater than the desired number.")
  19.         return 1
  20.     elif guess < r:
  21.         print("Your guess (" + str(guess) + ") is lower than the desired number.")
  22.         return -1
  23.     else:
  24.         print("CORRECT - Your guess (" + str(guess) + ") is equal to the desired number.")
  25.         return 0
  26.  
  27.  
  28. # ******** 1. Initialization of the problem ********
  29. print()
  30. akro1 = 1
  31. akro2 = 10**6
  32. N = 20
  33. counter = 0
  34. r = randrange(akro1, akro2 + 1)
  35. guesses = list()
  36. RIGHTN = log2(akro2 - akro1 + 1)
  37. if RIGHTN != int(RIGHTN):
  38.     RIGHTN = int(RIGHTN) + 1
  39. print("**********************************************************")
  40. print("    akro1 =", akro1, "akro2 =", akro2, "r =", r)
  41. print("N = " + str(N) + ", minimumN I need for sure is " + str(int(RIGHTN)))
  42. print("**********************************************************")
  43. print()
  44.  
  45. # ******** 2. First guess to start the problem ********
  46. guess = (akro1 + akro2) // 2
  47. guesses.append(guess)
  48. answer = help(guess, r)
  49. counter += 1
  50.  
  51. # ******** 3. In all the cases, I will apply the binary search ********
  52. foundAnswer = False
  53. while answer != 0 and counter < N:
  54.     if answer == 1:
  55.         # That means that my guess is GREATER, so I have to check in the down "sub-interval"
  56.         akro2 = guess
  57.         guess = (akro1 + akro2) // 2
  58.         counter += 1
  59.     else:
  60.         akro1 = guess
  61.         guess = (akro1 + akro2) // 2
  62.         counter += 1
  63.     answer = help(guess, r)
  64.     if answer == 0:
  65.         foundAnswer = True
  66.         break
  67.  
  68.  
  69. print()
  70. print("**********************************************************")
  71. if foundAnswer == True:
  72.     print("Correct answer = " + str(guess))
  73.     print("Number of guesses = " + str(counter))
  74.     print("Limit = N = " + str(N))
  75. else:
  76.     print("Correct answer was " + str(r), ", but your last guess was " + str(guess))
  77.     print("You ran out of tries....")
  78.     print("Number of guesses = " + str(counter))
  79.     print("Limit = N = " + str(N))
  80. print("**********************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement