Question: Exercise 1 This exercise is about matrix addition. If we have two rectangular matrices A and B of same size, we can then define the

 Exercise 1 This exercise is about matrix addition. If we have

Exercise 1 This exercise is about matrix addition. If we have two rectangular matrices A and B of same size, we can then define the addition of both C=A+B, where C[i][j]=A[i][j]+B[i][j ) (addition element by element). Write a function matrixAdd with two parameters first and second which are two matrices. The function matrixadd should return the sum of first and second. The function should check that both matrices are rectangular matrices (all rows have the same number of elements) of same sizes, otherwise it raises an assertion error. In the main program create test cases to check that your program is working well. In particular check that matrixAdd ( first, second) is always equal to natrixAdd (second, first), and that matrixadd ( f irst, zero), where zero is the rectangular matrix of same size as first but full af 0 , returns first. Present your results nicely by showing the first matrix, one row per line, the second matrix one row per line, and the addition, one row per line. It could be useful to create another function for nicely printing the results. Don't forget to add docstrings! Exercise 2 Write a function scalaradd which takes a list of numbers 1 st and a number num as parameters. The function should modify 1 st by adding num to all the elements of 1 st. The function should not return anything. Call the function in the main program to do few tests, as per the below sample runs: Initiol 1 ist 1:[8,1,2,3,4,5,6,7,8,9] Volue of 1 after applying satlarAdd (1,9):(0,1,2,3,4,5,6,7,8,9] Value of 1 after applying saclackdd (1,1)=[1,2,3,4,5,6,7,8,9,18] Volue of 1 after applying soclaradd (1,2}=[3,4,5,6,7,8,9,10,11,12] value of 1 after applying saclaradd (1,3)=[6,7,8,9,18,11,12,13,14,15] value of 1 after applying saclacAdd (1,4):[19,11,12,13,14,15,16,17,18,19] Exercise 3 Write a function rand ist with a parameter size, that returns the a list composed of the integers between 1 and size in random order (each number should appear exactly once). Call if from the main function to check that it works. Hint: recall the shuffe function of the random module. What is the size of the random matrix you want? 18 [9,1,16,18,4,14,13,11,15,12,2,7,3,8,16,17,5,6] >>> What 15 the size of the random matrix you want? 18 [6,13,1,7,14,5,9,8,3,16,19,11,15,4,12,18,17,2] Exercise 4 Given a list, it is easy to find the minimal element. The goal of this exercise is to write a function that returns not only the smallest element but the k smallest ones in ascending arder (where k is an integer more than 1 ). In case k is less than the number of elements of the list, the function should return all the elements of the list. The function should not modify the initial list! To test your code, use the below main program: test= [2,7,9,3,6,5,1,1,2,9] test =[2,7,3,3] print("First test:") print("Before function: ", test) print("Five smallest: ", kMin(test, 5)) print("After function: ", test) print("5econd test:") print("Before function: ", test2) print("Five smallest: ", kMin(test2,5)) print("After function: ", test2) You should then be able to get the folkwing sample run: First test: Before function: [2,7,9,3,6,5,1,1,2,9] Five smallest: [1,1,2,2,3] After function: [2,7,9,3,6,5,1,1,2,9] Second test

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!