diff --git a/1] guessing game/README.md b/1] guessing game/README.md new file mode 100644 index 0000000..a3c9c02 --- /dev/null +++ b/1] guessing game/README.md @@ -0,0 +1,23 @@ +## GUESSING GAME + +Symple console application for guessing random number in the range 1-100. + + +# Lauched application + +Use the "cd" command to navigate to the "1] guessing game" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The application generates a random number between 1 and 100. + +2. The player tries to guess this number. The application prompts if the player to enter an integer. If the player enters letters or floats, the app asks them to correct it. If the entered number is higher or lower than the selected number, the application alerts the player. If the player guesses the number, the app will tell them and the game is ending. + +3. If the player wants to quit the application during the game, he can to use keyboard "CTRL+C". \ No newline at end of file diff --git a/1] guessing game/main.py b/1] guessing game/main.py index e69de29..cdd59ad 100644 --- a/1] guessing game/main.py +++ b/1] guessing game/main.py @@ -0,0 +1,42 @@ +from random import randint + + +def num_guessing_game() -> None: + """ + The function uses randint() and generates a number in + the range 1-100. Then it uses "while loop" and it gets + number tip from the player. The functions says the player + if is his tip higher or lower until the player guesses + the number. + """ + print("Welcome to our guessing game! " + "Can you guess the number between 1 and 100?") + + # getting a random number + wanted_num = randint(1, 101) + + # getting a number of player + while True: + try: + player_num = int(input("Enter whole number: ")) + + # wrong value entery check + except ValueError: + print("You entered the wrong value, try again...") + continue + + # testing a player's number + if player_num < wanted_num: + print("Too small number...") + continue + elif player_num > wanted_num: + print("Too big number...") + continue + else: + print("!!! YOU WIN !!!") + return + + + + +print(num_guessing_game()) diff --git a/2] loto/README.md b/2] loto/README.md new file mode 100644 index 0000000..3a3f55a --- /dev/null +++ b/2] loto/README.md @@ -0,0 +1,25 @@ +## LOTTO SIMULATOR + +Simple console LOTTO application. The application gives 6 random numbers in range 1 - 49 and player is guessing them. + + +# Lauched application + +Use the "cd" command to navigate to the "2] loto" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The function gets the list numbers from a player and while checks: +- whether the entered text is a valid number, +- whether the user has not entered a given number before, +- if the number is in the range of 1-49. +2. The function uses randint (Random library) and gets a list of six original winning numbers in the range 1-49. +3. The function dislays list of numbers from player and list of winning numbers and then compares them. +4. The function displays how many numbers the player guessed. \ No newline at end of file diff --git a/2] loto/main.py b/2] loto/main.py index e69de29..71465a6 100644 --- a/2] loto/main.py +++ b/2] loto/main.py @@ -0,0 +1,71 @@ +from random import randint + + +def nums_of_player() -> set: + """ + The function gets a list of six original numbers + of the player. + + Funktion checks whether: + player entered integer in the range 1-49, + player has not entered a given number before. + + Funktion return a sorted list of numbers. + """ + # gettint a list of numbers + player_nums = set() + while len(player_nums)<6: + try: + num = int(input(f"Enter number No. {len(player_nums) +1}: ")) + # check a number + if num in player_nums: + print("The number has already been entered. Try again...") + elif num < 1 or num > 49: + print("The number is not in range of 1 - 49. Try again...") + else: + player_nums.add(num) + + except ValueError: + print("Value is not whole number. Try again...") + + return player_nums + + +def winning_nums() -> set: + """ + The function uses randint() and gets a list + of six original numbers in the range 1-49. + """ + draw_nums = set() + while len(draw_nums) < 6: + draw_nums.add(randint(1, 50)) + + return draw_nums + + +def main(): + """ + The function uses the "nums_of_player()" function and it gets the list + numbers from a player. Then it uses the "winning_nums()" function and + it gets list of winning numbers. + The function dislays these lists of numbers and compares them. Then it + displays how many numbers the player guessed. + """ + print(f"WELCOME TO THE PYTHON LOTTERY!\n{'=' * 30}\nWe draw " + "sixt numbers in range of 1-49. Try to guess them...") + + # getting numbers of player + player_nums = nums_of_player() + print(f"Your numbers:\n{', '.join(map(str, player_nums))}") + + # getting winning numbers + draw_nums = winning_nums() + print(f"Winning numbers:\n{', '.join(map(str, draw_nums))}") + + # evaluation of results + hits = draw_nums.intersection(player_nums) + separator = "=" * 25 + print(f"{separator}\nSUM OF GUESSED NUMBERS: {len(hits)}\n{separator}") + + +main() diff --git a/3] guessing game 2/README.md b/3] guessing game 2/README.md new file mode 100644 index 0000000..3c33183 --- /dev/null +++ b/3] guessing game 2/README.md @@ -0,0 +1,23 @@ +## Number guessing game 2 + +A simple console application that guesses a player's number in the range of 1-1000. + + +# Lauched application + +Use the "cd" command to navigate to the "3] guessing game 2" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The function greets a player and uses a math formula for getting most likely numbers in the range 0 - 1000. + +2. Then the function finds, if is the number correct, too hight or too low. If the quessed number is not correct, the function change numbers for calculation and repeats the process. + +3. This process is repeat only ten times, because if is it not enought, the game is not fair. diff --git a/3] guessing game 2/main.py b/3] guessing game 2/main.py index e69de29..dfc0e39 100644 --- a/3] guessing game 2/main.py +++ b/3] guessing game 2/main.py @@ -0,0 +1,62 @@ +def get_answer() -> str: + """ + A function for getting player's answer. + + A function gets the player's answer and chcek if + it is in the list of correct answers. If it is, it + return answer as string. If it is not, the function + starts again. + """ + # getting an answer + answer = input("(ALLOWED ANSWERS: too low / too high / " + "you quessed):\n") + + # answer check + correct_answers = ["too low", "too high", "you quessed"] + if answer in correct_answers: + return answer + else: + print("Wrong value, try again...") + get_answer() + + +def main(): + """ + A function for guessing a number. + + The function uses a math formula for getting most + likely numbers in the range 0 - 1000. Then the function + uses get_answer() for information, if is the number correct, + too hight or too low. + If the quessed number is not correct, the function change + numbers for calculation and repeats the process. + If the game is fair, the PC guesses the number within ten cycles. + """ + # hello area + print("Think about a number from 0 do 1000 and " + f"let me guess it!\n{'=' * 56}") + + min = 0 + max = 1000 + num_of_tries = 10 + + while num_of_tries: + # calculation of the quessed number + guess = (max-min) // 2 + min + print(f"Guessing: {guess}. Am I right?") + print(f"Pokus {num_of_tries}") + + # processing the player's response + answer = get_answer() + if answer == "too high": + max = guess + num_of_tries -= 1 + elif answer == "too low": + min = guess + num_of_tries -= 1 + elif answer == "you quessed": + print("I won!") + num_of_tries = 0 + + +main() \ No newline at end of file diff --git a/4] guessing game 3/README.md b/4] guessing game 3/README.md new file mode 100644 index 0000000..85da2e5 --- /dev/null +++ b/4] guessing game 3/README.md @@ -0,0 +1,25 @@ +## Number guessing game 2 + +Flasková aplikace, ve které PC hádá číslo v rozsahu 0-1000. + + +# Lauched application + +Use the "cd" command to navigate to the "4] guessing game 3" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. Aplikace uvítá hráče a zeptá se ho, jestli chce hrát. + +2. Po potvrzení se zobrazí hrací stránka s tipem čísla od počítače a hráč je vyzván, aby řekl, jestli je číslo moc vysoké nebo nízké nebo jestli je to správné číslo. + +3. Po kliknutí na tlačítko "too low" nebo "too hide" se tip čísla přepočítá a znovu se opakuje bod dva. + +4. Pokud je tipované číslo správné, hráč klikne na tlačítko "you win". PC poděkuje za hru a dá hráči možnost zahrát si zvovu. diff --git a/4] guessing game 3/main.py b/4] guessing game 3/main.py index e69de29..4f17bb6 100644 --- a/4] guessing game 3/main.py +++ b/4] guessing game 3/main.py @@ -0,0 +1,60 @@ +from flask import Flask, request, render_template + +app = Flask(__name__) + + +def calculation(mnimum=int, mximum=int) -> int: + """ + Fukce se základním vzorcem pro výpočet + tipu čísla. + """ + return (mximum-mnimum) // 2 + mnimum + + +@app.route("/", methods=["GET", "POST"]) +def game(): + """ + Aplikace pro hádání čísla hráče v rozsahu 0-1000. + Funkce získá vstupní hodnoty mn a mx, díky kterým + vypočítává nejpravděpodobnější číslo. Hodnoty jsou na + začátku nastavené defaltně a v průběhu hry se upravují + podle reakce uživatele dokud aplikace nedopočítá správné + číslo. + """ + # zobrazení úvodní stránky hry + if request.method == "GET": + return render_template("index.html") + + # zobrazení hrací stránky + elif request.method == "POST": + # získání hodnot mn/mx/guess + mn = int(request.form.get("mn", default=0)) + mx = int(request.form.get("mx", default=1000)) + guess = 0 + + # úprava hodnot podle vstupu uživatele + answer = request.form.get("button") + if answer == "low": + mn = int(request.form.get("guess")) + print(mn, mx) + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + elif answer == "high": + mx = int(request.form.get("guess")) + print(mn, mx) + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + # zobrazení výherní stránky + elif answer == "win": + return render_template("i_win.html") + + # první zobrazení hrací stránky (bez vstupu uživatele) + else: + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/4] guessing game 3/templates/guessing.html b/4] guessing game 3/templates/guessing.html new file mode 100644 index 0000000..3bc97a6 --- /dev/null +++ b/4] guessing game 3/templates/guessing.html @@ -0,0 +1,38 @@ + + + + + + Guessing GAME + + + +
+

GUESSING GAME

+ +

I guess {{ guess }}, it is right?

+ +
+ + + + + + +
+ + +
+ + \ No newline at end of file diff --git a/4] guessing game 3/templates/i_win.html b/4] guessing game 3/templates/i_win.html new file mode 100644 index 0000000..3ff7e76 --- /dev/null +++ b/4] guessing game 3/templates/i_win.html @@ -0,0 +1,32 @@ + + + + + + Guessing GAME + + + +
+

Yeah! I WIN!

+ +

Thank you for a game! Do you want to play again? +

+ +
+ +
+
+ + \ No newline at end of file diff --git a/4] guessing game 3/templates/index.html b/4] guessing game 3/templates/index.html new file mode 100644 index 0000000..84c07cb --- /dev/null +++ b/4] guessing game 3/templates/index.html @@ -0,0 +1,36 @@ + + + + + + Guessing GAME + + + +
+

LET'S PLAY A GUESSING GAME!

+ +

RULES: Think about a number in the range + 0 - 1000.
Then say me, if my guess is right, too + high or too low.

+ +
+ +
+
+ + \ No newline at end of file diff --git a/5] 2001/README.md b/5] 2001/README.md new file mode 100644 index 0000000..5fcda55 --- /dev/null +++ b/5] 2001/README.md @@ -0,0 +1,27 @@ +## Number guessing game 2 + +Aplikace pro výpočet hodu kostkou ve formátu xDy+z kde: +x = počet hodů kostkou (nepovinné) +D = sympol pro označení druhu kostky (povinné) +y = označení kolikastěnná je kostka (povinné) ++z = modifikátor, který označí matematickou operaci, která se má s výsledkem hodu provést - povoleno je +, - (nepovinné) + + +# Lauched application + +Use the "cd" command to navigate to the "5] 2001" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. Fukce přijme argument ve formátu strint a zkontroluje, zda byla použita správná kostka. Pokud ne, vrátí chybové hlášení. + +2. Pokud ano, funkce se pokusí získat i další hodnoty. Kontroluje, zda byly zadány správně a pokud ne, vrátí chybové hlášení. + +3. Na základně zjištěních hodnot funkce vypočítá výsledek a vrátí ho ve formě int. diff --git a/5] 2001/main.py b/5] 2001/main.py index e69de29..fb7107a 100644 --- a/5] 2001/main.py +++ b/5] 2001/main.py @@ -0,0 +1,69 @@ +from random import randint + + +def calculation(x: int, y: int) -> int: + """ + Vnořená funkce obsahující základní vzorec + pro výpočet hodu kostkou bez modifikátoru. + """ + return x * (randint(1, y+1)) + + +def dice_calculation(entered_value: str): + """ + Funkce pro výpočet hodu kostkou. Funkce zkontroluje + jestli byla zadána povolená kostka a správné hodnoty. + Pokud ano, vrátí hodnotu hodu za použití funkce + calculation(). Pokud ne, vrátí chybové hlášení. + """ + error_dice = "WRONG DICE: a correct dice - D3, D4, D6, D8, D10, D12, D20, D100" + error_value = "WRONG VALUE: a correct value is in format xDy+z." + correct_dice = ("D3", "D4", "D6", "D8", "D10", "D12", "D20", "D100") + + rolls = 0 + dice = 0 + modifier = 0 + math_mark = "" + + # získání typu kostky + kontrola + for correct_dice in correct_dice: + if correct_dice in entered_value: + dice = int(correct_dice.strip("D")) + first_cut = entered_value.split(correct_dice) + break + if dice == 0: + return error_dice + + # získání počtu hodů + kontrola + if first_cut[0]: + try: + rolls = int(first_cut[0]) + except ValueError: + return error_value + else: + rolls = 1 + + # získání případného modifikátoru + kontrola + if first_cut[1]: + correct_mark = ("+", "-") + for mark in correct_mark: + if mark in first_cut[1]: + math_mark = mark + try: + modifier = int(first_cut[1].strip(mark)) + break + except: + return error_value + if not math_mark: + return error_value + + # výpočet hodu s modifikátorem + if modifier: + return eval(str(calculation(rolls, dice)) + math_mark + str(modifier)) + + # výpočet hodu bez modifikátoru + else: + return calculation(rolls, dice) + + +print(dice_calculation("2D10+5")) \ No newline at end of file diff --git a/5] dice/README.md b/5] dice/README.md new file mode 100644 index 0000000..c545011 --- /dev/null +++ b/5] dice/README.md @@ -0,0 +1,23 @@ +## Number guessing game 2 + +A simple console application ... + + +# Lauched application + +Use the "cd" command to navigate to the "5] dice" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. ... + +2. ... + +3. ... diff --git a/mock_exam/task_1.py b/mock_exam/task_1.py new file mode 100644 index 0000000..22f540d --- /dev/null +++ b/mock_exam/task_1.py @@ -0,0 +1,15 @@ +def shorten(entered_str: str) -> str: + """ + Funkce přijme libovolně dlouhý řetězec a vrátí + řetězec obsahující první písmeno každého slova + ze zadného stringu. + """ + cut_str = entered_str.split() + short_str = list(word[0].upper() for word in cut_str) + + return "".join(short_str) + + +print(shorten("Don't repeat yourself")) +print(shorten("Read the fine manual")) +print(shorten("All terrain armoured transport")) diff --git a/mock_exam/task_2.py b/mock_exam/task_2.py new file mode 100644 index 0000000..2e3d18c --- /dev/null +++ b/mock_exam/task_2.py @@ -0,0 +1,24 @@ +def singulars_and_plurals(word_list: list) -> dict: + """ + Funkce přijme list slov, rozdělí je na jednotná + a množná a vráti slovník, který obsahuje seznamy + těchto slov. + """ + shorted_words = {"singulars": [], "plurals": []} + for word in word_list: + if word[-1] != "s": + shorted_words["singulars"].append(word) + else: + shorted_words["plurals"].append(word) + + return shorted_words + + +test_list = [ + "tomato", "tomatoes", + "potato", "potatoes", + "cars", "unicorns", + "horse", "cow" +] + +print(singulars_and_plurals(test_list)) diff --git a/mock_exam/task_3.py b/mock_exam/task_3.py new file mode 100644 index 0000000..3aa99b0 --- /dev/null +++ b/mock_exam/task_3.py @@ -0,0 +1,29 @@ +def check_palindrome(entered_str: str) -> bool: + """ + Funkce přijme řetězec a očistí ho o mezery a speciální + znaky a zkontroluje, jestli se jedná o palindrom. + """ + ## VERZE, KDE SE NEZOHLEĎNUJÍ ZNAMÉNKA VE VĚTÁCH + # cls_string = (entered_str.lower()).replace(" ", "") + # if cls_string == cls_string[::-1]: + # return True + # else: + # return False + + illegal_char = (" ", ",", ";", ".", "!", "?") + entered_str = entered_str.lower() + + for char in illegal_char: + if char in entered_str: + entered_str = entered_str.replace(char, "") + + if entered_str == entered_str[::-1]: + return True + else: + return False + + +print(check_palindrome("racecar")) +print(check_palindrome("level")) +print(check_palindrome("Was it a car or a cat I saw?")) +print(check_palindrome("return")) \ No newline at end of file diff --git a/mock_exam/task_4.py b/mock_exam/task_4.py new file mode 100644 index 0000000..1ac495f --- /dev/null +++ b/mock_exam/task_4.py @@ -0,0 +1,11 @@ +def div(num1: int, num2: int) -> list: + """ + Funkce přijme dvě čísla jako začátek a konec řady + a vrátí seznam těchto čísel, kterou jsou dělitelná + dvěma, ale ne třema. + """ + num_list = list(num for num in range(num1, num2 + 1) if num % 2 == 0 and num % 3 != 0) + return num_list + +print(div(0, 20)) + \ No newline at end of file diff --git a/mock_exam/task_5.py b/mock_exam/task_5.py new file mode 100644 index 0000000..f3ed78d --- /dev/null +++ b/mock_exam/task_5.py @@ -0,0 +1,14 @@ +from random import randint + + +def roll(throw, dice=6, modifier=0): + """ + Funkce přijímá jeden povinný argument (throw) a dva volitelné + argumenty. Vypočítá hodnotu jednotlivých hodů, sečte a + případně připočítá modifikátor a vrátí výsledek. + """ + throw_list = list(randint(1, dice+1) for _ in range(throw)) + return sum(throw_list) + modifier + + +print(roll(5, 7, 2)) \ No newline at end of file diff --git a/mock_exam/task_6/task_6.py b/mock_exam/task_6/task_6.py new file mode 100644 index 0000000..1e2e1c5 --- /dev/null +++ b/mock_exam/task_6/task_6.py @@ -0,0 +1,38 @@ +from flask import Flask, request, render_template + +app = Flask(__name__) + +@app.route("/movies", methods=["GET", "POST"]) +def movies(): + """ + Aplikace umožní uživateli zadat název filmu a následně + prohledá uložený seznam a zobrazí uživateli informaci, + jestli film patří mezi oblíbené či nikoliv. + """ + movies = { + "favourite": ["A New Hope", "Empire Strikes Back", "Return of the Jedi", + "The Force Awakens", "Jaws", "Predator", "Mad Max", + "Back to the Future", "2001: A Space Odyssey", "Robocop", + "The Hitchhiker's Guide to the Galaxy", "Doctor Who", + "Aliens", "Alien", "Terminator", "Blade Runner", "Matrix"], + + "hated": ["The Phantom Menace", "Attack of the Clones", "Star Trek", + "Alien Resurrection", "Twilight"] + + } + if request.method == "POST": + ask_movie = request.form["name_movie"] + if ask_movie in movies["favourite"]: + answer = "The movie is FAVOURITE!" + elif ask_movie in movies["hated"]: + answer = "The movie is HATED!" + else: + answer = "No such movie." + + return render_template("index.html", answer=answer) + else: + return render_template("index.html") + + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/mock_exam/task_6/templates/index.html b/mock_exam/task_6/templates/index.html new file mode 100644 index 0000000..0455487 --- /dev/null +++ b/mock_exam/task_6/templates/index.html @@ -0,0 +1,26 @@ + + + + + + Movies + + + +
+

MOVIE ANALYZER

+
+ + +
+ +

{{ answer }}

+
+ + \ No newline at end of file