Advertisement
furas

Python - test

Nov 30th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. def manipulate(data):
  2.    
  3.     if not isinstance(data, list):
  4.         return 'Only list is allowed'
  5.  
  6.     positive_count = 0
  7.     negative_sum = 0
  8.  
  9.     for n in data:
  10.         if n > 0:
  11.             positive_count += 1
  12.         elif n < 0:
  13.             negative_sum += n
  14.                    
  15.     return [positive_count, negative_sum]
  16.  
  17. # --- tests ---
  18.  
  19. tests = [
  20.     # ( data, expected_result )
  21.     ( [1,2,3,4], [4, 0] ),
  22.     ( [1, -9, 2, 3, 4, -5], [4, -14] ),
  23.     ( {}, 'Only list is allowed' ),
  24. ]
  25.  
  26. for data, result in tests:
  27.     print(data, result, manipulate(data) == result)
  28.  
  29. # [1, 2, 3, 4] [4, 0] True
  30. # [1, -9, 2, 3, 4, -5] [4, -14] True
  31. # {} Only list is allowed True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement