TheAlgorithms-Python
165 строк · 4.2 Кб
1"""
2guess the number using lower,higher and the value to find or guess
3
4solution works by dividing lower and higher of number guessed
5
6suppose lower is 0, higher is 1000 and the number to guess is 355
7
8>>> guess_the_number(10, 1000, 17)
9started...
10guess the number : 17
11details : [505, 257, 133, 71, 40, 25, 17]
12
13"""
14
15
16def temp_input_value(
17min_val: int = 10, max_val: int = 1000, option: bool = True
18) -> int:
19"""
20Temporary input values for tests
21
22>>> temp_input_value(option=True)
2310
24
25>>> temp_input_value(option=False)
261000
27
28>>> temp_input_value(min_val=100, option=True)
29100
30
31>>> temp_input_value(min_val=100, max_val=50)
32Traceback (most recent call last):
33...
34ValueError: Invalid value for min_val or max_val (min_value < max_value)
35
36>>> temp_input_value("ten","fifty",1)
37Traceback (most recent call last):
38...
39AssertionError: Invalid type of value(s) specified to function!
40
41>>> temp_input_value(min_val=-100, max_val=500)
42-100
43
44>>> temp_input_value(min_val=-5100, max_val=-100)
45-5100
46"""
47assert (
48isinstance(min_val, int)
49and isinstance(max_val, int)
50and isinstance(option, bool)
51), "Invalid type of value(s) specified to function!"
52
53if min_val > max_val:
54raise ValueError("Invalid value for min_val or max_val (min_value < max_value)")
55return min_val if option else max_val
56
57
58def get_avg(number_1: int, number_2: int) -> int:
59"""
60Return the mid-number(whole) of two integers a and b
61
62>>> get_avg(10, 15)
6312
64
65>>> get_avg(20, 300)
66160
67
68>>> get_avg("abcd", 300)
69Traceback (most recent call last):
70...
71TypeError: can only concatenate str (not "int") to str
72
73>>> get_avg(10.5,50.25)
7430
75"""
76return int((number_1 + number_2) / 2)
77
78
79def guess_the_number(lower: int, higher: int, to_guess: int) -> None:
80"""
81The `guess_the_number` function that guess the number by some operations
82and using inner functions
83
84>>> guess_the_number(10, 1000, 17)
85started...
86guess the number : 17
87details : [505, 257, 133, 71, 40, 25, 17]
88
89>>> guess_the_number(-10000, 10000, 7)
90started...
91guess the number : 7
92details : [0, 5000, 2500, 1250, 625, 312, 156, 78, 39, 19, 9, 4, 6, 7]
93
94>>> guess_the_number(10, 1000, "a")
95Traceback (most recent call last):
96...
97AssertionError: argument values must be type of "int"
98
99>>> guess_the_number(10, 1000, 5)
100Traceback (most recent call last):
101...
102ValueError: guess value must be within the range of lower and higher value
103
104>>> guess_the_number(10000, 100, 5)
105Traceback (most recent call last):
106...
107ValueError: argument value for lower and higher must be(lower > higher)
108"""
109assert (
110isinstance(lower, int) and isinstance(higher, int) and isinstance(to_guess, int)
111), 'argument values must be type of "int"'
112
113if lower > higher:
114raise ValueError("argument value for lower and higher must be(lower > higher)")
115
116if not lower < to_guess < higher:
117raise ValueError(
118"guess value must be within the range of lower and higher value"
119)
120
121def answer(number: int) -> str:
122"""
123Returns value by comparing with entered `to_guess` number
124"""
125if number > to_guess:
126return "high"
127elif number < to_guess:
128return "low"
129else:
130return "same"
131
132print("started...")
133
134last_lowest = lower
135last_highest = higher
136
137last_numbers = []
138
139while True:
140number = get_avg(last_lowest, last_highest)
141last_numbers.append(number)
142
143if answer(number) == "low":
144last_lowest = number
145elif answer(number) == "high":
146last_highest = number
147else:
148break
149
150print(f"guess the number : {last_numbers[-1]}")
151print(f"details : {last_numbers!s}")
152
153
154def main() -> None:
155"""
156starting point or function of script
157"""
158lower = int(input("Enter lower value : ").strip())
159higher = int(input("Enter high value : ").strip())
160guess = int(input("Enter value to guess : ").strip())
161guess_the_number(lower, higher, guess)
162
163
164if __name__ == "__main__":
165main()
166