Open the context
Choose the project folder and name the files or behavior involved.
Find the minimum scalar-multiplication cost for a chain of up to 12 matrices. Test one matrix and different parenthesizations, rejecting dimensions outside 1 through 1000 without multiplying any matrix entries.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def chain_cost(dimensions):
if type(dimensions) not in (list, tuple) or not 2 <= len(dimensions) <= 13:
raise ValueError("one through twelve matrices")
if any(type(d) is not int or not 1 <= d <= 1000 for d in dimensions):
raise ValueError("dimensions 1 through 1000")
n = len(dimensions)-1
cost = [[0]*n for _ in range(n)]
for width in range(2, n+1):
for left in range(n-width+1):
right = left+width-1
cost[left][right] = min(
cost[left][k]+cost[k+1][right]+
dimensions[left]*dimensions[k+1]*dimensions[right+1]
for k in range(left,right))
return cost[0][n-1]The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.