0
RECURSIONS
can someone please explain this particular code in a less advanced way for me, especially this one ---------> return list[0]**2 + calc(list[1:]) def calc(list): if len(list)==0: return 0 else: return list[0]**2 + calc(list[1:]) list = [1, 3, 4, 2, 5] x = calc(list) print(x)
1 Respuesta
+ 3
list[0]**2 + calc(list[1:])
Calculating list[0] power 2 + (again calling function remaining list with [3, 4,2,5] so now list[0] for new function is 3
Finally returning like
1**2 + ( 3**2 + ( 4**2 + (2**2 + (5**2 +( 0 ) ))))
= 1 + (9 + ( 16 + ( 4 + ( 25 + 0) ) ) ) = 55