Advertisement
Dijit

1in100.py

Jan 2nd, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Task: Find 1 number in 100 with the lowest number of iterations.
  3. import random
  4. import argparse
  5.  
  6. # Maxint is the maximum number we're going to, we hardcoded '100'
  7. # However we need this number later anyway, so we may as well variablise it.
  8. maxint = 100
  9. # Iterations is the number of times we had to scan, this number should be < 10
  10. iterations = 0
  11. # We need a random number between 1 and our maximum.
  12. number = random.randint(1,maxint)
  13. #verbose = False
  14.  
  15. # Standard Arguments, such as debug or verbose
  16. parser = argparse.ArgumentParser(description=
  17.                                     'Find a random number in 100 quickly')
  18. parser.add_argument('-v', dest='verbose', action='store_true')
  19. args = parser.parse_args()
  20.  
  21. verbose = args.verbose
  22.  
  23. if verbose == True:
  24.     print("number is %d" % number)
  25.  
  26. current_count = maxint / 2
  27. # while Number != Current && iterations < 10
  28. iterations = iterations+1
  29. if number < current_count:
  30.     if verbose == True:
  31.         print("%d: Number (%d) less than %d" %
  32.                 (iterations, number, current_count))
  33.     # decrease the "count" by half of the difference
  34.     current_count = current_count + (current_count - previous_count / 2)
  35.  
  36. else:
  37.     previous_count = current_count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement