Try writing these recursion functions. Include a main()
program that calls and tests the recursive function!
sum all values in a list: sumList(L)
L: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sumList(L): 45
L: [2, 4, 6, 8]
sumList(L): 20
determine if given string is a palindrome or not: isPalindrome(s)
string: racecar
palindrome!
string: we love CS
NOT a palindrome...
determine if given list is sorted or not: isSorted(L)
L: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
isSorted(L): True
L: [9, 4, 60, 8]
isSorted(L): False
circles(cp, size, win)
replace all instances of ch in string s with new: replace(s, ch, new)
string: we love computer science!
ch: e
new: 3
w3 lov3 comput3r sci3nc3!
string: mississippi
ch: i
new: PONY
mPONYssPONYssPONYppPONY
return a list with n identical items in it: xerox(item, n)
xerox('pony', 4): ['pony', 'pony', 'pony', 'pony']
xerox(3.14, 6): [3.14, 3.14, 3.14, 3.14, 3.14, 3.14]
Hint: you can add two lists together:
>>> L1 = ['pony']
>>> L2 = ['horse']
>>> L1 + L2
['pony', 'horse']
multiply all values in a list: multiplyList(L)
L: [2, 3, 4, 5]
multiplyList(L): 120
L: [237, 0, 138, 407]
multiplyList(L): 0
find the largest item in a list: recmax(L)
L: [0, 8, 4, 5, 6, 3, 1, 2, 9, 7]
recmax(L): 9
L: [200, 44, 321, -8]
recmax(L): 321
get valid input from user -- get an integer: getInt()
integer: 5.0
integer: pony
integer: 4 4
integer: 16
16
(keep asking until you get an integer)
fourcircles(cp, size, win)
recursion is fun! :)