3 ms·
For example, trying to solve this problem (CodeJam 2008 minimum scalar product): You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar
by AncientPC 14y ago
For example, trying to solve this problem (CodeJam 2008 minimum scalar product):
You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+...+xnyn.
Haskell:
sum(zipWith (*) [1,2,3][3,2,1])
Python is not inherently functional, just supports a few convenience idioms. It has zip, but no zipWith. As a result you have to use list comprehension to perform the same:
sum([x*y for (x,y) in zip([1,2,3][3,2,1])])
- Lycanthrope 14y agoYou can use map with several iterables: sum(map(operator.mul, [1,2,3], [3,2,1]))