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. Provided you have named
- function as described, it will be called automatically.
- 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
- '''
- ''' *************************************************
- ** Enter your solution functions below **
- *************************************************
- '''
- ''' *******************************
- ** 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'FAILED with {result}')
- print(f' sliding: {sliding}')
- print(f' base: base: {base}')
- print(f' expected: {answers}')
- else:
- print(f'Passed: {result}')
- ''' testing functions '''
- if __name__ == '__main__':
- func_list = [func for name, func in globals().items()
- if callable(func) and name[0:2] not in ('__')
- and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
- test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement