From 5206d59c895f56074bf0e828dc61dd2ddb16220d Mon Sep 17 00:00:00 2001 From: AdePhil Date: Mon, 2 Oct 2023 11:45:21 +0100 Subject: [PATCH 1/7] feat: add gas station --- greedy_methods/gas_station.py | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 greedy_methods/gas_station.py diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py new file mode 100644 index 000000000000..f6fef621fc95 --- /dev/null +++ b/greedy_methods/gas_station.py @@ -0,0 +1,66 @@ +""" +Task: +There are n gas stations along a circular route, where the amount of gas +at the ith station is gas[i]. + +You have a car with an unlimited gas tank and it costs cost[i] of gas +to travel from the ith station to its next (i + 1)th station. You begin the +journey with an empty tank at one of the gas stations. + +Given two integer arrays gas and cost, return the starting gas station's index +if you can travel around the circuit once in the clockwise direction, +otherwise return -1. If there exists a solution, it is guaranteed to be unique + +Reference: https://leetcode.com/problems/gas-station/description + +Implementation notes: +First, check whether the total gas is enough to complete the journey. If not, return -1. +However, if there is enough gas, it is guaranteed that there is a valid +starting index to reach the end of the journey. +Greedily calculate the net gain (gas - cost) at each station. +If the net gain ever goes below 0 while iterating through the stations, +start checking from the next station. + +""" + + +def can_complete_journey(gas: list[int], cost: list[int]) -> int: + """ + This function returns the index from which to start the journey + in order to reach the end. + + Args: + gas [list]: Amount of gas available at each station + cost [list]: The cost of gas required to move from a station to the next + + Returns: + start [int]: start index needed to complete the journey + + Examples: + >>> can_complete_journey([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) + 3 + >>> can_complete_journey([2, 3, 4], [3, 4, 3]) + -1 + + """ + total_gas = sum(gas) + total_cost = sum(cost) + + if total_gas < total_cost: + return -1 + + start = 0 + net = 0 + for i in range(len(gas)): + net += gas[i] - cost[i] + if net < 0: + start = i + 1 + net = 0 + + return start + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 736da2c254aded92ff1a8925b471f2ef2707819b Mon Sep 17 00:00:00 2001 From: AdePhil Date: Thu, 5 Oct 2023 13:38:06 +0100 Subject: [PATCH 2/7] make code more readable make code more readable --- greedy_methods/gas_station.py | 57 ++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index f6fef621fc95..b643057fa206 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -1,15 +1,18 @@ +from dataclasses import dataclass + """ Task: There are n gas stations along a circular route, where the amount of gas -at the ith station is gas[i]. +at the ith station is gas_quantities[i]. -You have a car with an unlimited gas tank and it costs cost[i] of gas -to travel from the ith station to its next (i + 1)th station. You begin the -journey with an empty tank at one of the gas stations. +You have a car with an unlimited gas tank and it costs costs[i] of gas +to travel from the ith station to its next (i + 1)th station. +You begin the journey with an empty tank at one of the gas stations. -Given two integer arrays gas and cost, return the starting gas station's index -if you can travel around the circuit once in the clockwise direction, -otherwise return -1. If there exists a solution, it is guaranteed to be unique +Given two integer arrays gas_quantities and costs, return the starting +gas station's index if you can travel around the circuit once +in the clockwise direction, otherwise return -1. +If there exists a solution, it is guaranteed to be unique Reference: https://leetcode.com/problems/gas-station/description @@ -17,20 +20,44 @@ First, check whether the total gas is enough to complete the journey. If not, return -1. However, if there is enough gas, it is guaranteed that there is a valid starting index to reach the end of the journey. -Greedily calculate the net gain (gas - cost) at each station. +Greedily calculate the net gain (gas_quantity - cost) at each station. If the net gain ever goes below 0 while iterating through the stations, start checking from the next station. """ -def can_complete_journey(gas: list[int], cost: list[int]) -> int: +@dataclass +class GasStation: + gas_quantity: int + cost: int + + +def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> list[GasStation]: + """ + This function returns a list of gas stations. + + Args: + gas_quantities [list]: Amount of gas available at each station + cost [list]: The cost of gas required to move from a station to the next + + Returns: + gas_stations [list]: a list of gas stations + """ + gas_stations = [ + GasStation(gas_quantity, cost) + for (gas_quantity, cost) in zip(gas_quantities, costs) + ] + return gas_stations + + +def can_complete_journey(gas_quantities: list[int], costs: list[int]) -> int: """ This function returns the index from which to start the journey in order to reach the end. Args: - gas [list]: Amount of gas available at each station + gas_quantities [list]: Amount of gas available at each station cost [list]: The cost of gas required to move from a station to the next Returns: @@ -43,16 +70,18 @@ def can_complete_journey(gas: list[int], cost: list[int]) -> int: -1 """ - total_gas = sum(gas) - total_cost = sum(cost) + total_gas = sum(gas_quantities) + total_cost = sum(costs) if total_gas < total_cost: return -1 start = 0 net = 0 - for i in range(len(gas)): - net += gas[i] - cost[i] + gas_stations = get_gas_stations(gas_quantities, costs) + + for i, gas_station in enumerate(gas_stations): + net += gas_station.gas_quantity - gas_station.cost if net < 0: start = i + 1 net = 0 From 06acf547c114d4d91dc78e025354172ae42d171b Mon Sep 17 00:00:00 2001 From: AdePhil Date: Thu, 5 Oct 2023 14:01:06 +0100 Subject: [PATCH 3/7] update test --- greedy_methods/gas_station.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index b643057fa206..49a76a24575b 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -43,6 +43,12 @@ def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> list[GasSta Returns: gas_stations [list]: a list of gas stations + + Examples: + >>> get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) + [GasStation(gas_quantity=1, cost=3), GasStation(gas_quantity=2, cost=4), \ +GasStation(gas_quantity=3, cost=5), GasStation(gas_quantity=4, cost=1), \ +GasStation(gas_quantity=5, cost=2)] """ gas_stations = [ GasStation(gas_quantity, cost) From 02073549e1b96df92d5e9b5156ae13b9055f8ee5 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 5 Oct 2023 15:08:30 +0200 Subject: [PATCH 4/7] Update gas_station.py --- greedy_methods/gas_station.py | 54 ++++++++++++++++------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index 49a76a24575b..b0b84c136be3 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -1,5 +1,3 @@ -from dataclasses import dataclass - """ Task: There are n gas stations along a circular route, where the amount of gas @@ -11,7 +9,7 @@ Given two integer arrays gas_quantities and costs, return the starting gas station's index if you can travel around the circuit once -in the clockwise direction, otherwise return -1. +in the clockwise direction otherwise, return -1. If there exists a solution, it is guaranteed to be unique Reference: https://leetcode.com/problems/gas-station/description @@ -25,6 +23,7 @@ start checking from the next station. """ +from dataclasses import dataclass @dataclass @@ -33,65 +32,60 @@ class GasStation: cost: int -def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> list[GasStation]: +def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasStation]: """ - This function returns a list of gas stations. + This function returns a tuple of gas stations. Args: - gas_quantities [list]: Amount of gas available at each station - cost [list]: The cost of gas required to move from a station to the next + gas_quantities: Amount of gas available at each station + costs: The cost of gas required to move from one station to the next Returns: - gas_stations [list]: a list of gas stations - - Examples: - >>> get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) - [GasStation(gas_quantity=1, cost=3), GasStation(gas_quantity=2, cost=4), \ -GasStation(gas_quantity=3, cost=5), GasStation(gas_quantity=4, cost=1), \ -GasStation(gas_quantity=5, cost=2)] + A tuple of gas stations + + >>> gas_stations = get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) + >>> len(gas_stations) + 5 + >>> gas_stations[0] + GasStation(gas_quantity=1, cost=3) + >>> gas_stations[-1] + GasStation(gas_quantity=5, cost=2) """ - gas_stations = [ - GasStation(gas_quantity, cost) - for (gas_quantity, cost) in zip(gas_quantities, costs) - ] - return gas_stations + return tuple( + GasStation(quantity, cost) for quantity, cost in zip(gas_quantities, costs) + ) -def can_complete_journey(gas_quantities: list[int], costs: list[int]) -> int: +def can_complete_journey(gas_stations: tuple[GasStation]) -> int: """ This function returns the index from which to start the journey in order to reach the end. Args: gas_quantities [list]: Amount of gas available at each station - cost [list]: The cost of gas required to move from a station to the next + cost [list]: The cost of gas required to move from one station to the next Returns: start [int]: start index needed to complete the journey Examples: - >>> can_complete_journey([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) + >>> can_complete_journey(get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])) 3 - >>> can_complete_journey([2, 3, 4], [3, 4, 3]) + >>> can_complete_journey(get_gas_stations([2, 3, 4], [3, 4, 3])) -1 - """ - total_gas = sum(gas_quantities) - total_cost = sum(costs) - + total_gas = sum(gas_station.gas_quantity for gas_station in gas_stations) + total_cost = sum(gas_station.cost for gas_station in gas_stations) if total_gas < total_cost: return -1 start = 0 net = 0 - gas_stations = get_gas_stations(gas_quantities, costs) - for i, gas_station in enumerate(gas_stations): net += gas_station.gas_quantity - gas_station.cost if net < 0: start = i + 1 net = 0 - return start From 4aa558e7fa3489613aa3e55e8bfdbb4155ff46d3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:09:06 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/gas_station.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index b0b84c136be3..c2a15fcf5011 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -42,7 +42,7 @@ def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasSt Returns: A tuple of gas stations - + >>> gas_stations = get_gas_stations([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) >>> len(gas_stations) 5 From 3006ec973cc39426d548ec50b9c7a07f56dd4ae1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 5 Oct 2023 15:14:10 +0200 Subject: [PATCH 6/7] tuple[GasStation, ...] --- greedy_methods/gas_station.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index c2a15fcf5011..ba7cf92c0afd 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -32,7 +32,7 @@ class GasStation: cost: int -def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasStation]: +def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasStation, ...]: """ This function returns a tuple of gas stations. @@ -56,7 +56,7 @@ def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasSt ) -def can_complete_journey(gas_stations: tuple[GasStation]) -> int: +def can_complete_journey(gas_stations: tuple[GasStation, ...]) -> int: """ This function returns the index from which to start the journey in order to reach the end. From a1ac025d7d66cb96b8b1a73f49e3b748be42106d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:15:08 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/gas_station.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/greedy_methods/gas_station.py b/greedy_methods/gas_station.py index ba7cf92c0afd..2427375d2664 100644 --- a/greedy_methods/gas_station.py +++ b/greedy_methods/gas_station.py @@ -32,7 +32,9 @@ class GasStation: cost: int -def get_gas_stations(gas_quantities: list[int], costs: list[int]) -> tuple[GasStation, ...]: +def get_gas_stations( + gas_quantities: list[int], costs: list[int] +) -> tuple[GasStation, ...]: """ This function returns a tuple of gas stations.