Lab 12: Recursion
The main goal this lab is for you to get a more practice with recursion.
Introductions
Introduce yourself to your lab partner(s). What are your favorite winter holiday activities?
Recursion Tips
What is the base case or cases? What is the simplest instance of the problem you can solve?
What is the recursive step?
Task
Complete the methods in Lab12.py, as shown below. Except as noted, don't use any built-in Python functions (other than len()) and don't use any for loops.
# Lab 12
# YOUR NAMES & EMAIL HERE
# Discuss this with your lab partner
def upAndDown(n):
print("Down:", n)
if (n > 0):
upAndDown(n-1)
print("Up:", n)
return 0
# n! = 1*2*3*...*(n-1)*n
# no for loops or Python library functions
def fac(n):
return 0
# sum of the first n positive integers
# 1 + 2 + 3 + ... + (n-1) + n
# no for loops or Python library functions
def sum(n):
return 0
# return the nth Fibonacci number
# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
# no for loops or Python library functions
def F(n):
return 0
# find the max in the list
# non-recursive
def maxR(lst):
return maxRhelper(lst,0)
# find the max in the list starting from index p
# recursive
# no for loops or Python library functions
def maxRhelper(lst, p):
return 0
# find the minimum item in the list
def minR(lst):
return minRhelper(lst,0)
# find the minimum item in the list starting from index p
# recursive
# no for loops or Python library functions
def minRhelper(lst, p):
return 0
# find the number of times item v occurs in the list
def countItems(lst, v):
return countItemsRhelper(lst,v,0)
# find the number of times item v occurs in the list
# starting from index p
# no for loops or Python library functions
def countItemsRhelper(lst, v, p):
return 0
# Output the number of digits in a given number
# Hint: use //
# no for loops or Python library functions
def digits(n):
return 0
# Convert the integer value to a String representing its binary value
# Example: 12022008 -> 101101110111000011111000
# Example: 10 -> 1010
# Note: 10 is even, so rightmost bit is 0
# divide 10 by 2, resulting in 5
# 5 is odd, so next bit is 1
# divide 5 by 2, resulting in 2
# 2 is even, so next bit is 0
# divide 2 by 2, resulting in 1
# 1 is odd, so next bit is 1
# Example: 1 -> 1
# Example: 0 -> 0
# Hint: Use // and % operators
# no for loops or Python library functions
def binary(n):
return "0"
# Multiply the input values using only addition.
# no for loops or Python library functions
# Hint: 5*7 = 7 + 4*7
def multiply(m, n):
return 0
# Ackermann's Function
# A(m,n) = n+1 if m=0
# A(m,n) = A(m-1,1) if n=0
# A(m,n) = A(m-1,A(m,n-1)) otherwise
# Note: This is a really C-R-A-Z-Y function!
# See http://mathworld.wolfram.com/AckermannFunction.html
def Ackermann(m, n):
return 0
# Determine all the permutations of a given input string
# Example:
# input: "abc"
# output: ["abc", "acb", "bac", "bca", "cab", "cba"]
# Hint: use the : array slicing operator
# recall s[i] is the ith character in the string
# s[0:i] is the substring from positions 0 through i-1 inclusive
# s[i:0] is the substring from positions i through the end of the string
# Generally recursive, but using for loops is ok
def permutations(s):
return list()
def main():
# Discuss this with your lab partner
print("upAndDown - explain this output")
upAndDown(5)
print("Factorials")
# 1 1 2 6 24 120 720 5040 40320 362880
for i in range(10):
print(fac(i), end=" ")
print()
print("Sums")
# 0 1 3 6 10 15 21 28 36 45
for i in range(10):
print(sum(i), end=" ")
print()
print("Fibonacci Numbers")
# 0 1 1 2 3 5 8 13 21 34 55 89 144
for i in range(13):
print(F(i), end=" ")
print()
print("max")
# 9
values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8]
print(maxR(values))
print("min")
# 1
values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8]
print(minR(values))
print("countItems")
# Opening lines from the song "Across the Universe" by the Beatles
text = "\
Words are flowing out like endless rain into a paper cup\n\
They slither wildly as they slip away across the universe"
print(text)
print(countItems(text, " ")) # 19
print(countItems(text, "a")) # 8
print(countItems(text, "x")) # 0
print("digits")
print(digits(0)) # 1
print(digits(9)) # 1
print(digits(54321)) # 5
print("binary")
print(binary(0)) # 0
print(binary(1)) # 1
print(binary(10)) # 1010
print(binary(12022008)) # 101101110111000011111000
print("multiply")
print(multiply(5,7)) # 35
print(multiply(0,0)) # 0
print(multiply(1,1)) # 1
print("Ackermann")
print(Ackermann(3,0)) # 5
print(Ackermann(0,2)) # 3
print(Ackermann(3,3)) # 61
print(Ackermann(5,5)) # discuss why this fails, then comment out this line
print("permutations")
# ['abcd', 'abdc', 'acbd', 'acdb', ..., 'dcba']
perms = permutations("abcd")
print(perms)
main()
When you are finished, email your lab files to your lab partner(s) and instructor.