You are given a list of numbers num. Write a function Exercisel (num) returning a list...
Fantastic news! We've located the answer you've been seeking!
Question:
Transcribed Image Text:
You are given a list of numbers num. Write a function Exercisel (num) returning a list ans, such that ans [i] is equal to the product of all the numbers of num except num [i]. For example, if your input is num= [1,2,3,4], your function should return [24, 12, 8, 6]. Note that to get the full mark, you are only allowed to use the builtin functions len (), range () and maybe reversed (). Mark: [10%] Hint: 1. Initialize two empty arrays, L and R where for a given index i, L[i] would contain the product of all the numbers to the left of i and R[i] would contain the product of all the numbers to the right of i. 2. For the array L, L[0] would be 1 since there are no elements to the left of the first element. For the rest of the elements, we simply use L[i] = L[i − 1] * num[i – 1]. 3. For the other array, we do the same thing but in reverse i.e., we start with the initial value of 1 in R[length  1] where length is the number of elements in the list and keep updating R[i] in reverse. Essentially, R[i] = R[i+1] * num[i+1] 4. Once we have the two arrays set up properly, we simply iterate over the input array one element at a time, and for each element at index i, we find the wanted result by calculating L[i]* R[i]. Question 2) You are given an m x n matrix. Write a function Exercise2 (matrix) that returns all elements of the matrix in spiral order. Your code should work on any matrix (a nested list) with m > 0 and n> 0. Mark: [10% ] Example 1: 1 2 3 4+5 6 7 8 9 Input: matrix = [[1,2,3], [4,5,6], [7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: 1+2+3+4 8 5 6 7 9 10 11 12 Input: matrix = [[1,2,3,4], [5,6,7,8], [9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Hint: You are allowed to use numpy module. In that case, make sure your function's output is converted to a nested list before returning the output. Alternatively, you can use the approach below: 1. Initialize the top, right, bottom, and left boundaries as up, right, down, and left. 2. Initialize the output array result. 3. Traverse the elements in spiral order and add each element to result: o Traverse from left boundary to right boundary. o Traverse from up boundary to down boundary. o Before we traverse from right to left, we need to make sure that we are not on a row that has already been traversed. If we are not, then we can traverse from right to left. o Similarly, before we traverse from top to bottom, we need to make sure that we are not on a column that has already been traversed. Then we can traverse from down to up. o Remember to move the boundaries by updating left, right, up, and down accordingly. 4. Return result. Question 3) You are given four lists nums 1, nums 2, nums 3, and nums 4, all of them with integer numbers as their items, and all of them with equal length n. Write a function Exercise3 (nums1, nums 2, nums 3, nums 4) that returns the number of tuples (i, j, k, 1) such that: 0 <= i, j, k, 1 < n numsl[i] + nums2 [j] + nums 3[k] + nums4 [1] == 0 Mark: [10%] Example 1: Input: nums1 = [1,2], nums2 = [2,1], nums 3 = [1,2], nums 4 = [0,2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) > nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (2) + (1) + 2 = 0 2. (1, 1, 0, 0) > nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (1) + (1) + 0 = 0 Example 2: Input: nums1 = [0], nums2 = [0], nums 3 = [0], nums4= [0] Output: 1 Hint: An easy and efficient approach would be to use three nested loops. In this case, for each sum a+b+c, search for a complementary value d == (a+b+c) in the fourth array. It is recommended that you populate the fourth array into a dictionary. Note that you need to track the frequency of each element in the fourth array. If an element is repeated multiple times, it will form multiple quadruples. Therefore, we will use dictionary values to store counts.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions

Twodimensional array operations: Movie Ratings program ** You have recently collected reviews from four movie reviewers where the reviewers are numbered 14. Each reviewer has rated six movies where...

Four people, each with a mass of 72.4 kg, are in a car with a mass of 1 130 kg. An earthquake strikes. The driver manages to pull off the road and stop, as the vertical oscillations of the ground...

Why do national income accountants compare the market value of the total outputs in various years rather than actual physical volumes of production? What problem is posed by any comparison over time...

Draw NAD + and FAD showing where the electrons and hydrogens go when the molecules are reduced.

Write a VHDL description for the sequential circuit with the state diagram given by Figure 419(d). Include an asynchronous RESET signal to initialize the circuit to state Init. Compile your...

Suppose that Sun Farms contacts every basil grower in the country and buys the last remaining chemicalfree basil anywhere. Nevertheless, Sun Farms is able to ship only 1,475 pounds to Vals. Would...

Looking into the issues with Walmart, their long lines and how to make it better. For starters, I started the research and find a solution for the lack of Walmart Cashiers/Employees. We all have had...

Suppose the current price of a stock is $18. Consider a two state tree model defined such that after 3 months, the stock price may increase to $19 with a 40% probability or decrease to $16 with a 60%...

A point particle of charge q,3.0x10* C is place on the xaxis at x = 10 cm. A second particle of charge q, is placed on the xaxis at x= +25 cm. The electric field at the origin is zero. Determine...

Address the issues that are related to replication and fault tolerance. Explain the difference between the crash of a server and the Byzantine failure of a server.

In a particular digital EEG processing system, a signal bandwidth of 12Hz and a signal amplitude resolution of 0.5% covering a voltage range of 3V are required. Uniform quantisation is to be used....

What additional fault tolerance method would be recommended in order to protect the workstation from the other natural disasters like floods and fires? Does this method protect against harddrive...

Find the risk profile for the following: i. A borrower who borrows with flexible interest rates as interest rates rise. ii. A Chocolate manufacturer as the price of Cocoa rises. iii. A US...

What is the most difficult type of fault to detect? crash faults rational behavior byzantine faults noop

The equilibrium constant K, and Kp, for the homogeneous gaseous reaction A= 2B and C D+E respectively are in the ratio of 1:9. If the degree of dissociation of A and C be equal then the ratio of...

Horse serum containing specific antibody to snake venom has been a successful approach to treating snakebite in humans. How do you think this antivenom could be generated? What are some advantages...

Luke and Angie Inc.s financial statements as at December 31, 2016, appear below: Supplemental financial information for the year ended December 31, 2016: Luke and Angie exchanged 1,500 preferred...

Super Computers Inc. (SCI) is a publicly accountable enterprise that manufactures computer microchips. It is conducting various impairment tests in concert with the preparation of its financial...

In January 2016, JN Norman Inc. (JNN) purchased and installed production equipment. It was first available for use on March 1, 2016. JNN has a December 31 yearend and accounts for partial years...

Why do stockprice movements repeat themselves?

Why would technical charts be useful in trying to apply the Dow theory?

How do the new contrarians differ from the more traditional contrarians?
Question Categories