Session 12

Breaking Problems Down,
Building Programs Up

CS 1510
Introduction to Computing

Opening Exercise: Another Infinite Series

Here is another cool expression that converges to a specific value:

    1/0! + 1/1! + 1/2! + 1/3! + 1/4! + ...

What is the value?

Recall that ! means 'factorial', and refers to the product all integers from 1 to the number. So, 4! = 4 * 3 * 2 * 1 = 24. By definition, 0! = 1.

Write a Python program that computes the sum for n terms, whatever n is. Start your accumulator with a value of 1, and compute n terms up to the one that contains n!.

Lessons from the Lab

The goal of Lab 6 was to introduce you the idea of methods and to give you more practice writing loops. We used strings for both.

Task 1 asked you to experiment with string case methods. A method is a function that is associated with a specific kind of object. A couple of things to note:

Task 2 asked you to write code to compute the Scrabble score for a string. This can be done with an ordinary for loop that determines which point-value group each letter is in:

    score = 0
    for ch in word:
        if ch in one_point_letters:
            value = 1
        elif ch in two_point_letters:
            value = 2
        elif ch in three_point_letters:
            value = 3
        elif ch in four_point_letters:
            value = 4
        elif ch in five_point_letters:
            value = 5
        elif ch in eight_point_letters:
            value = 8
            value = 10
        score += value

Yes, that is the running total pattern again. It really does show up in a lot of different situations. It would be nice not to have to write such a long if statement... Soon.

Finally, Task 3 asked you to write code to compute a student's average grade from a comma-separated string of grades. As noted in the assignment, this extends an idea we had seen in class the day before. There, we printed items separated by commas in a string; here we wanted to tally them up and find their average.

    total = 0     # new, for running total
    count = 0     # new, for running count

    start = 0
    index = 0
    while index < len(grades):
        if grades[index] == ',':
            grade = int(grades[start:index])     # instead
            total += grade                       # of
            count += 1                           # printing
            start = index + 1
        index += 1

    grade = int(grades[start:])                  # instead
    total += grade                               # of final
    count += 1                                   # print

    average = total / count

A couple of things to note:

We can write code that does remarkable things with relatively simple constructs like if, for, and while, coupled with simple sequences of statements. They are like the atoms we find in the periodic table: the basic building blocks of the computational universe.

Lessons from the Homework

Spec 1 asked you to compute a really big number and then put that number into context. The solution looks a lot like the solution to the tasks on Homework 2, with a short loop thrown in

    total_grains = 0
    for i in range(64):
        new_grains = 2**i
        total_grains += new_grains

That's all. A few things stand out to me:

I liked the idea behind Spec 2 because it is a great example of an algorithm. We all know how to multiply numbers (or learned how to, once), but we rarely think about the fact that there are other ways to do it. With very simple tools: doubling, halving, and adding, we can multiply quite effectively. The principle that underlies the so-called Russian Peasant algorithm is useful even today with powerful computers, because the principle can be used to compute the products of very large numbers more efficiently than native hardware!

Again, the core solution is a humble while loop:

    product = 0
    while n > 0:
        if n % 2 == 1:
            product += m
        m *= 2
        n //= 2

The spec asked us to let users compute multiple products, so I used a version of the "loop and a half" pattern:

    while True:
        # READ m
        if m == -1:
        # READ n
        # CALCULATE PRODUCT    -- loop from above

I wanted to print a more complete final answer for the user, so I built my 'label' before letting my loop change the values of m and n. I could just as easily have made copies in two new variables before entering the loop. Keep in mind: the programmer controls what the program does.

Looking to the Exam

What will be on there?

You have seen many examples of code to trace, debug, and write in class. They are scattered throughout the lecture notes, along with a few "quick exercises" that we didn't do in class.

You don't need to memorize all the esoteric details of Python. In class, we have used a relatively small subset of what the textbook presents. Many of those are nice power tools for us to use once we understand how to write code. On the exam, I will generally ask you to use something specific.

The exam will not last the entire period. We will open with a few minutes of something else first, and then spend 50-60 minutes on the exam.

Do you have any questions? I have office hours and have been known to read a little e-mail...

Wrap Up

Eugene Wallingford ..... ..... October 2, 2014