## Finding the closest pair of points

#Compute the distance between towo points (x1, y1) and (x2, y2) def distance(x1, y1, x2, y2): return ((x2 – x1) * (x2 – x1) + (y2 – y1) * (y2 – y1)) ** 0.5 def nearestPoints(points): # p1 and p2 are the indexws in the points list p1, p2 = 0, 1 # Initial two … Read moreFinding the closest pair of points

def main(): # Students’ answer to the questions answers = [ [‘A’, ‘B’, ‘A’, ‘C’, ‘C’, ‘D’, ‘E’, ‘E’, ‘A’, ‘D’], [‘D’, ‘B’, ‘A’, ‘B’, ‘C’, ‘A’, ‘E’, ‘E’, ‘A’, ‘D’], [‘E’, ‘D’, ‘D’, ‘A’, ‘C’, ‘B’, ‘E’, ‘E’, ‘A’, ‘D’], [‘C’, ‘B’, ‘A’, ‘E’, ‘D’, ‘C’, ‘E’, ‘E’, ‘A’, ‘D’], [‘A’, ‘B’, ‘D’, ‘C’, … Read moreGrade An Exam (program that grades multiple-choice tests)

## Random Shuffling A matrix

import random matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Assume a list is given for row in range(len(matrix)): for column in range(len(matrix[row])): i = random.randint(0, len(matrix) – 1) j = random.randint(0, len(matrix[row]) – 1) # Swap matrix[row][column] with matrix [i][j] matrix[row][column], matrix[i][j] = matrix[i][j], matrix[row][column] for row in range(len(matrix)): for … Read moreRandom Shuffling A matrix

## Finding the sum of elements by column in a matrix

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Assume a list is given for column in range(len(matrix[0])): total = 0 for row in range(len(matrix)): total += matrix[row][column] print(“Sum for column”, column, “is”, total)

## Finding the Row with largest Sum in a matrix

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] maxRow = sum(matrix[0]) indexOfMaxRow = 0 for i in range(1, len(matrix)): if sum(matrix[i]) > maxRow: maxRow = sum(matrix[i]) indexOfMaxRow = i print(“Row”, indexOfMaxRow, “has the maximum sum of “, maxRow)

## Initialize Matrix with random values

import random matrix = [] # Create an empty list numberOfRows = eval(input(“Enter the number of rows: “)) numberOfColumns = eval(input(“Enter the number of columns: “)) for row in range(numberOfRows): matrix.append([]) #add an empty new row for column in range(numberOfColumns): matrix[row].append(random.randint(0, 99)) for row in matrix: for value in row: print(value, end=” “) print() # … Read moreInitialize Matrix with random values

## Initialize a Matrix and enter elements

matrix = [] # Create an empty list numberOfRows = eval(input(“Enter the number of rows: “)) numberOfColumns = eval(input(“Enter the number of columns: “)) for row in range(numberOfRows): matrix.append([]) #add an empty new row for column in range(numberOfColumns): value = eval(input(“Enter an element and press Enter: “)) matrix[row].append(value) print(matrix)