-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_binary_search.py
More file actions
103 lines (72 loc) · 2.52 KB
/
Copy pathbasic_binary_search.py
File metadata and controls
103 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
|--------------------------------------------------------------------------
| Binary Search
|--------------------------------------------------------------------------
| Jason Monroe
| jason@jasonmonroe.com
|
| December 24, 2024
|
| Guess a number between y and x. If the guess is incorrect, try again until
| the correct number is found.
"""
import random
print('Binary Search')
# Initialize the guessing game.
def init_guessing():
min_val, max_val = int(input('Enter the minimum value: ')), int(input('Enter the maximum value: '))
number = random.randint(min_val, max_val)
print(f'The minimum value is {min_val} and the maximum value is {max_val}')
return {'min_val': min_val, 'max_val': max_val, 'number': number}
# Start guessing the number.
def start_guessing(number):
guess_cnt = 0
guess = None
max_guesses = 10
while guess != number and guess_cnt < max_guesses:
print(f'\nGuess count: {guess_cnt}')
guess = int(input('Enter your guess: '))
# If not the answer tell it's lower or higher?
if guess < number:
print('Higher')
elif guess > number:
print('Lower')
guess_cnt += 1
if guess_cnt == 10:
print(f'You have reached the maximum number of tries. The number was {number}')
if guess == number:
print(f'* You have guessed the number {number} in {guess_cnt + 1} tries.')
def auto_guessing(data):
guess_cnt = 0
guess = None
max_guesses = 20
number = data['number']
min_val = data['min_val']
max_val = data['max_val']
print('Auto guessing the number...')
while guess != number and guess_cnt < max_guesses:
print('\nGuess count:', guess_cnt)
if guess is None:
guess = (min_val + max_val) // 2
print('Auto-Guess:', guess)
if number < guess:
print('Lower')
max_val = guess
guess = (min_val + guess) // 2
elif number > guess:
print('Higher')
min_val = guess
guess = (max_val + guess) // 2
guess_cnt += 1
if guess_cnt == 20:
print(f'Auto-reached the maximum number of tries. The number was {number}')
return
if guess == number:
print(f'* Auto-guessed the number {number} in {guess_cnt + 1} tries.')
guess_data = init_guessing()
# Start the guessing game.
start_guessing(guess_data['number'])
# Do this automatically by using binary search algorithm.
print('\nBinary Search Algorithm')
auto_guessing(guess_data)
# End of file