CSC148H5 Lecture Notes - Lecture 12: University Of Toronto Mississauga, Empty String, If And Only If

29 views4 pages

Document Summary

Csc148h5s - introduction to computer science (winter 2017) If you remove the first element from a list, then the rest of it is still a list, and a smaller list. You can split list in middle which gives you two smaller lists. Let"s write a recursive function to return the sum of a list of integers. Base case: empty list []; answer is 0 here. Answer is 1 + whatever the sum of the rest of the list is def sum_list (lst): Base case this time: a list with no nesting. """(list of int possibly with nesting) -> int. Return sum of lst. lst could be nested. If isinstance(item, int): total = total + item. Else : # item is a list total = total + sum_list(item) Let"s show that this function works on [3, 4, [1, 2]] Already you know that it works for [1, 2]. Part of our base case; no nesting total starts off as 0.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents