Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' Beginner Exercise on sliding window array multiplication.
- NOTE < and > symbols in the below surround variable names to
- indicate you can use your own name in place of what is used in
- this text, they are not part of the Python syntax.
- Write a function that returns a list of results of some array
- multiplications as described below.
- Your function must accept as parameters two numeric lists, we shall
- call them <sliding> and <base>, and return one numeric list,
- <answers>.
- Note that <sliding> will contain at least as many elements as <base>.
- Multiply each element in <base> by its corresponding element in
- sliding: <base>[0] * <sliding>[0], <base>[1] * <sliding>[1],
- <base>[2] * <sliding>[2] and so on.
- Add up the results of the above. Store this as your first answer,
- <answers>[0].
- Do it again, but start one element further along in the sliding list.
- <base>[0] * <sliding>[1], <base>[1] * <sliding>[2], <base>[2] *
- <sliding>[3], and so on.
- Add up the results of the above. Store this as your second answer,
- <answers>[1].
- Repeat as many times as you can, storing each answer. You cannot go
- past the point where <sliding> has fewer elements than <base>.
- Optionally, you can output to the console all of the answers. This
- is not required for testing purposes.
- Return the answers list from your function. This is required for
- testing.
- Use the test framework included below to test your function. Note
- that the test framework may be changed from time to time, and
- additional test values added, so check back. You should include
- the name of your function, without the (), in the test call on
- the last line.
- You are welcome to provide multiple solutions in the same file.
- You function name should start with your name, followed by a name for
- the function, with _ between the names,
- e.g. def Fred_Blogs_tricky(sliding, base):
- You can use common libraries as part of your solution, such as numpy,
- if you wish.
- The image is designed to make the challenge more clear.
- See picture for more explanation: https://i.imgur.com/O2c3jdH.jpg
- '''
- Stuart_Moore_lambda = lambda sliding, base: [sum(x*y for x, y in zip(sliding[counter:], base)) for counter in range(0, len(sliding)-len(base)+1)]
- def Stuart_Moore_shorty(sliding, base):
- return [sum(x*y for x, y in zip(sliding[counter:], base)) for counter in range(0, len(sliding)-len(base)+1)]
- def Mark_Geyzer_mul_sliding(sliding, base):
- import operator
- def _mul_sliding_helper(slider, results):
- if len(slider) < len(base):
- return results
- results.append(sum(operator.mul(*numbers) for numbers in zip(slider, base)))
- return _mul_sliding_helper(slider[1:], results)
- return _mul_sliding_helper(sliding, [])
- def Stuart_Moore_rangy(sliding, base):
- ''' using range, zip, and comprehension '''
- totals = []
- for counter in range(0, len(sliding)-len(base)+1):
- totals.append(sum(x*y for x, y in zip(sliding[counter:], base)))
- return totals
- def Stuart_Moore_zippy(sliding, base):
- ''' using zip, pop and comprehension '''
- slide = sliding[:]
- totals = []
- baselen = len(base) - 1
- while slide[baselen:]:
- totals.append(sum(x*y for x,y in zip(slide, base)))
- slide.pop(0)
- return totals
- def Stuart_Moore_windy(sliding, base):
- ''' without zip, pop or comprehension '''
- slide = sliding[:]
- totals = []
- baselen = len(base) - 1
- while slide[baselen:]:
- total = 0
- slide_items = iter(slide)
- for base_item in base:
- slide_item = next(slide_items)
- total += base_item * slide_item
- totals.append(total)
- slide = slide[1:]
- return totals
- def Stuart_Moore_slidy(sliding, base):
- ''' using a sliding window '''
- def window(iterable, size=2):
- itered = iter(iterable)
- slit = []
- for _ in range(0, size):
- slit.append(next(itered))
- yield slit
- for element in itered:
- slit = slit[1:] + [element]
- yield slit
- slide = window(sliding, len(base))
- totals = []
- for slide_slice in slide:
- totals.append(sum(x*y for x,y in zip(slide_slice, base)))
- return totals
- def Yap_Seng_Kuang_m(sliding,base):
- return [sum([i*j for i,j in zip(sliding[k:k+len(base)],base ) ]) for k in range(0,len(sliding)-len(base)+1)]
- ''' *******************************
- ** TESTING **
- *******************************
- '''
- def test_funcs(*funcs):
- import traceback
- tests = [( [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
- [40, 30, 20, 10],
- [10, 490, 1210, 2330, 3320, 2370, 1190] ),
- ( [56, -34, 20, -8, 15, 57,16, 13, 2, -5],
- [5, 10, 15, 20],
- [80, 210, 1385, 1285, 1145, 680, 140] )
- ]
- for func in funcs:
- print(f'\n\nTesting function {func.__name__}:\n')
- for sliding, base, answers in tests:
- result = func(sliding, base)
- try:
- assert result == answers
- except AssertionError:
- print(f'function {func.__name__}: FAILED with {result}')
- print(f' for data, sliding: {sliding}')
- print(f' and base: base: {base}')
- print(f' expected: {answers}')
- else:
- print(f'function {func.__name__}: {result}')
- ''' testing functions '''
- if __name__ == '__main__':
- func_list = [func for name, func in globals().items()
- if callable(func) and name[0:2]
- not in ('__', 'te', 'ex', 'qu', 'ge')]
- test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement