Advertisement
gruntfutuk

arrayPack_numbers

Jul 5th, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. ''' array packing exercise
  2.  
  3.    write a function that receives a list of positive integers, each less than 256,
  4.    packs those numbers into one large number that is returned such that each number
  5.    takes up one byte position starting from least significant byte for first list
  6.    element, through to most significant byte for last list element
  7. '''
  8.  
  9. def arrayPacking(arr):
  10.     return sum([num << (index * 8) for index, num in enumerate(arr)])
  11.  
  12. ''' test data:
  13.        list of tuples each contain list of integers and expected answer '''
  14. tests = [([24, 85, 0], 21784),
  15.          ([235, 129, 57], 3768811),
  16.          ([1, 2, 3], 197121),
  17.          ([56, 13, 111], 7277880)
  18.         ]
  19.  
  20. for test_list, answer in tests:
  21.     result = arrayPacking(test_list)
  22.     try:
  23.         assert  result == answer
  24.     except AssertionError:
  25.         print(f'** ERROR ** Expected {answer} for {test_list}, got {result} ')
  26.     else:
  27.         print(f'{test_list} gives {result}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement