A sparse vector is a vector whose entries are almost all zero, like [1, 0, 0, 0, 0, 0, 3, 0, 0, 0]. Storing all those zeros in a list wastes memory, so programmers often use dictionaries instead to keep track of just the nonzero
entries. For example, the vector shown earlier would be represented as {0:1, 6:3}, because the vector it is meant to represent has the value 1 at index 0 and the value 3 at index 6.
a. The sum of two
For example, the sum of [1, 2, 3] and [4, 5, 6] is [5, 7, 9]. Write a function called sparse_add that takes two sparse vectors stored as dictionaries and returns a new dictionary representing their sum.
b. The dot product of two vectors is the sum of the products of corresponding elements. For example, the dot product of [1, 2, 3] and [4, 5, 6] is 4+10+18, or 32. Write another function called sparse_dot that calculates
the dot product of two sparse vectors.
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- Write in javaarrow_forwardWrite an ASM program which: 1. Fills in an array Vector with at most 50 signed double-word integers from the keyboard; 2. Computes the sum of all the negative values in Vector; 3. Counts the number of all the positive values in Vector; 4. Finds the minimum value between position I and position J of Vector; 5. Checks whether Vector is a palindrome or not Your program will display the following interaction with you (things in reds are your inputs) What ia the aize N of Veetor?> 13 Khat are the 13 valuna in Vector?> - -3 -17 o -100 -30 +2 -30 -100 0 -17 +3 -1 Size of Vector la N- 13 Vector - -1 +3 +17 0 -100 -30 +2 -30 -100 +17 + -1 The sun of all the negative values in Vector isi Sun - -262 The mamber of all the positive values in Vector is: Count -5 Please give me two valums I and J wach that 15 I SISN> 2 7 I - 2 and J- 1, and The ninimum value between position 2 and 7 of Vector ia: Minimu - -100 Vector ia a palindrone because it seads the same way in both directiona. Repeat with a nex…arrow_forwardSuppose an array has n elements. This _____ sorts to sort and array works as follows: Find the smallest element and place it in the first position. Then find the smallest of the remaining n-1 elements and place it in the second position. Repeat on n-2 elements, n-3 elements, ..., until the array is sorted.arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY