To get credit for these two problems the code must not use: for or while loops,
ID: 3827764 • Letter: T
Question
To get credit for these two problems the code must not use: for or while loops, functions from other modules, and sum(). Using list slices. type(). and ien() is OK. a) Write a recursive function 1stsum(1st) that computes the sum with the addition(+) operator of all elements of 1st assuming the list is a flat list The sum of all elements in [] is 0 by definition. Example: lst_sum{[4.1, 3.2]) returns 10, while tsf_sum([4.3, [2]]) fails because the list argument is not flat -- it has a nested sublist. This problem is the easy case. b) Write a function 1st_sum_all(tst) that returns the sum of all elements in list 1st and in its all nested sublists, if any exist. For instance, the call lst_sum_all([1.||[2|).3).4.[[[||5]].6]]]]) returns 21.Explanation / Answer
Hi,
Please find below the answer-
Ans a) def RecursiveSum(piece):
Ans b) def nested_list_sum(L):
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.