diff --git a/README.md b/README.md index 012bd27..e0fbb37 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -# 5-Python-Projects-For-Beginners \ No newline at end of file +# 5-Python-Projects-For-Beginners + +Első commit tartalma +Második commit \ No newline at end of file diff --git a/divisibility.py b/divisibility.py new file mode 100644 index 0000000..78b7e5e --- /dev/null +++ b/divisibility.py @@ -0,0 +1,8 @@ +# Python Program to Check Number is Divisible by 5 and 11 +def divisibility(): + number = int(input( " Please Enter any Positive Integer : ")) + + if((number % 5 == 0) and (number % 11 == 0)): + print("Given Number {0} is Divisible by 5 and 11".format(number)) + else: + print("Given Number {0} is Not Divisible by 5 and 11".format(number)) \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..28390c8 --- /dev/null +++ b/game.py @@ -0,0 +1,32 @@ +import rps_match +from rps_match import options + + +def game(): + user_wins = 0 + computer_wins = 0 + + while True: + user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower() + if user_input == "q": + break + + if user_input not in options: + continue + + result = rps_match.rps_match(user_input) + + if result == "user_wins": + print("You won!") + user_wins += 1 + + elif result == "computer_wins": + print("You lost!") + computer_wins += 1 + else: + print("Nobody wins!") + + print("You won", user_wins, "times.") + print("The computer won", computer_wins, "times.") + print("Goodbye!") + diff --git a/leap_year.py b/leap_year.py new file mode 100644 index 0000000..97cd1d1 --- /dev/null +++ b/leap_year.py @@ -0,0 +1,13 @@ +#függvény, aminek egy bemenete van, és egy kimenete, a bemenet az évszám, +#kimenet pedig igaz vagy hamis, igaz, ha szökőév, hamis, ha nem az. + +def leap_year(year: int): + if year % 400 == 0: + return True + elif year % 100 == 0: + return False + elif year % 4 == 0: + return True + else: + return False + diff --git a/monthly_payment_calculator.py b/monthly_payment_calculator.py new file mode 100644 index 0000000..ea18446 --- /dev/null +++ b/monthly_payment_calculator.py @@ -0,0 +1,6 @@ +# calculate monthly payment +def calculate(p: int, months: int, rate: float): + return (rate/12) * (1/(1-(1+rate/12)**(-months)))*p + + + diff --git a/rock_paper_scissors.py b/rock_paper_scissors.py index 3249fe4..e39ba42 100644 --- a/rock_paper_scissors.py +++ b/rock_paper_scissors.py @@ -1,39 +1,3 @@ -import random +import game -user_wins = 0 -computer_wins = 0 - -options = ["rock", "paper", "scissors"] - -while True: - user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower() - if user_input == "q": - break - - if user_input not in options: - continue - - random_number = random.randint(0, 2) - # rock: 0, paper: 1, scissors: 2 - computer_pick = options[random_number] - print("Computer picked", computer_pick + ".") - - if user_input == "rock" and computer_pick == "scissors": - print("You won!") - user_wins += 1 - - elif user_input == "paper" and computer_pick == "rock": - print("You won!") - user_wins += 1 - - elif user_input == "scissors" and computer_pick == "paper": - print("You won!") - user_wins += 1 - - else: - print("You lost!") - computer_wins += 1 - -print("You won", user_wins, "times.") -print("The computer won", computer_wins, "times.") -print("Goodbye!") +game.game() diff --git a/rps_match.py b/rps_match.py new file mode 100644 index 0000000..5e3d5cb --- /dev/null +++ b/rps_match.py @@ -0,0 +1,24 @@ +import random + +options = ["rock", "paper", "scissors"] + + +def rps_match(user_input_param): + + random_number = random.randint(0, 2) + # rock: 0, paper: 1, scissors: 2 + computer_pick = options[random_number] + print("Computer picked", computer_pick + ".") + + if user_input_param == "rock" and computer_pick == "scissors": + return "user_wins" + + elif user_input_param == "paper" and computer_pick == "rock": + return "user_wins" + + elif user_input_param == "scissors" and computer_pick == "paper": + return "user_wins" + elif user_input_param == computer_pick: + return "nobody wins" + else: + return "computer_wins" diff --git a/test_divisibility.py b/test_divisibility.py new file mode 100644 index 0000000..28fbbfb --- /dev/null +++ b/test_divisibility.py @@ -0,0 +1,26 @@ +from tud_test_base import set_keyboard_input, get_display_output +from divisibility import divisibility + + +def test_divisibility_none(): + set_keyboard_input([3]) + divisibility() + assert get_display_output() == [' Please Enter any Positive Integer : ', "Given Number 3 is Not Divisible by 5 and 11"] + + +def test_divisibility_only_5(): + set_keyboard_input([15]) + divisibility() + assert get_display_output() == [' Please Enter any Positive Integer : ', "Given Number 15 is Not Divisible by 5 and 11"] + + +def test_divisibility_only_11(): + set_keyboard_input([22]) + divisibility() + assert get_display_output() == [' Please Enter any Positive Integer : ', "Given Number 22 is Not Divisible by 5 and 11"] + + +def test_divisibility_both(): + set_keyboard_input([55]) + divisibility() + assert get_display_output() == [' Please Enter any Positive Integer : ', "Given Number 55 is Divisible by 5 and 11"] diff --git a/test_game.py b/test_game.py new file mode 100644 index 0000000..9a0bc83 --- /dev/null +++ b/test_game.py @@ -0,0 +1,11 @@ +from tud_test_base import set_keyboard_input, get_display_output +import game + + +def test_game(): + + set_keyboard_input(["paper"]) + game.game() + output = get_display_output() + + assert output == ["valami","valami"] diff --git a/test_leap_year.py b/test_leap_year.py new file mode 100644 index 0000000..7312a68 --- /dev/null +++ b/test_leap_year.py @@ -0,0 +1,25 @@ +from leap_year import leap_year + + +def test_leap_year_divisible_by_4(): + assert leap_year(4) == True + assert leap_year(8) == True + assert leap_year(12) == True + assert leap_year(16) == True + + +def test_leap_year_divisible_by_100(): + assert leap_year(100) == False + assert leap_year(200) == False + assert leap_year(500) == False + assert leap_year(1000) == False + + +def test_leap_year_divisible_by_400(): + assert leap_year(400) == True + assert leap_year(800) == True + assert leap_year(1200) == True + assert leap_year(1600) == True + assert leap_year(2000) == True + + diff --git a/test_monthly_payment_calculator.py b/test_monthly_payment_calculator.py new file mode 100644 index 0000000..dc0be98 --- /dev/null +++ b/test_monthly_payment_calculator.py @@ -0,0 +1,29 @@ +import pytest + +from monthly_payment_calculator import calculate + + +def test_calculate_egymillio_negyev_tizszazalek(): + assert round(calculate(1000000, 48, 0.1)) == 25363 + + +def test_calculate_negymillio_tizev_tizszazalek(): + assert round(calculate(4000000, 120, 0.1)) == 52860 + + +def test_calculate_hárommillio_ötev_tizenketszazalek(): + assert round(calculate(3000000, 60, 0.12)) == 66733 + + +def test_calculate_nulla_negyev_tizszazalek(): + assert round(calculate(0, 48, 0.1)) == 0 + + +def test_calculate_egymillio_negyev_nullaszazalek(): + with pytest.raises(ZeroDivisionError): + assert round(calculate(1000000, 48, 0)) == 20833 + + +def test_calculate_egymillio_nulla_tizszazalek(): + with pytest.raises(ZeroDivisionError): + assert round(calculate(1000000, 0, 0.1)) == 25363 \ No newline at end of file diff --git a/test_rock_paper_scissors.py b/test_rock_paper_scissors.py new file mode 100644 index 0000000..ee227c6 --- /dev/null +++ b/test_rock_paper_scissors.py @@ -0,0 +1,50 @@ +import rps_match +import random + +from unittest.mock import MagicMock + + +def test_rps_match_paper_rock(): + random.randint = MagicMock(return_value=0) + assert rps_match.rps_match("paper") == "user_wins" + + +def test_rps_match_paper_paper(): + random.randint = MagicMock(return_value=1) + assert rps_match.rps_match("paper") == "nobody wins" + + +def test_rps_match_paper_scissors(): + random.randint = MagicMock(return_value=2) + assert rps_match.rps_match("paper") == "computer_wins" + + +def test_rps_match_scissors_rock(): + random.randint = MagicMock(return_value=0) + assert rps_match.rps_match("scissors") == "computer_wins" + + +def test_rps_match_scissors_paper(): + random.randint = MagicMock(return_value=1) + assert rps_match.rps_match("scissors") == "user_wins" + + +def test_rps_match_scissors_scissors(): + random.randint = MagicMock(return_value=2) + assert rps_match.rps_match("scissors") == "nobody wins" + + +def test_rps_match_rock_rock(): + random.randint = MagicMock(return_value=0) + assert rps_match.rps_match("rock") == "nobody wins" + + +def test_rps_match_rock_paper(): + random.randint = MagicMock(return_value=1) + assert rps_match.rps_match("rock") == "computer_wins" + + +def test_rps_match_rock_scissors(): + random.randint = MagicMock(return_value=2) + assert rps_match.rps_match("rock") == "user_wins" + diff --git a/tud_test_base.py b/tud_test_base.py new file mode 100644 index 0000000..ad6b3e3 --- /dev/null +++ b/tud_test_base.py @@ -0,0 +1,32 @@ +import builtins + +input_values = [] +print_values = [] + + +def mock_input(s): + print_values.append(s) + return input_values.pop(0) + + +def mock_input_output_start(): + global input_values, print_values + + input_values = [] + print_values = [] + + builtins.input = mock_input + builtins.print = lambda s: print_values.append(s) + + +def get_display_output(): + global print_values + return print_values + + +def set_keyboard_input(mocked_inputs): + global input_values + + mock_input_output_start() + input_values = mocked_inputs +