Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Write a function that takes an array/list of numbers and returns a number such that
- #Explanation total([1,2,3,4,5]) => 48
- #1+2=3--\ 3+5 => 8 \
- #2+3=5--/ \ == 8+12=>20\
- # ==>5+7=> 12 / \ 20+28 => 48
- #3+4=7--\ / == 12+16=>28/
- #4+5=9--/ 7+9 => 16 /
- #if total([1,2,3]) => 8 then
- #first+second => 3 \
- # then 3+5 => 8
- #second+third => 5 /
- #--------------------------------------------------------------------------------------
- def total(arr):
- arr_scrap = []
- for i in range(1, len(arr)):
- arr_scrap.append( arr[i] + arr[i-1] )
- arr = arr_scrap
- del arr_scrap
- if len(arr) == 1:
- return arr[0]
- else:
- return total(arr)
- print(total([1,2,3,4,5,6,7,8,9,10]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement