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)

7th Feb 2022, 12:06 PM
NWACHUKWU PRECIOUS CALEB
NWACHUKWU PRECIOUS CALEB - avatar
1 Answer
+ 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
7th Feb 2022, 12:53 PM
Jayakrishna 🇼🇳