CSci 157 Exam 2 Review



Exam Coverage: Chapters from ThinkCSPY assigned in the readings and homeworks;
Zelle Chapters 8 - 11

This exam will focus on newer material but is comprehensive with regard to fundamentals covered in the first exam:  

The second exam will emphasize these topics

Examples from lecture, homeworks, labs, and the text may figure in the exam. Homework and Activity solutions are posted on BrightSpace.

Sequence operations:

There are several operations we've used that are common to the various sequence types (str, list, tuple) and each has its own specific operations.

str upper, lower, split, join and the operators +, etc.
list append, insert, remove, clear, copy
tuple count,
common operations Operators: len(...), the splice operator :, the indexing operator []
see also https://docs.python.org/3/library/stdtypes.html#common-sequence-operations


Functions

Understand how to define new functions using def, use of parameters, return statements, and local variables. Know how we call functions, passing actual arguments to the call and how those parameters are (implicitly) assigned to the parameters in the parameter list. Variables defined inside a function are local to that function and are not defined outside the function; this is why we use parameters and return values to communicate data between functions.

Iteration

Know for and while iteration statements and when to use them.  Be able to use and explain the Initialize-Test-Update method for designing correct while loops. Be able to say how many times a given loop will iterate, what an infinite loop is, and how to tell if a loop terminates. Be able to explain why one style of loop might be preferred in a given situation. Examples included summations, input validation, and animation. See the relevant homework problems and more examples from the Loop Practice worksheet.

Some of our examples of using for loops used sequences, dictionaries, and range. Be able to use simple methods and operations (such as append, the indexing operator [], slices (:), etc.) on sequence data in a loop.
The split method for strings can also be very useful.

Example Iteration Problems:

For the loops in each function defined below:

Number 1: The hailstone function implements the mathematical process at the heart of the Collatz Conjecture, which is a fairly straightforward yet unsolved problem in mathematics.

    def hailstone():
      "Computes the "hailstone sequence"which we get the Collatz Conjecture, that any sequence starting at any positive number will terminate at 1)" 
      seed = 5          
      print("Hailstone: starting value = ", seed)
      while seed != 1:
          if seed % 2 == 1:
              seed = seed * 3 + 1
          else:
              seed = seed // 2
          print("Next value = ", see
d)

Number 2: Prints the first 18 numbers in the Fibonacci sequence.

     def fibonacci():
"Computes and prints the first n Fibonacci numbers" newfib = 1 oldfib = 1 print(oldfib, ", ", newfib)
for k in range(1, 17):    fib = newfib + oldfib     oldfib = newfib     newfib = fib
      print(fib)

Also, be able to convert loops like the one in fibonacci to a while loop, and be able to explain why while loops like the one in hailstone can't be converted to a for loop.

Nested Lists

Use of structured data such as lists nested inside another list to model data such as: game boards, matrices, calendars (lists of dates), and so on. Access/modification of individual elements using nested loops.
See the Nested Structured activity.

Dictionaries

Storing key/value pairs in both simple and nested dictionary structures (type <class 'dict'>). Primary examples come from the Collections Activity and ThinkCS Section 11.7.
Use of dictionary methods such as keys, values, and items.

Misc

Know the definitions of cohesion and coupling (from Art in Computer Programming) and think about how they apply to program, module, and function design.

The basic ideas and terminology from the Object-Oriented Programming handout may be covered.

Questions?

If at any time you find that you cannot do a problem or understand a concept or how a question on a previous test should be solved, contact me. Bring as many questions as necessary to clear up anything you can't figure out on your own.

There will be coding questions based on reading programs and there will be questions that require you to write short programs. These could feature anything covered.

Come Equipped

You may bring your own notes and handouts (no sharing).  No calculators, laptops, magnifying lenses, or iPhones needed or allowed; caffeinated beverages okay.  I assume you can do high school math computations on paper.