Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Task: Find 1 number in 100 with the lowest number of iterations.
- import random
- import argparse
- # Maxint is the maximum number we're going to, we hardcoded '100'
- # However we need this number later anyway, so we may as well variablise it.
- maxint = 100
- # Iterations is the number of times we had to scan, this number should be < 10
- iterations = 0
- # We need a random number between 1 and our maximum.
- number = random.randint(1,maxint)
- #verbose = False
- # Standard Arguments, such as debug or verbose
- parser = argparse.ArgumentParser(description=
- 'Find a random number in 100 quickly')
- parser.add_argument('-v', dest='verbose', action='store_true')
- args = parser.parse_args()
- verbose = args.verbose
- if verbose == True:
- print("number is %d" % number)
- current_count = maxint / 2
- # while Number != Current && iterations < 10
- iterations = iterations+1
- if number < current_count:
- if verbose == True:
- print("%d: Number (%d) less than %d" %
- (iterations, number, current_count))
- # decrease the "count" by half of the difference
- current_count = current_count + (current_count - previous_count / 2)
- else:
- previous_count = current_count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement