A sparse vector is a vector whose entries are almost all zero, like [1, 0, 0, 0, 0, 0, 0, 2, 0]. Storing all those zeros wastes memory and dictionaries are commonly used to keep track of just the nonzero entries. For example, the vector shown earlier can be represented as {0:1, 7:2}, since the vector it is meant to represent has the value 1 at index 0 and the value 2 at index 7.

1)Write a function called sparse_add that takes two sparse vectors stored as dictionaries and returns a new dictionary representing their sum.
2)Write another function called sparse_dot tha calculates the dot product of two sparse vectors.

Looks OK excersise, post your code if you get any questions to ask.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.