Helping Jennifer
The snippet can be accessed without any authentication.
Authored by
Chris Coley
Edited
jennifer.py 1.95 KiB
def list_as_precedence_matrix(a_response):
# Initialize to zero
return_matrix = [[0 for row in range(NUMBER_OF_FRAGMENTS)]
for column in range(NUMBER_OF_FRAGMENTS)]
# Foreach number 'n1' in a_response:
# Foreach number 'n2' that comes after 'n1' in a_response:
# return_matrix[n1][n2] = 1
for index_of_first in range(len(a_response) - 1):
for index_of_second in range(index_of_first + 1, len(a_response)):
value1, value2 = int(a_response[index_of_first]), int(a_response[index_of_second])
return_matrix[value1 - 1][value2 - 1] = 1
# Return the response matrix
return return_matrix
def pointwise_product(a_matrix, a_second_matrix):
return [[a_matrix[row][column] * a_second_matrix[row][column]
for column in range(NUMBER_OF_FRAGMENTS)]
for row in range(NUMBER_OF_FRAGMENTS)]
# NOTE: For this assignment, you really only have to calculate the products
# for the cells in the upper triangle. This is because you're only summing
# the cells in the upper triangle. I've included code that would do just
# that, but I wouldn't turn it in because it doesn't return a true pointwise
# product.
# Initialize to zero
return_matrix = [[0 for row in range(NUMBER_OF_FRAGMENTS)]
for column in range(NUMBER_OF_FRAGMENTS)]
# Calculate the products for cells in the upper triangle
for row in range(NUMBER_OF_FRAGMENTS):
for column in range(row + 1, NUMBER_OF_FRAGMENTS):
return_matrix[row][column] = a_matrix[row][column] * a_second_matrix[row][column]
def sum_of_upper_triangle(a_matrix):
sum_returned = 0
# Foreach row in a_matrix:
# Foreach column in row, where column starts at row + 1:
# sum += a_matrix[row][column]
for row in range(NUMBER_OF_FRAGMENTS):
for column in range(row + 1, NUMBER_OF_FRAGMENTS):
sum_returned += a_matrix[row][column]
return sum_returned
Please register or sign in to comment