eltneg

sliding_window_array_multiplication

Apr 30th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. def sliding_window_array_mul(sliding, base):
  2.     result = []
  3.     while len(sliding) >= len(base):
  4.         total = 0
  5.         for element in base:
  6.             total += element * sliding[base.index(element)]
  7.         result.append(total)
  8.         sliding.pop(0)
  9.     return result
  10.  
  11. slid_mul = lambda s, b: list(sum(list(map(lambda x: x[0]*x[1], f))) \
  12.             for f in ( list(zip(b, ss)) for ss in list(s[x:] \
  13.             for x in range(len(s)-len(b)+1)) ) )
  14.  
  15. def main():
  16.     tests = (
  17.         [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
  18.         [40, 30, 20, 10],
  19.         [10, 490, 1210, 2330, 3320, 2370, 1190]
  20.     )
  21.  
  22.     tests2 = (
  23.         [56, -34, 20, -8, 15, 57, 16, 13, 2, -5],
  24.         [5, 10, 15, 20],
  25.         [80, 210, 1385, 1285, 1145, 680, 140]
  26.     )
  27.     tests3 = (
  28.             [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
  29.             [40, 30, 20, 10],
  30.             [10, 490, 1210, 2330, 3320, 2370, 1190]
  31.     )
  32.     tests4 = (
  33.         [56, -34, 20, -8, 15, 57, 16, 13, 2, -5],
  34.         [5, 10, 15, 20],
  35.         [80, 210, 1385, 1285, 1145, 680, 140]
  36.     )
  37.     assert sliding_window_array_mul(tests[0], tests[1]) == tests[2]
  38.     assert sliding_window_array_mul(tests2[0], tests2[1]) == tests2[2]
  39.  
  40.     assert(slid_mul(tests3[0], tests3[1]) == tests3[2])
  41.     assert(slid_mul(tests4[0], tests4[1]) == tests4[2])
  42.  
  43.     print('done')
  44.  
  45. if __name__ == '__main__':
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment