Question: using python code, please help Exercise 1 This exercise is about matrix addition. If we have two rectangular matrices A and B of same size,

using python code, please help
 using python code, please help 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

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 matrixAdd (second, first), and that matrixAdd (first, zero), where zero is the rectangular matrix of same size as first but full of 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 lst and a number num as parameters. The function should modify lst by adding num to all the elements of lst. The function should not return anything. Call the function in the main program to do few tests, as per the below sample runs: Initial list 1:[0,1,2,3,4,5,6,7,8,9] Value of 1 after applying saclarAdd (1,0):[0,1,2,3,4,5,6,7,8,9] Value of 1 after applying saclarAdd (1,1):[1,2,3,4,5,6,7,8,9,10] Value of 1 after applying saclarAdd (1,2):[3,4,5,6,7,8,9,10,11,12] Value of 1 after applying saclarAdd (1,3):[6,7,8,9,10,11,12,13,14,15] Value of 1 after applying saclarAdd (1,4):[10,11,12,13,14,15,16,17,18,19] Exercise 3 Write a function randList 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 shuffle 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,10,17,5,6] What is the size of the random matrix you want? 18 [6,13,1,7,14,5,9,8,3,16,10,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 order (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] test2=[2,7,3,3] print("First test:") print("Before function:", test) print("Five smallest: ", kMin(test,5)) print("After function:", test) print("Second test:") print("Before function:", test2) print("Five smallest: ", kMin(test2,5)) print("After function:", test2) You should then be able to get the following 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: Before function: [2,7,3,3] Five smallest: [2,3,3,7] After function: [2,7,3,3]

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!