Exercises for Lesson 25
Exercise 1: Summing numbers, recursively
Write a function that returns the sum of the numbers from 1 to n
. It should use a recursive approach.
def recursiveSum(n):
return 0 # TODO
For example, if we have num = 6
, then recursiveSum(num)
should return 21
.
Exercise 2: Reversing a string, recursively
Write a function that returns the reverese of a string. It should use a recursive approach.
def recursiveReverse(s):
return 0 # TODO
For example, if we have mystr = "pineapple"
, then recursiveReverse(mystr)
should return "elppaenip"
.
Exercise 3: Finding vowels in a string, recursively
Write a function that returns a list containing only the vowels in a given string (assumed to be all lowercase). It should use a recursive approach.
def recursiveVowels(s):
return [] # TODO
For example, if we have mystr = "elephant"
, then recursiveVowels(mystr)
should return ['e', 'e', 'a']
.
Exercise 4: Finding anagrams of a string, recursively
Write a function that returns a list of all of the anagrams of a given string. It should use a recursive approach.
def recursiveAnagram(s):
return [] # TODO
For example, if we have mystr = "cat"
, then recursiveAnagrams(mystr)
should return ["cat", "cta", "act", "atc", "tca", "tac"]
.