Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' array packing exercise
- write a function that receives a list of positive integers, each less than 256,
- packs those numbers into one large number that is returned such that each number
- takes up one byte position starting from least significant byte for first list
- element, through to most significant byte for last list element
- '''
- def arrayPacking(arr):
- return sum([num << (index * 8) for index, num in enumerate(arr)])
- ''' test data:
- list of tuples each contain list of integers and expected answer '''
- tests = [([24, 85, 0], 21784),
- ([235, 129, 57], 3768811),
- ([1, 2, 3], 197121),
- ([56, 13, 111], 7277880)
- ]
- for test_list, answer in tests:
- result = arrayPacking(test_list)
- try:
- assert result == answer
- except AssertionError:
- print(f'** ERROR ** Expected {answer} for {test_list}, got {result} ')
- else:
- print(f'{test_list} gives {result}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement