def northwest_corner(demand, supply, cost): The code starts by initializing the northwest_corner function that takes in three inputs: demand, supply, and cost. result = [] The next thing happened is that a list of row = 0 tuples is created with the row and col = 0 column numbers for each tuple in the total_cost = 0 list. This list will be used to create an allocation matrix later on. while row < len(demand) and col < Then it loops through the rows of len(supply): demand and the columns of supply until if demand[row] < supply[col]: either row or col is equal to len(demand) result.append(((row, col), demand[row])) or len(supply), respectively. total_cost += demand[row] * While row < len(demand) and col < cost[row][col] len(supply), then if demand[row] < supply[col] -= demand[row] supply[col], then result.append((row, row += 1 col), demand[row]) will be appended to else: the list with (row, col) being set as its result.append(((row, col), supply[col])) values. total_cost += supply[col] * cost[row][col] This will cause total_cost increase by demand[row] -= supply[col] the amount of each item in demand col += 1 multiplied by their respective costs for return result, total_cost that column on row. It happens same way with the second case, when if demand[row] > supply[col] demand = [100, 50, 75] After initialize the NWCR function, we supply = [200, 75, 50, 100] apply to an example to solve problem. cost = [[10, 20, 30, 40], [15, 25, 35, 45], The code will print the allocation matrix [5, 10, 15, 20]] and total cost. allocation, total_cost = northwest_corner(demand, supply, cost) print("Allocation Matrix: ", allocation) print("Total Cost: ", total_cost)