diff --git a/1] guessing game/main.py b/1] guessing game/main.py index e69de29..313eabc 100644 --- a/1] guessing game/main.py +++ b/1] guessing game/main.py @@ -0,0 +1,23 @@ +from random import randint as ri + +def run(): + hidden_number = ri(1, 100) + + while True: + try: + user_number = int(input("Guess the number: ")) + except ValueError as err: + print(f"Your input: {user_number} is not a real number. Try again") + continue + + if user_number < hidden_number: + print("too small") + elif user_number > hidden_number: + print("too big") + else: + print("you won") + return + + +if __name__ == "__main__": + run() \ No newline at end of file diff --git a/2] loto/main.py b/2] loto/main.py index e69de29..15aa860 100644 --- a/2] loto/main.py +++ b/2] loto/main.py @@ -0,0 +1,48 @@ +from random import choice + +def collect_user_picks() -> set[int]: + print("Please pick 6 numbers. Each in range between 1-49\n") + + picks = set() + count = 1 + while count != 7: + try: + pick = int(input(f"Please pick {count}. number: ")) + if not (1 <= pick <= 49): + print(f"Number {pick} has to be within 1-49 range, try again") + continue + if pick in picks: + print(f"Number {pick} can be picked only once, try again") + continue + except ValueError as e: + print("This is not a number, try again") + continue + + count +=1 + picks.add(pick) + + return picks + + +def get_winning_numbers() -> set[int]: + all_numbers = [i for i in range(1, 50)] + winning_numbers = {choice(all_numbers) for _ in range(6)} + + return winning_numbers + + +def run(): + + user_picks = collect_user_picks() + print(f"User picked: {sorted(user_picks)}") + + winnig_numbers = get_winning_numbers() + print(f"Winning numbers are: {winnig_numbers}") + + intersection = user_picks & winnig_numbers + print(f"user picked {len(intersection)} numbers that were in winning numbers.") + + + +if __name__ == "__main__": + run() \ No newline at end of file diff --git a/3] guessing game 2/binary_search.py b/3] guessing game 2/binary_search.py new file mode 100644 index 0000000..ffce5b5 --- /dev/null +++ b/3] guessing game 2/binary_search.py @@ -0,0 +1,25 @@ +# this problem is classical application of binary search algorithm but add some more complexity because of the user inputs +# here is the version without user inputs (user is automated) + +from random import randint as ri + +def binary_search(): + low, high = 1, 1000 + user_number = ri(1, 1000) + print(f"I am the user, and I am thinking of the number {user_number}") + + while low <= high: + mid = (low + high) >> 1 + + if user_number < mid: + print(f"You guessed {mid}, and it is too big") + high = mid - 1 + elif user_number > mid: + print(f"You guessed {mid}, and it is too small") + low = mid + 1 + else: + print(f"You guessed {mid}, and it is correct!") + return mid + +if __name__ == "__main__": + binary_search() diff --git a/3] guessing game 2/main.py b/3] guessing game 2/main.py index e69de29..bbe5da8 100644 --- a/3] guessing game 2/main.py +++ b/3] guessing game 2/main.py @@ -0,0 +1,35 @@ +# here is the version with the user inputs as asked in the course + +def user_answer() -> str: + possible_answers = ["small", "big", "ok"] + while True: + answer = input() + if answer not in possible_answers: + print(f"You can answer only one of {possible_answers}") + continue + else: + return answer + + +def run(): + low, high = 1, 1000 + print(f"OK, I (the user) am thinking of number between {low} and {high} and you (the computer) must guess it") + print("After each guess I will give you answer, if your guess was too small or big\n'\n") + + while low <= high: + mid = (low + high) >> 1 + + print(f"I (the computer) am guessing {mid}. Is it small or big?") + + answer = user_answer() + if answer == "big": + high = mid - 1 + elif answer == "small": + low = mid + 1 + else: + return mid + +if __name__ == "__main__": + run() + + diff --git a/4] guessing game 3/main.py b/4] guessing game 3/main.py index e69de29..1440e82 100644 --- a/4] guessing game 3/main.py +++ b/4] guessing game 3/main.py @@ -0,0 +1 @@ +nedelal jsem, je tam moc HTML diff --git a/5] 2001/main.py b/5] 2001/main.py deleted file mode 100644 index e69de29..0000000 diff --git a/5] dice/main.py b/5] dice/main.py index e69de29..2eaaa22 100644 --- a/5] dice/main.py +++ b/5] dice/main.py @@ -0,0 +1,33 @@ +import re +from random import randint as ri + +def parse(code: str) -> tuple[str]: + pattern = re.compile(r'(\d*)D(3|4|6|8|10|12|20|100)([+-]\d+)?') + matched = pattern.match(code) + + if matched: + rolls = int(matched.group(1)) if matched.group(1) else 1 + dice = int(matched.group(2)) + extras = int(matched.group(3)) if matched.group(3) else 0 + + return rolls, dice, extras + + raise ValueError(f"The code {code} is invalid") + + +def simulate(code: str) -> int: + try: + rolls, dice, extras = parse(code) + except ValueError as ex: + raise ValueError(f"The code '{code}' is invalid, simulation cannot continue.") from ex + + result = extras + for _ in range(rolls): + result += ri(1, dice) + + return result + + +if __name__ == "__main__": + + result = simulate("2D10+10") diff --git a/6] 2001/main.py b/6] 2001/main.py new file mode 100644 index 0000000..65e4fc7 --- /dev/null +++ b/6] 2001/main.py @@ -0,0 +1 @@ +nedelal jsem nebyl cas \ No newline at end of file