Skip to content

Instantly share code, notes, and snippets.

@sharonovd
Last active July 8, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharonovd/6a857b0dc47a6c3c8a6919849b0b2ef4 to your computer and use it in GitHub Desktop.
Save sharonovd/6a857b0dc47a6c3c8a6919849b0b2ef4 to your computer and use it in GitHub Desktop.
def mult(v1, v2):
"""vX is array of int pairs, e. g. [(6, 2), [7,3 ]] which is equivalent to vector (6, 6, 7, 7, 7) """
result = 0
position1 = -1
position2 = -1
counter1 = 0
counter2 = 0
while True:
assert counter1 >= 0 and counter2 >= 0
if counter1 == 0:
if position1 == len(v1) - 1 and position2 == len(v2) - 1:
return result
position1 += 1
counter1 = v1[position1][1]
if counter2 == 0:
if position1 == len(v1) - 1 and position2 == len(v2) - 1:
return result
position2 += 1
counter2 = v2[position2][1]
assert position1 < len(v1) and position2 < len(v2), 'Vector size seems not equal!'
result += v1[position1][0] * v2[position2][0]
counter1 -= 1
counter2 -= 1
assert mult([(6, 2), (7, 3)], [(1, 1), (3, 1), (5, 2), (1, 1)]) == 101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment