I'm learning python through the "How to think like a computer scientist" book. There is a certain exercise that multiplies a matrix m by a scalar n:
above is the correct code.
Now, can someone explain why I get :
>>> b [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
scalar_mult(10, b)
Expected:
[[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
Got:
[[30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30]]
when the code is written as follows:
what exactly happens when I move new_row = [] above "for row in m:" ?
Code:
def scalar_mult(n, m):
"""
>>> a = [[1, 2], [3, 4]]
>>> scalar_mult(3, a)
[[3, 6], [9, 12]]
>>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2,2, 3]]
>>> b
[[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
>>> scalar_mult(10, b)
[[30, 50, 70], [10, 10, 10], [0, 20, 0], [20,20, 30]]
>>> b
[[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
"""
new_matrix = []
for row in m:
new_row = []
for value in row:
new_row += [value*n]
new_matrix += [new_row]
return new_matrix
Now, can someone explain why I get :
>>> b [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
scalar_mult(10, b)
Expected:
[[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
Got:
[[30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30], [30, 50, 70, 10, 10, 10, 0, 20, 0, 20, 20, 30]]
when the code is written as follows:
Code:
new_matrix = []
new_row = []
for row in m:
for value in row:
new_row += [value*n]
new_matrix += [new_row]
return new_matrix
Comment