Advertisement
gruntfutuk

sliding windows test

Apr 23rd, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. ''' Beginner Exercise on sliding window array multiplication.
  2.  
  3.    NOTE < and > symbols in the below surround variable names to
  4.    indicate you can use your own name in place of what is used in
  5.    this text, they are not part of the Python syntax.
  6.  
  7.    Write a function that returns a list of results of some array
  8.    multiplications as described below.
  9.  
  10.    Your function must accept as parameters two numeric lists, we shall
  11.    call them <sliding> and <base>, and return one numeric list,
  12.    <answers>.
  13.  
  14.    Note that <sliding> will contain at least as many elements as <base>.
  15.  
  16.    Multiply each element in <base> by its corresponding element in
  17.    sliding: <base>[0] * <sliding>[0], <base>[1] * <sliding>[1],
  18.    <base>[2] * <sliding>[2]  and so on.
  19.  
  20.    Add up the results of the above. Store this as your first answer,
  21.    <answers>[0].
  22.    
  23.    Do it again, but start one element further along in the sliding list.
  24.    <base>[0] * <sliding>[1], <base>[1] * <sliding>[2], <base>[2] *
  25.    <sliding>[3], and so on.
  26.  
  27.    Add up the results of the above. Store this as your second answer,
  28.    <answers>[1].
  29.  
  30.    Repeat as many times as you can, storing each answer. You cannot go
  31.    past the point where <sliding> has fewer elements than <base>.
  32.    
  33.    Optionally, you can output to the console all of the answers. This
  34.    is not required for testing purposes.
  35.  
  36.    Return the answers list from your function. This is required for
  37.    testing.
  38.    
  39.    Use the test framework included below to test your function. Note
  40.    that the test framework may be changed from time to time, and
  41.    additional test values added, so check back. Provided you have named
  42.     function as described, it will be called automatically.
  43.  
  44.    You are welcome to provide multiple solutions in the same file.
  45.  
  46.    You function name should start with your name, followed by a name for
  47.    the function, with _ between the names,
  48.    e.g. def Fred_Blogs_tricky(sliding, base):
  49.  
  50.    You can use common libraries as part of your solution, such as numpy,
  51.    if you wish.
  52.  
  53.    The image is designed to make the challenge more clear.
  54.    
  55.    See picture for more explanation: https://i.imgur.com/O2c3jdH.jpg
  56. '''
  57.  
  58. ''' *************************************************
  59.    **     Enter your solution functions below     **
  60.    *************************************************
  61. '''
  62.  
  63.  
  64. ''' *******************************
  65.    **          TESTING          **
  66.    *******************************
  67. '''
  68.  
  69. def test_funcs(*funcs):
  70.     import traceback
  71.    
  72.     tests = [(  [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3],
  73.                 [40, 30, 20, 10],
  74.                 [10, 490, 1210, 2330, 3320, 2370, 1190] ),
  75.              (  [56, -34, 20, -8, 15, 57,16, 13, 2, -5],
  76.                 [5, 10, 15, 20],
  77.                 [80, 210, 1385, 1285, 1145, 680, 140] )              
  78.             ]
  79.    
  80.     for func in funcs:
  81.         print(f'\n\nTesting function {func.__name__}:\n')
  82.         for sliding, base, answers in tests:
  83.             result = func(sliding, base)
  84.             try:
  85.                 assert result == answers
  86.             except AssertionError:
  87.                 print(f'FAILED with {result}')
  88.                 print(f' sliding: {sliding}')
  89.                 print(f' base: base: {base}')
  90.                 print(f' expected: {answers}')
  91.             else:
  92.                 print(f'Passed: {result}')
  93.  
  94.  
  95. ''' testing functions '''
  96. if __name__ == '__main__':
  97.     func_list = [func for name, func in globals().items()
  98.                  if callable(func) and name[0:2] not in ('__')
  99.                     and name[0:4] not in ('exit', 'quit', 'get_', 'test')]
  100.     test_funcs(*func_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement