Previous Lecture Lecture 6 Next Lecture

Lecture 6, Thu 04/18

Inheritance, Midterm review, Asymptotic Behavior

Plan for today

Midterm Review

Student Questions

When a parent and child class have the same method and you call that method on an instance of the child class, which version of the method runs first?

Algorithm Analysis

import time

def function_timing(func_name, func_param):
    print("Starting", func_name)
    start = time.time()
    func_name(func_param)
    end = time.time()
    return end - start

def list_insert(total):
    num_list = []
    for num in range(total):
        num_list.insert(0, num)

def list_append(total):
    num_list = []
    for num in range(total):
        num_list.append(num)

num_items = 100
print(f"Testing functions using {num_items} elements")

f1_time = function_timing(list_insert, num_items)
print("list_insert:", f1_time)

f2_time = function_timing(list_append, num_items)
print("list_append:", f2_time)

Asymptotic Behavior

Recursion

A simple recursive function:

def count(n):
    if n == 0:
        print("Hello!")
    else:
        print(n)
        count(n-1)

Call it using count(3) to see it produce:

3
2
1
Hello!

How can we modify this function to now output the following?

3
2
1
Hello!
1
2
3

You might find it helpful to run through this example in Python Tutor to see the state of the function calls and the arguments that they are called with.