Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'Work'
- '''
- Star unpacking is similar to list processing features of other functional languages. Lists can be split into head and
- tails components.
- '''
- items = [1, 10, 7, 4, 5, 9]
- head, *tail = items
- # Functions can use head and tail splitting to carry out some recursive algorithm.
- # Ex:
- def summation(items):
- head, *tail = items
- return head + summation(tail) if tail else head
- items = [2923,239,21,3,5,-12,239,-1233]
- x = sum(items)
- y = summation(items)
- print('BIF sum(): ',x, '\n', "summation(): ", y, '\n', 'x == y ? ', x == y)
- # If items greater than 1 return the sum of all tail elements and the head. If not return just the head
- # (cannot unpack 0 values).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement