mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
psf/black code formatting (#1277)
This commit is contained in:
committed by
Christian Clauss
parent
07f04a2e55
commit
9eac17a408
@@ -12,7 +12,7 @@ pieces separately or not cutting it at all if the price of it is the maximum obt
|
||||
|
||||
|
||||
def naive_cut_rod_recursive(n: int, prices: list):
|
||||
"""
|
||||
"""
|
||||
Solves the rod-cutting problem via naively without using the benefit of dynamic programming.
|
||||
The results is the same sub-problems are solved several times leading to an exponential runtime
|
||||
|
||||
@@ -36,18 +36,20 @@ def naive_cut_rod_recursive(n: int, prices: list):
|
||||
30
|
||||
"""
|
||||
|
||||
_enforce_args(n, prices)
|
||||
if n == 0:
|
||||
return 0
|
||||
max_revue = float("-inf")
|
||||
for i in range(1, n + 1):
|
||||
max_revue = max(max_revue, prices[i - 1] + naive_cut_rod_recursive(n - i, prices))
|
||||
_enforce_args(n, prices)
|
||||
if n == 0:
|
||||
return 0
|
||||
max_revue = float("-inf")
|
||||
for i in range(1, n + 1):
|
||||
max_revue = max(
|
||||
max_revue, prices[i - 1] + naive_cut_rod_recursive(n - i, prices)
|
||||
)
|
||||
|
||||
return max_revue
|
||||
return max_revue
|
||||
|
||||
|
||||
def top_down_cut_rod(n: int, prices: list):
|
||||
"""
|
||||
"""
|
||||
Constructs a top-down dynamic programming solution for the rod-cutting problem
|
||||
via memoization. This function serves as a wrapper for _top_down_cut_rod_recursive
|
||||
|
||||
@@ -75,13 +77,13 @@ def top_down_cut_rod(n: int, prices: list):
|
||||
>>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
||||
30
|
||||
"""
|
||||
_enforce_args(n, prices)
|
||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||
return _top_down_cut_rod_recursive(n, prices, max_rev)
|
||||
_enforce_args(n, prices)
|
||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||
return _top_down_cut_rod_recursive(n, prices, max_rev)
|
||||
|
||||
|
||||
def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
||||
"""
|
||||
"""
|
||||
Constructs a top-down dynamic programming solution for the rod-cutting problem
|
||||
via memoization.
|
||||
|
||||
@@ -99,22 +101,25 @@ def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list):
|
||||
-------
|
||||
The maximum revenue obtainable for a rod of length n given the list of prices for each piece.
|
||||
"""
|
||||
if max_rev[n] >= 0:
|
||||
return max_rev[n]
|
||||
elif n == 0:
|
||||
return 0
|
||||
else:
|
||||
max_revenue = float("-inf")
|
||||
for i in range(1, n + 1):
|
||||
max_revenue = max(max_revenue, prices[i - 1] + _top_down_cut_rod_recursive(n - i, prices, max_rev))
|
||||
if max_rev[n] >= 0:
|
||||
return max_rev[n]
|
||||
elif n == 0:
|
||||
return 0
|
||||
else:
|
||||
max_revenue = float("-inf")
|
||||
for i in range(1, n + 1):
|
||||
max_revenue = max(
|
||||
max_revenue,
|
||||
prices[i - 1] + _top_down_cut_rod_recursive(n - i, prices, max_rev),
|
||||
)
|
||||
|
||||
max_rev[n] = max_revenue
|
||||
max_rev[n] = max_revenue
|
||||
|
||||
return max_rev[n]
|
||||
return max_rev[n]
|
||||
|
||||
|
||||
def bottom_up_cut_rod(n: int, prices: list):
|
||||
"""
|
||||
"""
|
||||
Constructs a bottom-up dynamic programming solution for the rod-cutting problem
|
||||
|
||||
Runtime: O(n^2)
|
||||
@@ -137,24 +142,24 @@ def bottom_up_cut_rod(n: int, prices: list):
|
||||
>>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30])
|
||||
30
|
||||
"""
|
||||
_enforce_args(n, prices)
|
||||
_enforce_args(n, prices)
|
||||
|
||||
# length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of length 0.
|
||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||
max_rev[0] = 0
|
||||
# length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of length 0.
|
||||
max_rev = [float("-inf") for _ in range(n + 1)]
|
||||
max_rev[0] = 0
|
||||
|
||||
for i in range(1, n + 1):
|
||||
max_revenue_i = max_rev[i]
|
||||
for j in range(1, i + 1):
|
||||
max_revenue_i = max(max_revenue_i, prices[j - 1] + max_rev[i - j])
|
||||
for i in range(1, n + 1):
|
||||
max_revenue_i = max_rev[i]
|
||||
for j in range(1, i + 1):
|
||||
max_revenue_i = max(max_revenue_i, prices[j - 1] + max_rev[i - j])
|
||||
|
||||
max_rev[i] = max_revenue_i
|
||||
max_rev[i] = max_revenue_i
|
||||
|
||||
return max_rev[n]
|
||||
return max_rev[n]
|
||||
|
||||
|
||||
def _enforce_args(n: int, prices: list):
|
||||
"""
|
||||
"""
|
||||
Basic checks on the arguments to the rod-cutting algorithms
|
||||
|
||||
n: int, the length of the rod
|
||||
@@ -164,30 +169,32 @@ def _enforce_args(n: int, prices: list):
|
||||
|
||||
if n is negative or there are fewer items in the price list than the length of the rod
|
||||
"""
|
||||
if n < 0:
|
||||
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
|
||||
if n < 0:
|
||||
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
|
||||
|
||||
if n > len(prices):
|
||||
raise ValueError(f"Each integral piece of rod must have a corresponding "
|
||||
f"price. Got n = {n} but length of prices = {len(prices)}")
|
||||
if n > len(prices):
|
||||
raise ValueError(
|
||||
f"Each integral piece of rod must have a corresponding "
|
||||
f"price. Got n = {n} but length of prices = {len(prices)}"
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
prices = [6, 10, 12, 15, 20, 23]
|
||||
n = len(prices)
|
||||
prices = [6, 10, 12, 15, 20, 23]
|
||||
n = len(prices)
|
||||
|
||||
# the best revenue comes from cutting the rod into 6 pieces, each
|
||||
# of length 1 resulting in a revenue of 6 * 6 = 36.
|
||||
expected_max_revenue = 36
|
||||
# the best revenue comes from cutting the rod into 6 pieces, each
|
||||
# of length 1 resulting in a revenue of 6 * 6 = 36.
|
||||
expected_max_revenue = 36
|
||||
|
||||
max_rev_top_down = top_down_cut_rod(n, prices)
|
||||
max_rev_bottom_up = bottom_up_cut_rod(n, prices)
|
||||
max_rev_naive = naive_cut_rod_recursive(n, prices)
|
||||
max_rev_top_down = top_down_cut_rod(n, prices)
|
||||
max_rev_bottom_up = bottom_up_cut_rod(n, prices)
|
||||
max_rev_naive = naive_cut_rod_recursive(n, prices)
|
||||
|
||||
assert expected_max_revenue == max_rev_top_down
|
||||
assert max_rev_top_down == max_rev_bottom_up
|
||||
assert max_rev_bottom_up == max_rev_naive
|
||||
assert expected_max_revenue == max_rev_top_down
|
||||
assert max_rev_top_down == max_rev_bottom_up
|
||||
assert max_rev_bottom_up == max_rev_naive
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user