Simple Python nested strings and slicing assignment. At least it should be but,
ID: 3594191 • Letter: S
Question
Simple Python nested strings and slicing assignment. At least it should be but, I'm having issues and looking to compare coding to figure out where I'm going wrong. I need to use ranges (not col and row matrixing) to solve this and my calculations aren't coming out correct. Thank you in advance for the help! :)
def process_matrix(matrix): """ process a 2-D list: Finish this function which accepts a 2-D(nested) list and calculate the sum of all the item in this 2-D matrix. For example, a 2-D matrix may look like [[1,3],[2,4]]. The first list [1, 3] is the first line and [2, 4] is the 2nd line: [ [1, 3], [2, 4] ] You can assume that there are multiple lines of this matrix and each line has multiple values The return value should be the sum. """
# test my_2d_list = [[88,1], [5,67], [52,60,7]] print(process_matrix(my_2d_list)) # should b 280 print("--"*10) def largest_diagonal_value(matrix): """ process a 2-D list with if-else statements: Finish this function which finds the largest diagonal value in a matrix. A diagonal value of a matrix is a value which has the same row index and column index. For the matrix below: [ [1, 3], [2, 4] ] 1 and 4 are the diagonal values. You can assume that the parameter, matrix, is a non-empty n-by-n matrix. The return value should be the largest diagonal value. Hint: you can handle this question with one loop. """
#test my_4_by_4 = [ [1,2,3,4], [11,22,33,44], [61,-62,63,64], [49,64,81,100] ] print(largest_diagonal_value(my_4_by_4)) # 100 print("--"*10) def has_negative_value_in_matrix(matrix): """ process a 2-D list with nested loops and if-else: Finish this function which checks whether the parameter, matrix, has any negative value. This function should always return a boolean. """
#test print(has_negative_value_in_matrix(my_4_by_4)) #True print(has_negative_value_in_matrix(my_2d_list)) #False print("--"*10)
Explanation / Answer
def process_matrix(matrix):
"""
process a 2-D list:
Finish this function which accepts a 2-D(nested) list and calculate the sum of all the item in this 2-D matrix.
For example, a 2-D matrix may look like [[1,3],[2,4]]. The first list [1, 3] is the first line and [2, 4] is the 2nd line:
[ [1, 3],
[2, 4] ]
You can assume that there are multiple lines of this matrix and each line has multiple values
The return value should be the sum.
"""
sum2D = 0
for row in matrix:
for cell in row:
sum2D += cell
return sum2D
# test
my_2d_list = [[88,1], [5,67], [52,60,7]]
print(process_matrix(my_2d_list)) # should b 280
print("--"*10)
def largest_diagonal_value(matrix):
"""
process a 2-D list with if-else statements:
Finish this function which finds the largest diagonal value in a matrix.
A diagonal value of a matrix is a value which has the same row index and column index.
For the matrix below:
[ [1, 3],
[2, 4] ]
1 and 4 are the diagonal values.
You can assume that the parameter, matrix, is a non-empty n-by-n matrix.
The return value should be the largest diagonal value.
Hint: you can handle this question with one loop.
"""
maxDiagonal = matrix[0][0]
for i in range(len(matrix)):
if maxDiagonal < matrix[i][i]:
maxDiagonal = matrix[i][i]
return maxDiagonal
#test
my_4_by_4 = [
[1,2,3,4],
[11,22,33,44],
[61,-62,63,64],
[49,64,81,100]
]
print(largest_diagonal_value(my_4_by_4)) # 100
print("--"*10)
def has_negative_value_in_matrix(matrix):
"""
process a 2-D list with nested loops and if-else:
Finish this function which checks whether the parameter, matrix, has any negative value.
This function should always return a boolean.
"""
for row in matrix:
for cell in row:
if cell < 0:
return True
return False
#test
print(has_negative_value_in_matrix(my_4_by_4)) #True
print(has_negative_value_in_matrix(my_2d_list)) #False
print("--"*10)
# copy pastable code link: https://paste.ee/p/R8bvr
Sample output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.