CS 111 w20 lecture 13 outline

1 Debugging

1.1 Example Bugs

# takes a list of numbers and returns True if it is sorted smallest to largest
def is_sorted(nums):
    for i in range(len(nums)):
        if i > i + 1:
            return False
    return True
  • needs to compare values rather than indexes
  • needs to use range(len(nums) - 1) to avoid an index error
  • do we need to loop over indexes?
# force x to be between 0 and 1
def clamp_01(x):
    if x < 0:
        print(0)
    elif x > 1:
        print(1)
    else:
        print(x)
  • needs to use return instead of print
def find_max(nums):
    max_num = nums[0]
    for num in nums:
        if num > max_num:
            return num
  • needs to update max_num and the return it after the loop
  • Python has built-in max and min functions
words = ["alabaster", "alarm", "albatross", "album", "alfalfa"]
vowels = "aeiou"
for word in words:
    vowel_count = 0
for c in word:
    if c in vowels:
        vowel_count += 1
print(word, "has", vowel_count, "vowels")
  • loop over word needs to be inside the loop over words

1.2 Lotka-Volterra Simulation

1.2.1 Background

  • simple model of predator-prey interactions
  • simulate the populations of a prey species and and predator species
    • for each of some number of time steps, calculate the population of each species
    • see how they change over time
  • basic form has two key equations:
    • change in prey population (\(change_x(x, y)\)) is \(x(\alpha - \beta y)\)
    • change in predator population (\(change_y(x, y)\)) is \(-y(\gamma - \delta x)\)
    • \(x\) is number of prey (e.g., rabbits)
    • \(y\) is number of predators (e.g., foxes)
    • \(\alpha\), \(\beta\), \(\gamma\), and \(\delta\) are values describing the interaction of the two species
  • estimate the population at a time step in two steps
    • \(y_{mid} = y + 0.5(change_x(x, y))\), \(x_{mid} = x + 0.5(change_x(x, y))\)
    • \(y_{final} = y + change_y(x_{mid}, y_{mid})\), \(x_{final} = x + change_x(x_{mid}, y_{mid})\)

1.2.2 Debugging

  • read through and cirle anything you think could be an issue
  • run, index error, devise fix
  • run, only 0 to 1 on x-axis, y is a straight line, plt.plot uses range(len(y)) for x-axis, why is it only two points?
  • run, why only orange?
  • run, math must be wrong, look over carefully

2 Newton's method

Issac Newton devised an effective way to estimate the square root of a number. You start with a simple guess like the number divided by two. Then, you repeatedly refine your guess with the following formula: \(newguess = \frac{guess+ \frac{x}{guess}}{2}\) where \(x\) is the original number. It turns out that it doesn't take many refinements to get very close to the exact square root.

Write a function that takes a number and uses Newton's Method to return an estimate of the square root of that number. How many times should you refine the estimate? Until it converges (i.e., the difference between the existing estimate and an improved estimate gets very small). You can use a while loop to apply the above formula until this occurs.

3 Quiz

  • Average 26.8, median 27
  • Main confusions:
    • print inside a loop gets repeated
    • looping over values vs looping over indexes
    • return ends a function, even if the return is inside a loop

status-week4.png

  • notional machine
  • arithmetic, cpu
  • assignment, labels in memory
  • print, send data to screen
  • functions, separate definition and execution
    • call, pass arguments to function execution, move instruction pointer
    • return, send data to program that called the function, move instruction pointer
    • scope, data stays within the function (except for aliases of mutable data)
  • conditionals, control instruction pointer
    • Boolean values
    • Boolean operators
  • loops, iteration
    • for, definite
    • while, indefinite
  • sequences
    • lists
    • strings
    • tuples