diff --git a/solutions/python/currency-exchange/1/exchange.py b/solutions/python/currency-exchange/1/exchange.py new file mode 100644 index 0000000000..2ddb82f503 --- /dev/null +++ b/solutions/python/currency-exchange/1/exchange.py @@ -0,0 +1,79 @@ +"""Functions for calculating steps in exchanging currency. + +Python numbers documentation: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex + +Overview of exchanging currency when travelling: https://www.compareremit.com/money-transfer-tips/guide-to-exchanging-currency-for-overseas-travel/ +""" + + + +def exchange_money(budget, exchange_rate): + """ + + :param budget: float - amount of money you are planning to exchange. + :param exchange_rate: float - unit value of the foreign currency. + :return: float - exchanged value of the foreign currency you can receive. + """ + + return budget / exchange_rate + + +def get_change(budget, exchanging_value): + """ + + :param budget: float - amount of money you own. + :param exchanging_value: float - amount of your money you want to exchange now. + :return: float - amount left of your starting currency after exchanging. + """ + + return budget - exchanging_value + + +def get_value_of_bills(denomination, number_of_bills): + """ + + :param denomination: int - the value of a bill. + :param number_of_bills: int - total number of bills. + :return: int - calculated value of the bills. + """ + + return denomination * number_of_bills + + +def get_number_of_bills(amount, denomination): + """ + + :param amount: float - the total starting value. + :param denomination: int - the value of a single bill. + :return: int - number of bills that can be obtained from the amount. + """ + + return amount // denomination + + +def get_leftover_of_bills(amount, denomination): + """ + + :param amount: float - the total starting value. + :param denomination: int - the value of a single bill. + :return: float - the amount that is "leftover", given the current denomination. + """ + + return amount % denomination + + +def exchangeable_value(budget, exchange_rate, spread, denomination): + """ + + :param budget: float - the amount of your money you are planning to exchange. + :param exchange_rate: float - the unit value of the foreign currency. + :param spread: int - percentage that is taken as an exchange fee. + :param denomination: int - the value of a single bill. + :return: int - maximum value you can get. + """ + + spread_decimal = spread / 100 + effective_rate = exchange_rate * (1 + spread_decimal) + exchanged_amount = budget / effective_rate + leftover = get_leftover_of_bills(exchanged_amount, denomination) + return int(exchanged_amount - leftover) diff --git a/solutions/python/ghost-gobble-arcade-game/1/arcade_game.py b/solutions/python/ghost-gobble-arcade-game/1/arcade_game.py new file mode 100644 index 0000000000..07cf976900 --- /dev/null +++ b/solutions/python/ghost-gobble-arcade-game/1/arcade_game.py @@ -0,0 +1,57 @@ +"""Functions for implementing the rules of the classic arcade game Pac-Man.""" + + +def eat_ghost(power_pellet_active, touching_ghost): + """Verify that Pac-Man can eat a ghost if he is empowered by a power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - can a ghost be eaten? + """ + + if power_pellet_active and touching_ghost: + return True + else: + return False + +def score(touching_power_pellet, touching_dot): + """Verify that Pac-Man has scored when a power pellet or dot has been eaten. + + :param touching_power_pellet: bool - is the player touching a power pellet? + :param touching_dot: bool - is the player touching a dot? + :return: bool - has the player scored or not? + """ + + if touching_power_pellet or touching_dot: + return True + else: + return False + + +def lose(power_pellet_active, touching_ghost): + """Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player lost the game? + """ + + if touching_ghost and (power_pellet_active == False): + return True + else: + return False + + +def win(has_eaten_all_dots, power_pellet_active, touching_ghost): + """Trigger the victory event when all dots have been eaten. + + :param has_eaten_all_dots: bool - has the player "eaten" all the dots? + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player won the game? + """ + + if has_eaten_all_dots and (lose(power_pellet_active, touching_ghost) == False): + return True + else: + return False diff --git a/solutions/python/ghost-gobble-arcade-game/2/arcade_game.py b/solutions/python/ghost-gobble-arcade-game/2/arcade_game.py new file mode 100644 index 0000000000..2b0598b14e --- /dev/null +++ b/solutions/python/ghost-gobble-arcade-game/2/arcade_game.py @@ -0,0 +1,44 @@ +"""Functions for implementing the rules of the classic arcade game Pac-Man.""" + + +def eat_ghost(power_pellet_active, touching_ghost): + """Verify that Pac-Man can eat a ghost if he is empowered by a power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - can a ghost be eaten? + """ + + return power_pellet_active and touching_ghost + +def score(touching_power_pellet, touching_dot): + """Verify that Pac-Man has scored when a power pellet or dot has been eaten. + + :param touching_power_pellet: bool - is the player touching a power pellet? + :param touching_dot: bool - is the player touching a dot? + :return: bool - has the player scored or not? + """ + + return touching_power_pellet or touching_dot + + +def lose(power_pellet_active, touching_ghost): + """Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player lost the game? + """ + + return touching_ghost and (power_pellet_active == False) + +def win(has_eaten_all_dots, power_pellet_active, touching_ghost): + """Trigger the victory event when all dots have been eaten. + + :param has_eaten_all_dots: bool - has the player "eaten" all the dots? + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player won the game? + """ + + return has_eaten_all_dots and (lose(power_pellet_active, touching_ghost) == False) diff --git a/solutions/python/ghost-gobble-arcade-game/3/arcade_game.py b/solutions/python/ghost-gobble-arcade-game/3/arcade_game.py new file mode 100644 index 0000000000..38501a80f4 --- /dev/null +++ b/solutions/python/ghost-gobble-arcade-game/3/arcade_game.py @@ -0,0 +1,44 @@ +"""Functions for implementing the rules of the classic arcade game Pac-Man.""" + + +def eat_ghost(power_pellet_active, touching_ghost): + """Verify that Pac-Man can eat a ghost if he is empowered by a power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - can a ghost be eaten? + """ + + return power_pellet_active and touching_ghost + +def score(touching_power_pellet, touching_dot): + """Verify that Pac-Man has scored when a power pellet or dot has been eaten. + + :param touching_power_pellet: bool - is the player touching a power pellet? + :param touching_dot: bool - is the player touching a dot? + :return: bool - has the player scored or not? + """ + + return touching_power_pellet or touching_dot + + +def lose(power_pellet_active, touching_ghost): + """Trigger the game loop to end (GAME OVER) when Pac-Man touches a ghost without his power pellet. + + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player lost the game? + """ + + return touching_ghost and (power_pellet_active is False) + +def win(has_eaten_all_dots, power_pellet_active, touching_ghost): + """Trigger the victory event when all dots have been eaten. + + :param has_eaten_all_dots: bool - has the player "eaten" all the dots? + :param power_pellet_active: bool - does the player have an active power pellet? + :param touching_ghost: bool - is the player touching a ghost? + :return: bool - has the player won the game? + """ + + return has_eaten_all_dots and (lose(power_pellet_active, touching_ghost) is False) diff --git a/solutions/python/guidos-gorgeous-lasagna/1/lasagna.py b/solutions/python/guidos-gorgeous-lasagna/1/lasagna.py new file mode 100644 index 0000000000..a628118a7e --- /dev/null +++ b/solutions/python/guidos-gorgeous-lasagna/1/lasagna.py @@ -0,0 +1,52 @@ +"""Functions used in preparing Guido's gorgeous lasagna. + +Learn about Guido, the creator of the Python language: +https://en.wikipedia.org/wiki/Guido_van_Rossum + +This is a module docstring, used to describe the functionality +of a module and its functions and/or classes. +""" + +EXPECTED_BAKE_TIME = 40 +PREPARATION_TIME = 2 + + +def bake_time_remaining(elapsed_bake_time): + """Calculate the bake time remaining. + + :param elapsed_bake_time: int - baking time already elapsed. + :return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'. + + Function that takes the actual minutes the lasagna has been in the oven as + an argument and returns how many minutes the lasagna still needs to bake + based on the `EXPECTED_BAKE_TIME`. + """ + return EXPECTED_BAKE_TIME - elapsed_bake_time + +def preparation_time_in_minutes(number_of_layers): + """Calculate the preperation time based on layers. + + :param number_of_layers: int - the number of layers. + :return: int - the preperation time it takes in minutes based on number of layers and PREPARATION_TIME. + + Function that takes the number of layers we want to add to the lasagna and returns how many minutes we would spend making them. + """ + + return number_of_layers * PREPARATION_TIME + +def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time): + + """Calculate the elapsed cooking time. + + :param number_of_layers: int - the number of layers in the lasagna. + :param elapsed_bake_time: int - elapsed cooking time. + :return: int - total time elapsed (in minutes) preparing and cooking. + + This function takes two integers representing the number of lasagna layers and the + time already spent baking and calculates the total elapsed minutes spent cooking the + lasagna. + """ + + return preparation_time_in_minutes(number_of_layers) + elapsed_bake_time + + diff --git a/solutions/python/guidos-gorgeous-lasagna/2/lasagna.py b/solutions/python/guidos-gorgeous-lasagna/2/lasagna.py new file mode 100644 index 0000000000..b7531c5415 --- /dev/null +++ b/solutions/python/guidos-gorgeous-lasagna/2/lasagna.py @@ -0,0 +1,50 @@ +"""Functions used in preparing Guido's gorgeous lasagna. + +Learn about Guido, the creator of the Python language: +https://en.wikipedia.org/wiki/Guido_van_Rossum + +This is a module docstring, used to describe the functionality +of a module and its functions and/or classes. +""" + +EXPECTED_BAKE_TIME = 40 +PREPARATION_TIME = 2 + + +def bake_time_remaining(elapsed_bake_time): + """Calculate the bake time remaining. + + :param elapsed_bake_time: int - baking time already elapsed. + :return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'. + + Function that takes the actual minutes the lasagna has been in the oven as + an argument and returns how many minutes the lasagna still needs to bake + based on the `EXPECTED_BAKE_TIME`. + """ + return EXPECTED_BAKE_TIME - elapsed_bake_time + +def preparation_time_in_minutes(number_of_layers): + """Calculate the preperation time based on layers. + + :param number_of_layers: int - the number of layers. + :return: int - the preperation time it takes in minutes based on number of layers and PREPARATION_TIME. + + Function that takes the number of layers we want to add to the lasagna and returns how many minutes we would spend making them. + """ + + return number_of_layers * PREPARATION_TIME + +def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time): + + """Calculate the elapsed cooking time. + + :param number_of_layers: int - the number of layers in the lasagna. + :param elapsed_bake_time: int - elapsed cooking time. + :return: int - total time elapsed (in minutes) preparing and cooking. + + This function takes two integers representing the number of lasagna layers and the + time already spent baking and calculates the total elapsed minutes spent cooking the + lasagna. + """ + + return preparation_time_in_minutes(number_of_layers) + elapsed_bake_time diff --git a/solutions/python/hello-world/1/hello_world.py b/solutions/python/hello-world/1/hello_world.py new file mode 100644 index 0000000000..426118d733 --- /dev/null +++ b/solutions/python/hello-world/1/hello_world.py @@ -0,0 +1,2 @@ +def hello(): + return "Hello, World!"