Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given a list of numbers num. Write a function Exercisel (num) returning a list ans, such that ans [i] is equal to

  

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 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: Kin Lo, George Fisher

3rd Edition Vol. 1

133865940, 133865943, 978-7300071374

More Books

Students also viewed these Programming questions

Question

failures of public policies

Answered: 1 week ago

Question

How do you add two harmonic motions having different frequencies?

Answered: 1 week ago