TURIN POLYTECHNIC UNIVERSITY IN TASHKENT COURSE OF ALGORITHM DESIGN PRACTICE n.4 Exercise 1: Calculate the N-th number of Fibonacci sequence (recursion and iteration) Definition: FIBN+1 = FIBN + FIBN-1 for n>0 FIB1 = 1 FIB0 = 0 FIB2 = FIB0 + FIB1 = 1 FIB3 = FIB1 + FIB2 = 2 FIB4 = FIB2 + FIB3 = 3 FIB5 = FIB3 + FIB4 = 5 Exercise 2: A text file contains a square matrix of real values, stored with the following format: • each of the following file lines gives all the values corresponding to a matrix row, separated by one or more spaces. Assume that the file format (and contents) is totally correct. Write down a Python program able to: • read such a matrix from the input file (the file name is specified by the user). • generate a new matrix, with the same size as the one loaded from the file, in which the value of each element is: o 0 if the corresponding element of the input matrix is negative or equal to 0. o the order of magnitude of the corresponding element of the input matrix otherwise. The order of magnitude of a number is defined as the smallest power of 10 which is larger than or equal to the given value. • display an appropriate message on screen according to the fact that the matrix so generated is symmetric or not. Solve the problem by implementing a proper number of Python functions (e.g., a function for loading the matrix from file, one in order to generate the second matrix, one more for checking whether the generated matrix is symmetric, etc.). Example Let the input file contents be the following: 0.7 -3.2 2.8 53.2 0 0.04 93.1 0.003 5.2 21.6 8.4 -12.4 11.4 0.009 -6.7 0.1 Then, the matrix generated by the program should be the following: 1.0 0.0 10.0 100.0 0.0 0.1 100.0 0.01 10.0 100.0 10.0 0.0 100.0 0.01 0.0 0.1 and thus the program should display a message like: The matrix generated starting from the input file is symmetric. Let the input file contents be the following: 0.7 -3.2 12.8 0 -14.5 793.1 75.2 121.6 55.4 Program Output: 1 0 100 0 0 1000 100 1000 100 The generated matrix is not symmetric. Solution 1. #recursive def fibonacci(n): if n == 1: return 0 if n == 2: return 1 f1 = fibonacci(n-1) f2 = fibonacci(n-2) return f1 + f2 for i in range(1,6): print(fibonacci(i)) #iterative def fibonacci(n): f1 = 1 f2 = 0 if n == 1: return 0 f = f1 + f2 for i in range(3,n+1): f2 = f1 f1 = f f = f1 + f2 return f for i in range(1,6): print(fibonacci(i)) 2. def generateOrder(matrixInput, matrixOrder, dim): for i in range(dim): for j in range(dim): if matrixInput[i][j] <= 0: matrixOrder[i][j] = 0 else: order = 1 while matrixInput[i][j] > order: order *= 10 while matrixInput[i][j] < order / 10: order /= 10 matrixOrder[i][j] = order def check(matrixOrder, dim): for i in range(dim): for j in range(i+1,dim): if matrixOrder[i][j] != matrixOrder[j][i]: return 0 return 1 matrixInput = [] matrixOrder = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] dim = 0 try: f = open("text.txt") for line in f.readlines(): new = [] for val in line.split(): new.append(float(val)) matrixInput.append(new) generateOrder(matrixInput, matrixOrder, len(matrixInput[0])) if check(matrixOrder, dim) == 0: space = " NOT " else: space = " " print("Output:") for i in range(len(matrixOrder)): for j in range(len(matrixOrder[0])): print(matrixOrder[i][j], end=" ") print() print("The generated matrix is{}symmetric".format(space)) finally: f.close()