You are given a list of numbers num. Write a function Exercisel (num) returning a list...
Fantastic news! We've Found the answer you've been seeking!
Question:
![](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2023/08/64f04ef1f0ccb_1693470449270.jpg)
![](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2023/08/64f04efeca31d_1693470462079.jpg)
![](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2023/08/64f04f097995e_1693470472772.jpg)
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 built-in 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. 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 built-in 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
-
How do theories of globalization and transnationalism reconfigure conventional understandings of social boundaries, community, and identity in an increasingly interconnected world ?
-
Two-dimensional array operations: Movie Ratings program ** You have recently collected reviews from four movie reviewers where the reviewers are numbered 1-4. Each reviewer has rated six movies where...
-
The following is a list of items that could be included in the intangible assets section of the balance sheet. (a) Indicate which items on the list below would generally be reported as intangible...
-
YOU have just graduated from Deakin as an accounting and finance graduate. During your time at Deakin, you were organized and diligent with your studies and thus graduated with flying colors. This...
-
On December 31, 2011, Ajacks Company reported the following information in its financial statements: Accounts receivable. $1,193,400 Allowance for doubtful accounts 81,648 Bad debts expense............
-
In Exercises find (a) ( o g) (-1) (b) (g o ) (2) (c) ( o ) (x) (d) (g o g) (x) f(x) = 2x, g(x)=x + 1
-
Enter the age distribution of the United States into a technology tool. Use the tool to find the mean age in the United States.
-
Sergio Flores works at the drive-through window of Big & Bad Burgers. Occasionally when a drive-through customer orders, Sergio fills the order and pockets the customers money. He does not ring up...
-
Please 100% correct within few minutes or else skip 11 Rs. 10,000 From the following particulars of Jackson and Company Ltd., prepare a cost sheet for the month of December, 2011 : Stock on 1.12.11...
-
The carrying amount of ABZ Inc. equity at January 1, 2018 was: Equity CU 1,200,000 Share Capital compromising 120,000 shares of CU 10 par value each Share premium 4,800,000 Share Option Reserve...
-
When Sally noticed that she was out of paper towels, she made amental note to stop at Walmart on the way home from work to buy apackage of Bounty paper towels. She did not consider any...
-
A 447 gram cart (mA) slides along a very smooth track and collides with a stationary 475 gram cart (mB). A motion detector records the velocity of cart A, as shown in Figures 1 and 2. A force probe...
-
M8 Homework i Saved 1 Mayfair Company completed the following transactions and uses a perpetual inventory system. Help Save & Exit Submit Check my work 10 points eBook Print References June 4 Sold...
-
Free Response Table Problem x -6 -80 -4 -3 f(x) 1.948 1 0 -2 -2.005 -798 undefined -2 -1.995 0 1 1.995 2 2.005 6 80 802 4 3.333 3.001 undefined 2.998 2.5 2.048 23. The table above represents values...
-
5. [-/0 Points] DETAILS OSPRECALC1 2.2.106. Use algebra to find the point at which the line f(x) = -x 258 -X+ intersects the line h(x) = x+ 91 + 25 10 (x, y) = Additional Materiale MY N
-
What does the graph tells? from your own understanding. CoursHeroTranscribedText 136 DIVIDED ATTENTION COUNTED TIME BACKWARDS 134 1 2 3 130 136 UNDIVIDED ATTENTION COUNTED TIME BACKWARDS 134 5 132...
-
Q9- Consider a project with free cash flows in one year of $90,000 in a weak economy or $117,000 in a strong economy, with each outcome being equally likely. The initial investment required for the...
-
Find a least expensive route, in monthly lease charges, between the pairs of computer centers in Exercise 11 using the lease charges given in Figure 2. a) Boston and Los Angeles b) New York and San...
-
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 year-end and accounts for partial years...
-
The International Licensing Industry Merchandisers Association (LIMA; www.licensing.org) is an organization with offices worldwide. It supports merchandise licensing through education, networking,...
-
Suppose you are an international entrepreneur and want to open your own franchise somewhere in Europe. You decide to conduct research to identify the most promising franchise and learn how to become...
-
What are the advantages and disadvantages of licensing? LO.1
![Mobile App Logo](https://dsd5zvtm8ll6.cloudfront.net/includes/images/mobile/finalLogo.png)
Study smarter with the SolutionInn App