Lecture 2 - Outline

Learning Objectives

Announcements

Plan

#practice with function composition
def f(x): return 4*x + 2
def g(x): return f(x//2 + 1)
def h(x):
   print(f(x-3))
   x -= 1
   print(g(x)+x)
   x %= 4
   return g(f(x) % 4) // 2
print(1 + h(6))

Functions Written In Lecture

def digitCount(n):
    n = abs(n)
    if(n == 0): return 1
    digits = 0
    while(n > 0):
        n //= 10
        digits += 1
    return digits
    
def isPrime(n):
    if(n < 2): return False
    elif(n == 2): return True
    elif(n % 2 == 0): return False
    maxFactor = int(math.sqrt(n))
    for factor in range(3, maxFactor + 1):
        if(n % factor == 0):
            return False
    return True

def nthPrime(n):
    # n = 0
    found = 0
    guess = 0
    # found = 1
    while(found <= n): # n = 0
        guess += 1
        # guess = 2
        if(isPrime(guess)):
            found += 1
    return guess

Extra Practice

#practice from the course notes
def f(x):
    print("A", end="")
    if (x == 0):
        print("B", end="")
        print("C", end="")
    elif (x == 1):
        print("D", end="")
    else:
        print("E", end="")
        if (x == 2):
            print("F", end="")
        else:
            print("G", end="")
    print("H")

Moved Topics

HW2

Tests: helper functions, for loops, while loops, conditionals

Up Next