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