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:
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...
-
DepreciationSL, DDB, SYD, Act., and MACRS On January 1, 2009, Locke Company, a small machine-tool manufacturer, acquired for $1,260,000 a piece of new industrial equipment. The new equipment had a...
-
Frannie tosses a coin 12 times and gets five heads and seven tails. In how many ways can these tosses result in (a) Two runs of heads and one run of tails; (b) Three runs; (c) Four runs; (d) Five...
-
An increasing number of electronic files are secured by encryption measures for added levels of protection. Such measures may make it difficult for law enforcement officials to access computer hard...
-
Palmer Corporation issued 3,000 7%, 5-year, $1,000 bonds dated January 1, 2012, at face value. Interest is paid each January 1. (a) Prepare the journal entry to record the sale of these bonds on...
-
8. A dielectric slab of dielectric constants k is slowly inserted inside the parallel plate capacitor having plate area A and separation between plates d as shown in figure. If dimensions of...
-
Consider the following scenario: A school is attempting to develop a policy manual. It wants to make aggressive behavior an expellable offense. It also wants to acknowledge bad behavior as a...
-
Suppose you have an LFSR with state bits (also known as the seed) (85, 84, 83, 82, 81, 80) = (0, 0, 0, 1, 0, 0) and tap bits (also known as feedback coefficients) (P5, P4, P3, P2, P1, Po) = (0, 1, 1,...
-
POEM is an acronym for paid, owned, and earned media. Give three examples of each of these in the media you use every day. Which do you think are the most effective? Why?
-
Comfy Chairs Co manufactures a standard office chair in Division A. The standard chair is improved in Division B with extra cushioning and easy-run castors. The manager of Division A has offered...
-
What are bankers looking for when they evaluate working capital.
-
Positioning can also become a brainstorming technique. Think of two opposites for two different points of interest for your target audience. Your X-axis might be a place and your Y-axis might be...
-
What is e-business.
-
The electronic absorption spectrum of molecular oxygen has an absorption band with a maximum at 150 nm and a continuum range starting at around 100 nm. Q7B What is the origin of the continuum range?...
-
Show that gj concave AHUCQ Abadie For nonnegative variables, we have the following corollary.
-
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...
-
Explain how the control variate technique is implemented.
-
Suppose that in September 2013 a company takes a long position in a contract on May 2014 crude oil futures. It closes out its position in March 2014. The futures price (per barrel) is \($88.30\) when...
-
A U.S. Treasury bond pays a 7% coupon on January 7 and July 7. How much interest accrues per \($100\) of principal to the bond holder between July 7, 2013, and August 9, 2013? How would your answer...
Study smarter with the SolutionInn App