From f9c1432c3c65d1ff99b6931d9a0d5e4be44f09c6 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 1 Nov 2022 12:40:44 +0400 Subject: [PATCH 01/17] add dp up - down minimum cost for tickets --- dynamic_programming/minimum_tickets_cost.py | 55 +++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 dynamic_programming/minimum_tickets_cost.py diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py new file mode 100644 index 000000000000..7fad7009c8f4 --- /dev/null +++ b/dynamic_programming/minimum_tickets_cost.py @@ -0,0 +1,55 @@ +""" +Author : Alexander Pantyukhin +Date : November 1, 2022 + +Task: +Given a list of days when you need to travel. Each day is int from 1 to 365. +You are able to use tickets for 1 day, 7 days and 30 days. +Each ticket has a cost. + +Find the minimum cost you need to travel every day in the given list of days. + +Implementation notes: +implementation Dynamic Programming up bottom approach. + +The implementation was tested on the +leetcode: https://leetcode.com/problems/minimum-cost-for-tickets/ +""" + +from typing import List + +""" +Minimum Cost For Tickets +Dynamic Programming: up -> down. +""" +def mincost_tickets(days: List[int], costs: List[int]) -> int: + """ + >>> mincost_tickets([1,4,6,7,8,20], [2,7,15]) + 11 + >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,7,15]) + 17 + >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) + 24 + """ + + from functools import lru_cache + + days_set = set(days) + + @lru_cache(maxsize=None) + def dp(index): + if index > 365: + return 0 + + if index not in days_set: + return dp(index + 1) + + return min(costs[0] + dp(index + 1), + costs[1] + dp(index + 7), + costs[2] + dp(index + 30)) + + return dp(1) + +if __name__ == "__main__": + import doctest + doctest.testmod() From e8875b41c95aeb4be346291d44e70e8a6aec9411 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 1 Nov 2022 12:43:41 +0400 Subject: [PATCH 02/17] add typints --- dynamic_programming/minimum_tickets_cost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 7fad7009c8f4..61a9571ee355 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -37,7 +37,7 @@ def mincost_tickets(days: List[int], costs: List[int]) -> int: days_set = set(days) @lru_cache(maxsize=None) - def dp(index): + def dp(index: int) -> int: if index > 365: return 0 From 9802915f759b8726e7bb2e400b180534e02fb843 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Nov 2022 08:44:55 +0000 Subject: [PATCH 03/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/minimum_tickets_cost.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 61a9571ee355..73c26fe99801 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -5,7 +5,7 @@ Task: Given a list of days when you need to travel. Each day is int from 1 to 365. You are able to use tickets for 1 day, 7 days and 30 days. -Each ticket has a cost. +Each ticket has a cost. Find the minimum cost you need to travel every day in the given list of days. @@ -22,7 +22,9 @@ Minimum Cost For Tickets Dynamic Programming: up -> down. """ -def mincost_tickets(days: List[int], costs: List[int]) -> int: + + +def mincost_tickets(days: list[int], costs: list[int]) -> int: """ >>> mincost_tickets([1,4,6,7,8,20], [2,7,15]) 11 @@ -44,12 +46,16 @@ def dp(index: int) -> int: if index not in days_set: return dp(index + 1) - return min(costs[0] + dp(index + 1), - costs[1] + dp(index + 7), - costs[2] + dp(index + 30)) - + return min( + costs[0] + dp(index + 1), + costs[1] + dp(index + 7), + costs[2] + dp(index + 30), + ) + return dp(1) + if __name__ == "__main__": import doctest + doctest.testmod() From c33931dab9fd7e0b9a2f327ca92716cd6c7173e2 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 11:46:44 +0400 Subject: [PATCH 04/17] add new tests and checks. --- dynamic_programming/minimum_tickets_cost.py | 27 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 61a9571ee355..0cafcf21f721 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -3,7 +3,7 @@ Date : November 1, 2022 Task: -Given a list of days when you need to travel. Each day is int from 1 to 365. +Given a list of days when you need to travel. Each day is positive integer. You are able to use tickets for 1 day, 7 days and 30 days. Each ticket has a cost. @@ -12,6 +12,8 @@ Implementation notes: implementation Dynamic Programming up bottom approach. +Runtime complexity: O(n) + The implementation was tested on the leetcode: https://leetcode.com/problems/minimum-cost-for-tickets/ """ @@ -30,15 +32,34 @@ def mincost_tickets(days: List[int], costs: List[int]) -> int: 17 >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) 24 + >>> mincost_tickets([-1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) + Traceback (most recent call last): + ... + ValueError: All days elements should be greater than 0 + >>> mincost_tickets([2,3,4,5,6,7,8,9,10,30,31], []) + Traceback (most recent call last): + ... + ValueError: The lengths of costs should be equal 3 + >>> mincost_tickets([2,3,4,5,6,7,8,9,10,30,31], [1,2,3,4]) + Traceback (most recent call last): + ... + ValueError: The lengths of costs should be equal 3 """ from functools import lru_cache + if len(costs) != 3: + raise ValueError('The lengths of costs should be equal 3') + + if min(days) <= 0: + raise ValueError('All days elements should be greater than 0') + days_set = set(days) + max_days = max(days) @lru_cache(maxsize=None) - def dp(index: int) -> int: - if index > 365: + def dp(index): + if index > max_days: return 0 if index not in days_set: From 0417cb969f757d8cbfcba09993d660ca571ed688 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 07:48:44 +0000 Subject: [PATCH 05/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/minimum_tickets_cost.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 73ebffedfedd..180ec0453aa7 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -51,10 +51,10 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: from functools import lru_cache if len(costs) != 3: - raise ValueError('The lengths of costs should be equal 3') + raise ValueError("The lengths of costs should be equal 3") if min(days) <= 0: - raise ValueError('All days elements should be greater than 0') + raise ValueError("All days elements should be greater than 0") days_set = set(days) max_days = max(days) From bfac27a8baaa50b3ad4a530ae404c72350efe614 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 11:56:26 +0400 Subject: [PATCH 06/17] add more tests --- dynamic_programming/minimum_tickets_cost.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 73ebffedfedd..8c60c33d3888 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -34,6 +34,10 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: 17 >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) 24 + >>> mincost_tickets([2], [2,90,150]) + 2 + >>> mincost_tickets([], [2,90,150]) + 0 >>> mincost_tickets([-1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) Traceback (most recent call last): ... @@ -42,6 +46,10 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: Traceback (most recent call last): ... ValueError: The lengths of costs should be equal 3 + >>> mincost_tickets([], []) + Traceback (most recent call last): + ... + ValueError: The lengths of costs should be equal 3 >>> mincost_tickets([2,3,4,5,6,7,8,9,10,30,31], [1,2,3,4]) Traceback (most recent call last): ... @@ -53,6 +61,9 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if len(costs) != 3: raise ValueError('The lengths of costs should be equal 3') + if len(days) == 0: + return 0 + if min(days) <= 0: raise ValueError('All days elements should be greater than 0') From cc0e45fab588db75620d8cd0bb381d33f7ac4e8b Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 12:08:54 +0400 Subject: [PATCH 07/17] add types for the dp function --- dynamic_programming/minimum_tickets_cost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 8c60c33d3888..dda049f65070 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -71,7 +71,7 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: max_days = max(days) @lru_cache(maxsize=None) - def dp(index): + def dp(index: int) -> int: if index > max_days: return 0 From ef0c2dbe15bbd3d0dd36d6a462491d695add89ba Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Wed, 2 Nov 2022 14:04:44 +0400 Subject: [PATCH 08/17] Update dynamic_programming/minimum_tickets_cost.py Co-authored-by: Christian Clauss --- dynamic_programming/minimum_tickets_cost.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 0884721311e9..54bc25ef56f0 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -16,11 +16,6 @@ The implementation was tested on the leetcode: https://leetcode.com/problems/minimum-cost-for-tickets/ -""" - -from typing import List - -""" Minimum Cost For Tickets Dynamic Programming: up -> down. """ From ea4fc07ee5a7a697742cfa7727c8818c3fb90118 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 14:48:42 +0400 Subject: [PATCH 09/17] fix review notes --- dynamic_programming/minimum_tickets_cost.py | 85 +++++++++++++++++---- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 0884721311e9..7bc302e6cb17 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -3,7 +3,7 @@ Date : November 1, 2022 Task: -Given a list of days when you need to travel. Each day is positive integer. +Given a list of days when you need to travel. Each day is integer from 1 to 365. You are able to use tickets for 1 day, 7 days and 30 days. Each ticket has a cost. @@ -26,37 +26,81 @@ """ -def mincost_tickets(days: list[int], costs: list[int]) -> int: +def mincost_tickets(days: List[int], costs: List[int]) -> int: """ - >>> mincost_tickets([1,4,6,7,8,20], [2,7,15]) + >>> mincost_tickets([1, 4, 6, 7, 8, 20], [2, 7, 15]) 11 - >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,7,15]) + + >>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 7, 15]) 17 - >>> mincost_tickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) + + >>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150]) 24 - >>> mincost_tickets([2], [2,90,150]) + + >>> mincost_tickets([2], [2, 90, 150]) 2 - >>> mincost_tickets([], [2,90,150]) + + >>> mincost_tickets([], [2, 90, 150]) 0 - >>> mincost_tickets([-1,2,3,4,5,6,7,8,9,10,30,31], [2,90,150]) + + >>> mincost_tickets('hello', [2, 90, 150]) + Traceback (most recent call last): + ... + ValueError: The parameter days should be a list + + >>> mincost_tickets([], 'world') + Traceback (most recent call last): + ... + ValueError: The parameter costs should be a list + + >>> mincost_tickets([0.25, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150]) + Traceback (most recent call last): + ... + ValueError: All days elements should be integer values + + >>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 0.9, 150]) + Traceback (most recent call last): + ... + ValueError: All costs elements should be integer values + + >>> mincost_tickets([-1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150]) Traceback (most recent call last): ... ValueError: All days elements should be greater than 0 - >>> mincost_tickets([2,3,4,5,6,7,8,9,10,30,31], []) + + >>> mincost_tickets([2, 367], [2, 90, 150]) + Traceback (most recent call last): + ... + ValueError: All days elements should be less than 366 + + >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], []) Traceback (most recent call last): ... ValueError: The lengths of costs should be equal 3 + >>> mincost_tickets([], []) Traceback (most recent call last): ... ValueError: The lengths of costs should be equal 3 - >>> mincost_tickets([2,3,4,5,6,7,8,9,10,30,31], [1,2,3,4]) + + >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [1, 2, 3, 4]) Traceback (most recent call last): ... ValueError: The lengths of costs should be equal 3 """ - from functools import lru_cache + # Validation + if not isinstance(days, list): + raise ValueError("The parameter days should be a list") + + if not isinstance(costs, list): + raise ValueError("The parameter costs should be a list") + + if any([not isinstance(day, int) for day in days]): + raise ValueError("All days elements should be integer values") + + if any([not isinstance(cost, int) for cost in costs]): + raise ValueError("All costs elements should be integer values") if len(costs) != 3: raise ValueError("The lengths of costs should be equal 3") @@ -67,21 +111,30 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if min(days) <= 0: raise ValueError("All days elements should be greater than 0") + if max(days) >= 366: + raise ValueError("All days elements should be less than 366") + + from functools import lru_cache + days_set = set(days) - max_days = max(days) @lru_cache(maxsize=None) def dp(index: int) -> int: - if index > max_days: + """ + >>> dp(366) + 0 + """ + + if index > 365: return 0 if index not in days_set: return dp(index + 1) return min( - costs[0] + dp(index + 1), - costs[1] + dp(index + 7), - costs[2] + dp(index + 30), + costs[0] + dp(index + 1), + costs[1] + dp(index + 7), + costs[2] + dp(index + 30), ) return dp(1) From 157330c7c56798c0ee27e27b066f55264a85ba77 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 10:51:14 +0000 Subject: [PATCH 10/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/minimum_tickets_cost.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 2679659c5595..117a6f360b9a 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -127,9 +127,9 @@ def dp(index: int) -> int: return dp(index + 1) return min( - costs[0] + dp(index + 1), - costs[1] + dp(index + 7), - costs[2] + dp(index + 30), + costs[0] + dp(index + 1), + costs[1] + dp(index + 7), + costs[2] + dp(index + 30), ) return dp(1) From 186ba33b21ff6d429a5f877cc899d0039ced5a90 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 14:56:22 +0400 Subject: [PATCH 11/17] small fix --- dynamic_programming/minimum_tickets_cost.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 2679659c5595..c4abaa27dfe4 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -16,12 +16,14 @@ The implementation was tested on the leetcode: https://leetcode.com/problems/minimum-cost-for-tickets/ + + Minimum Cost For Tickets Dynamic Programming: up -> down. """ -def mincost_tickets(days: List[int], costs: List[int]) -> int: +def mincost_tickets(days: list[int], costs: list[int]) -> int: """ >>> mincost_tickets([1, 4, 6, 7, 8, 20], [2, 7, 15]) 11 @@ -127,9 +129,9 @@ def dp(index: int) -> int: return dp(index + 1) return min( - costs[0] + dp(index + 1), - costs[1] + dp(index + 7), - costs[2] + dp(index + 30), + costs[0] + dp(index + 1), + costs[1] + dp(index + 7), + costs[2] + dp(index + 30), ) return dp(1) From 99af69b13a1edf5b876098db155c0b1862cd007a Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Wed, 2 Nov 2022 15:43:33 +0400 Subject: [PATCH 12/17] Update dynamic_programming/minimum_tickets_cost.py Co-authored-by: Christian Clauss --- dynamic_programming/minimum_tickets_cost.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index c4abaa27dfe4..06b47efa8684 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -87,8 +87,8 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: """ # Validation - if not isinstance(days, list): - raise ValueError("The parameter days should be a list") + if not isinstance(days, list) or not all(isinstance(day, int) for day in days): + raise ValueError("The parameter days should be a list of integers") if not isinstance(costs, list): raise ValueError("The parameter costs should be a list") From d34a84ed6af8a5dc023b19c7cedfde4d61849155 Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Wed, 2 Nov 2022 15:43:44 +0400 Subject: [PATCH 13/17] Update dynamic_programming/minimum_tickets_cost.py Co-authored-by: Christian Clauss --- dynamic_programming/minimum_tickets_cost.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 06b47efa8684..353e9b9cc21d 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -90,8 +90,8 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if not isinstance(days, list) or not all(isinstance(day, int) for day in days): raise ValueError("The parameter days should be a list of integers") - if not isinstance(costs, list): - raise ValueError("The parameter costs should be a list") + if len(costs) !=3 or not all(isinstance(cost, int) for cost in costs): + raise ValueError("The parameter costs should be a list of three integers") if any([not isinstance(day, int) for day in days]): raise ValueError("All days elements should be integer values") From 2d66c34cb9b21dceb264a392ad2183519f2757d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Nov 2022 11:47:00 +0000 Subject: [PATCH 14/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/minimum_tickets_cost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 353e9b9cc21d..e0d2cb9803ec 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -90,7 +90,7 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if not isinstance(days, list) or not all(isinstance(day, int) for day in days): raise ValueError("The parameter days should be a list of integers") - if len(costs) !=3 or not all(isinstance(cost, int) for cost in costs): + if len(costs) != 3 or not all(isinstance(cost, int) for cost in costs): raise ValueError("The parameter costs should be a list of three integers") if any([not isinstance(day, int) for day in days]): From 3568f84fcd3b1f75ad9fcdf2c9a1878fed366c7d Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 2 Nov 2022 15:51:57 +0400 Subject: [PATCH 15/17] fix tests --- dynamic_programming/minimum_tickets_cost.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 353e9b9cc21d..1582f044962e 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -22,6 +22,8 @@ Dynamic Programming: up -> down. """ +from functools import lru_cache + def mincost_tickets(days: list[int], costs: list[int]) -> int: """ @@ -43,22 +45,22 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: >>> mincost_tickets('hello', [2, 90, 150]) Traceback (most recent call last): ... - ValueError: The parameter days should be a list + ValueError: The parameter days should be a list of integers >>> mincost_tickets([], 'world') Traceback (most recent call last): ... - ValueError: The parameter costs should be a list + ValueError: The parameter costs should be a list of three integers >>> mincost_tickets([0.25, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150]) Traceback (most recent call last): ... - ValueError: All days elements should be integer values + ValueError: The parameter days should be a list of integers >>> mincost_tickets([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 0.9, 150]) Traceback (most recent call last): ... - ValueError: All costs elements should be integer values + ValueError: The parameter costs should be a list of three integers >>> mincost_tickets([-1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [2, 90, 150]) Traceback (most recent call last): @@ -73,17 +75,17 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], []) Traceback (most recent call last): ... - ValueError: The lengths of costs should be equal 3 + ValueError: The parameter costs should be a list of three integers >>> mincost_tickets([], []) Traceback (most recent call last): ... - ValueError: The lengths of costs should be equal 3 + ValueError: The parameter costs should be a list of three integers >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [1, 2, 3, 4]) Traceback (most recent call last): ... - ValueError: The lengths of costs should be equal 3 + ValueError: The parameter costs should be a list of three integers """ # Validation @@ -111,8 +113,6 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if max(days) >= 366: raise ValueError("All days elements should be less than 366") - from functools import lru_cache - days_set = set(days) @lru_cache(maxsize=None) From 3d6688ccc719c8c554822617c361d398559a511b Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Wed, 2 Nov 2022 16:22:58 +0400 Subject: [PATCH 16/17] Update dynamic_programming/minimum_tickets_cost.py Co-authored-by: Christian Clauss --- dynamic_programming/minimum_tickets_cost.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index f354a7fc1ff6..6548999ecbed 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -95,15 +95,6 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: if len(costs) != 3 or not all(isinstance(cost, int) for cost in costs): raise ValueError("The parameter costs should be a list of three integers") - if any([not isinstance(day, int) for day in days]): - raise ValueError("All days elements should be integer values") - - if any([not isinstance(cost, int) for cost in costs]): - raise ValueError("All costs elements should be integer values") - - if len(costs) != 3: - raise ValueError("The lengths of costs should be equal 3") - if len(days) == 0: return 0 From e232624a1000e60e6d3dc199ef5eb54c4303bb69 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 2 Nov 2022 13:25:02 +0100 Subject: [PATCH 17/17] Update dynamic_programming/minimum_tickets_cost.py --- dynamic_programming/minimum_tickets_cost.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/minimum_tickets_cost.py b/dynamic_programming/minimum_tickets_cost.py index 6548999ecbed..261a5a7cf42a 100644 --- a/dynamic_programming/minimum_tickets_cost.py +++ b/dynamic_programming/minimum_tickets_cost.py @@ -107,12 +107,7 @@ def mincost_tickets(days: list[int], costs: list[int]) -> int: days_set = set(days) @lru_cache(maxsize=None) - def dp(index: int) -> int: - """ - >>> dp(366) - 0 - """ - + def dynamic_programming(index: int) -> int: if index > 365: return 0 @@ -125,7 +120,7 @@ def dp(index: int) -> int: costs[2] + dp(index + 30), ) - return dp(1) + return dynamic_programming(1) if __name__ == "__main__":