Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sliding_window_array_mul(sliding, base):
- result = []
- while len(sliding) >= len(base):
- total = 0
- for element in base:
- total += element * sliding[base.index(element)]
- result.append(total)
- sliding.pop(0)
- return result
- slid_mul = lambda s, b: list(sum(list(map(lambda x: x[0]*x[1], f))) \
- for f in ( list(zip(b, ss)) for ss in list(s[x:] \
- for x in range(len(s)-len(b)+1)) ) )
- def main():
- tests = (
- [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
- [40, 30, 20, 10],
- [10, 490, 1210, 2330, 3320, 2370, 1190]
- )
- tests2 = (
- [56, -34, 20, -8, 15, 57, 16, 13, 2, -5],
- [5, 10, 15, 20],
- [80, 210, 1385, 1285, 1145, 680, 140]
- )
- tests3 = (
- [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
- [40, 30, 20, 10],
- [10, 490, 1210, 2330, 3320, 2370, 1190]
- )
- tests4 = (
- [56, -34, 20, -8, 15, 57, 16, 13, 2, -5],
- [5, 10, 15, 20],
- [80, 210, 1385, 1285, 1145, 680, 140]
- )
- assert sliding_window_array_mul(tests[0], tests[1]) == tests[2]
- assert sliding_window_array_mul(tests2[0], tests2[1]) == tests2[2]
- assert(slid_mul(tests3[0], tests3[1]) == tests3[2])
- assert(slid_mul(tests4[0], tests4[1]) == tests4[2])
- print('done')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment