""" Authors: Peter Mawhorter Consulted: Date: 2021-11-7 Purpose: Solutions for lab 12 part 1 on fruitful recursion. """ def sumUpTo(n): """ Returns the sum of all positive integers up to and including the specified number. """ if n == 0: # Base case: return 0 return 0 else: # Recursive case: compute sum of smaller numbers; add n return n + sumUpTo(n - 1) def addToEach(nums, add): """ Returns a list where the 'add' number has been added to each number in the given list of numbers. """ if len(nums) == 0: # Base case: empty list return [] else: # Recursive case: split into first + rest first = nums[0] rest = nums[1:] # Add to first; recurse on rest return [ first + add ] + addToEach(rest, add) def countLs(word): """ Returns the number of times 'l' or 'L' appears in the given word. """ if len(word) == 0: # Base case: no letters -> no 'l's return 0 else: # Recursive case: Split into first and rest first = word[0] rest = word[1:] # Count the first letter or not if first in 'lL': count = 1 else: count = 0 # Add 1 (or 0) to count from rest return count + countLs(rest)