MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _enforce_args

Function _enforce_args

dynamic_programming/rod_cutting.py:177–197  ·  view source on GitHub ↗

Basic checks on the arguments to the rod-cutting algorithms * `n`: int, the length of the rod * `prices`: list, the price list for each piece of rod. Throws ``ValueError``: if `n` is negative or there are fewer items in the price list than the length of the rod

(n: int, prices: list)

Source from the content-addressed store, hash-verified

175
176
177def _enforce_args(n: int, prices: list):
178 """
179 Basic checks on the arguments to the rod-cutting algorithms
180
181 * `n`: int, the length of the rod
182 * `prices`: list, the price list for each piece of rod.
183
184 Throws ``ValueError``:
185 if `n` is negative or there are fewer items in the price list than the length of
186 the rod
187 """
188 if n < 0:
189 msg = f"n must be greater than or equal to 0. Got n = {n}"
190 raise ValueError(msg)
191
192 if n > len(prices):
193 msg = (
194 "Each integral piece of rod must have a corresponding price. "
195 f"Got n = {n} but length of prices = {len(prices)}"
196 )
197 raise ValueError(msg)
198
199
200def main():

Callers 3

naive_cut_rod_recursiveFunction · 0.85
top_down_cut_rodFunction · 0.85
bottom_up_cut_rodFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected