Python Foundations, Lab 3: Practice with Functions¶
Instructor: Wesley Beckner
Contact: wesleybeckner@gmail.com
In this lab we will continue to practice writing functions
✍🏽 L3 Q1: Basic Function Writing¶
Part A¶
Write a Python function to find the maximum of three numbers
# Cell for L3 Q1 A
def max_num(a, b, c):
if (a > b) and (a > c):
return a
elif (b > a) and (b > c):
return b
else:
return c
max_num(100, 20, 3)
100
Part B¶
Write a Python function to multiply all the numbers in a list
Example:
Sample List : (8, 2, 3, -1, 7)
Expected Output : -336
# Cell for L3 Q1 B
def mult_list(a):
val = 1
for i in a:
val *= i
return val
mult_list([8, 2, 3, -1, 7])
-336
Part C¶
Write a Python program to reverse a string
Example:
Sample String : "1234abcd"
Expected Output : "dcba4321"
# Cell for L3 Q1 C
def rev_string(a):
return a[::-1]
rev_string("1234abcd")
'dcba4321'
❄️ L3 Q2: Finding Unique Elements¶
Write a Python function that takes a list and returns a new list with unique elements of the first list
Example:
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
# Cell for L3 Q2
def find_unique(a):
b = []
for i in a:
if i not in b:
b.append(i)
return b
find_unique([1,2,3,3,3,3,4,5])
[1, 2, 3, 4, 5]
❗ L3 Q3: Computing Factorials¶
Write a Python function to calculate the factorial of a number (any non-negative integer). The function accepts the number as an argument
Example:
Input: 4
Output: 24
# Cell for L3 Q3
def fact(a):
b = 1
for i in range(1,a+1):
b *= i
return b
fact(4)
24
Practicing our hand at functions by implementing recursion¶
We will practice the last exercise we did yesterday, this time using what we've learned about functions to write a recursive function
A recursive function is a function that makes a call to itself. This is particularly well suited for tasks that would otherwise require a lot of memory to hold intermediary solutions (when they are combined with a strategy called memoization which we will shy away from here). The Fibonnaci sequence is a good place for such a problem. So is a factorial calculation. See here below
def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""
if x == 1:
return 1
else:
# one trick to note that is common with recursive functions
# is that the incrementation in the key variable is done
# in the call to the function itself (vs a for loop)
return (x * factorial(x-1))
n = 3
factorial(n)
6
In the above, we have on lines 5 and 6, what we call the baseline solution. It is the answer to the simplest subset of the problem. When we build a recursive tree, it is useful to think of the smallest problem, and work upwards, providing this leaf solution to nodes further up on the recursive tree. Like so:
# 1! = 1
# 2! = 2 * 1!
# 3! = 3 * 2!
# N! = N * (N-1)! ... like ... like so
This problem is intended to be a fun way to wrap our heads around functions... don't stress. If you want to see a further breakdown of the factorial problem, check out this link
🛺 L3 Q4: Recursive Functions¶
Using recursion, complete the Fibonacci sequence up to the 10th place using recursion and any or all of the following:
* if
elif
else
* range
The Fibonacci sequence with indices:
0. 0
1. 1
2. 1
3. 2
4. 3
5. 5
6. 8
7. etc..
Example input:
index = 9
Example output:
Index: 9
Value: 34
# Cell for L3 Q4
def fib(a):
if a == 0:
return 0
elif a == 1:
return 1
else:
return fib(a-1) + fib(a-2)
fib(9)
34