Grade by Grade
A. Programming Concepts and Skills · Arrays and Subprograms

Arrays and Subprograms: Unit Review

A1.5 A1.6 A2.3 A3.1 A3.2 A4.1 A4.2 A4.3 A4.4 A4.5About 40 minutes
Learning goals
  • I can create, change, traverse, search and sort one-dimensional arrays (lists).
  • I can use existing subprograms and write my own with parameters, return values and local scope.
  • I can design a program as a set of functions and test it with a full range of test cases.
  • I can find and fix syntax, run-time and logic errors in programs that use lists and functions.

Words to know

one-dimensional array
An ordered row of elements under one name, reached by index; in Python, a list.
bounds
The legal indexes, 0 to len − 1.
linear search
Checking elements in order until the target is found, returning its index or −1.
parameter / argument
The name in a function definition / the value passed in a call.
local variable
A variable that exists only inside one call of a function.
test case
Input plus expected output, used to validate a program.

Let's review!

In this unit you learned to store many values under one name with lists (Python's one-dimensional arrays), to traverse them to count, total, average, find the highest and lowest, search and sort, and to work with strings as sequences of characters. You then learned to use existing subprograms and to write your own functions with parameters, return values and proper scope, and to build and test whole programs from them.

This review has a few questions from every lesson. Try each one, then press Show answer to check.

How to use this review

Do one or two sections at a time. For every "predict the output" question, write your prediction first, then check it in Python as well as with the answer. For every question you get wrong, write down why (index from 1? forgot return? wrong loop bound? wrong scope?) and go back to that lesson's worked examples. Finish with the 10-question quiz.

Arrays (lists)• elements, indexes 0 to len − 1, bounds• create, access, change, append, pop• traverse: count, total, average• highest / lowest, linear search• strings and substrings (slices)• sorting: selection, bubble• insert, delete, parallel lists, talliesSubprograms• use built-ins and modules (math, random)• def, parameters, return vs print• arguments: positional, keyword, default• lists are shared, numbers are not• local vs global scope• modular design and main()• testing and debugging (A4)
The two halves of this unit. Every idea on the left is something you do with data; every idea on the right is a way to organize the code that does it.
-140-91-222-63temps (a list)highest(values)loop + if + tracker-6argumentreturnMost programs in this unit look like this: a list goes in, an algorithm runs, a value comes back.
The pattern that joins both halves: a list is passed as an argument, an algorithm inside the function runs, and a value is returned.

Lists as One-Dimensional Arrays: Elements, Indexes and Bounds

1

For a = [12, 7, 30, 4, 19], give len(a), the upper bound, a[3] and a[-2].

2

Which of these raise IndexError for a list of length 5: a[5], a[-5], a[-6], a[4]?

3

In a C array of 2-byte integers starting at address 300, where is element 10?

Declaring, Initializing, Accessing and Modifying Lists

1

Predict: a = [3, 6], a.insert(1, 9), a.append(a[0]), print(a)

2

Predict: x = [1, 2, 3], y = x.copy(), y.pop(), print(x, y)

3

Write code that reads 4 whole numbers typed on one line and stores them as ints in a list.

4

What error does ["a", "b"].remove("c") raise?

Traversing a List: Counting, Totals and Averages

1

Write code that finds the average of rain = [3.5, 0, 12.0, 4.5].

2

Predict: c = 0, then for x in [5, -2, 0, 8, -7]: with body if x <= 0: c += 1, then print(c).

3

Why can't for x in a: x = x * 2 double the elements of a?

Highest, Lowest and Linear Search

1

Write code that prints the index of the lowest value in t = [4, -3, 8, -3]. Which index is printed with <?

2

A linear search for 50 in [10, 20, 50, 40] with break makes how many comparisons?

3

List four test cases for a linear search.

Strings as Sequences: Characters, Substrings and String Methods

1

For s = "Saskatoon", give s[3:6], s[-3:] and s.find("t").

2

Why does s[0] = "Z" fail? Make the change correctly.

3

Predict: print("-".join("a b c".split()))

Using Built-in Functions and Modules

1

What do abs(-4.5), round(6.5) and max([3, 11, 7]) return?

2

Write one line that sets d to a random whole number from 1 to 20.

3

What error comes from math.sqrt(9) without import math?

Writing Your Own Functions

1

Write perimeter(length, width) that returns the perimeter of a rectangle.

2

Predict: def f(n): print(n * 2) then x = f(5) then print(x).

3

What is the difference between a parameter and an argument?

Parameter Passing: Arguments, Defaults and Lists

1

For def fee(age, member=False):, what are the parameter values in fee(30) and in fee(member=True, age=12)?

2

Predict: def add_one(a): a.append(1) then b = [], add_one(b), add_one(b), print(b).

3

Predict: def inc(n): n += 1 then k = 5, inc(k), print(k).

Just for fun

Review riddle: Why did the array go to the unit review?

It wanted to make sure it had covered everything from index 0 to the upper bound.

Variable Scope: Local and Global

1

Predict: y = 1, def f(): y = 2; return y, print(f(), y).

2

Why does count = 0 with def tick(): count += 1 raise UnboundLocalError when tick() is called?

3

How should tick be rewritten without global?

Modular Design: Building Programs from Subprograms

1

Name the input, processing and output functions you would write for: read 7 daily temperatures and print the average and the coldest day.

2

Why should processing functions not print?

3

Write two assert tests for average.

Sorting Algorithms: Selection Sort and Bubble Sort

1

Show [6, 2, 9, 1] after each pass of selection sort.

2

Show [6, 2, 9, 1] after the first pass of bubble sort.

3

How many comparisons does selection sort make for 12 elements?

Inserting, Deleting and Parallel Lists

1

Insert 18 into [5, 12, 20, 31] keeping it sorted. Which elements shift?

2

Delete index 1 from [5, 12, 20, 31] by shifting. Show the list before the final pop.

3

counts = [0] * 4 and the data [2, 2, 3, 0]. What is counts after tallying?

Testing and Debugging Programs with Lists and Functions

1

Classify: (a) a missing colon (b) dividing by len([]) (c) a total that is always 10 too high.

2

Give the boundary test values for a function that accepts temperatures from −40 to 40 inclusive.

3

What is print debugging?

Putting It Together: A Modular List-Processing Program

1

In the ski club tracker, what does main() pass to fastest, and what comes back?

2

Why does the tracker check len(times) == 0 before computing statistics?

3

Name three conventions that make the tracker readable for other programmers.

Check your understanding

Quiz

1.

What is the upper bound of a = [4, 8, 15, 16, 23, 42]?

2.

What does a = [1, 2, 3], a.pop(0), print(a) show?

3.

To find the highest value, a tracker should start at:

4.

What is "Winnipeg"[1:4]?

5.

Which call can return 10?

6.

What does a function with no return statement give back?

7.

def f(a): a[0] = 0 and b = [5, 6], then f(b). What is b?

8.

A variable created inside a function is:

9.

How many comparisons does selection sort make on 10 elements?

10.

A program finishes but prints a wrong average. What kind of error is it?

For parents and teachers

How to use the results: Match each kind of mistake to the lesson to revisit.

  • Counting indexes from 1, or IndexError: Lists as One-Dimensional Arrays.
  • Confusing pop/remove/insert, or aliasing surprises: Declaring, Initializing, Accessing and Modifying Lists.
  • Accumulators in the wrong place, or trouble tracing loops: Traversing a List.
  • Max/min starting at 0, or search printing "not found" too early: Highest, Lowest and Linear Search.
  • Slices off by one, or expecting strings to change: Strings as Sequences.
  • Import errors or randint bounds: Using Built-in Functions and Modules.
  • Printing instead of returning: Writing Your Own Functions.
  • Surprise changes to lists, or argument order mix-ups: Parameter Passing.
  • NameError or UnboundLocalError: Variable Scope.
  • Programs that are one long block, or rely on globals: Modular Design.
  • Trouble tracing passes or swaps: Sorting Algorithms.
  • Shifting in the wrong direction, or parallel lists getting out of step: Inserting, Deleting and Parallel Lists.
  • Testing with only one easy case: Testing and Debugging.
  • Difficulty combining everything: redo the activity in Putting It Together with a smaller set of requirements.

Next: the Software Development unit uses these skills to plan, build and test larger programs in a team-style project.