CS Principles
This activity focuses on algorithms and abstraction and addresses
the following learning objectives:
- 15: The student can develop an algorithm.
- 16: The student can express an algorithm in a language.
- 20: The student can use abstraction to manage complexity in a program.
Introduction
This less focuses on an abstract loop pattern -- the running total pattern --
which can be used to solve a range of related problems.
You should first complete the
following homework, where we learned:
- The while loop structure is made up initialization, loop entry condition, and update step
elements and how they work together.
- The for each loop is an abstraction of the concept of list traversal.
- Computing the sum of a list of numbers is one example of the running total pattern.
Running Total Pattern
Recall this version of the running total pattern from the
homework:
# Function to calculate the sum of a list of numbers
# Argument: list contains a list of integers
To SumAList(list) :
Set global Sum to 0
Set global Index to 1
While Index <=length(list) do:
Add to the Sum the selected item in list at index Index
Add 1 to the Index
Return Sum
|
|
This version uses a while loop to traverse through a list.
If we focus just on the problem of computing the sum
(the green parts) and ignore
the problem of traversing the list, then the key elements of
the running total pattern are these:
- Initialize a variable to store the running total (Set global Sum to 0).
- For each item that should make up the running total, add it to the running total.
Because it makes it easier for us to ignore the problem of
traversing a list, the for each loop version of this algorithm
makes it easier to see the running total pattern:
# Function using a for-each loop to compute the sum of a list of numbers.
# The list is named aList
# The variable num is used to represent each number in the list
To computeListSum(aList) :
Set global Sum to 0
For each num in aList do:
Set Sum to Sum + num
Return Sum
|
|
Running Total Variations
- Summing only Some of the Numbers in a List:
What about the problem of summing only the odd numbers in a list? Is this
a variation of the running total pattern? Sure. In this case we just need to
add an if statement to filter out (ignore) the even numbers. So our algorithm
would be:
# Function using a for-each loop to sum the odd numbers in a list
# The list is named aList
# The variable num is used to represent each number in the list
To computeListSumOdd(aList) :
Set global Sum to 0
For each num in aList do:
If the num is odd then do:
Set Sum to Sum + num
Return Sum
|
|
- Summing a Range of Numbers:
What about the problem of summing between 1 and N or between
100 and 200? Is this a variation of the running total pattern?
Sure. In this case we need to traverse throught the range of numbers
starting at the low number and proceeding through the high
number, adding each number to the Sum. So our algorithm would be:
# Function using a for-range loop to sum the numbers in a range
# The argument low represents the lowest value in the range
# The argument high represents the lowest value in the range
# The variable n represents each number in the range
To sumARange(low, high) :
Set global Sum to 0
For each n in the range low to high do:
Set Sum to Sum + n
Return Sum
|
|
- Concatenating a List of Words into a String
What about the problem of concatenating a list of words into a
single string? Is this a variation of the running total pattern?
Sort of, because concatenation is like addition for test.
# Function to concatenate the words in a list into a string
# The list is named listOfWords
# The variable word is used to represent each word in the list
# The + operation for text is called join in App Inventor
To concatenateWords(listOfWords) :
Set global String to the empty string
For each word in listOfWords do:
Set String to String + word
Return String
|
|
Key Points
- The for-each loop is an abstraction of the concept of
list traversal.
- The for-range loop is an abstraction of the concept
traversing a range of numbers.
- The running total pattern is an abstraction of the
concept of an algorithm in which a total starts out at 0 and
then accumulates values as the algorithm traverses a range or list.
- Concatenation, where you append a item to a string is analagous
to addition on numbers.
- The running total can be generalized to include related
constructs such as a string formed through repeated concatenation.
Working in pairs, perform each of the following exercises. If you
do not finish in class, do the exercises for homework. HINT:
Some of the operations required for these exercises use built-in
functions that can be found in the App Inventor Lists
and Text menus.
- Write and test a function named computeSumListFives that
returns sum of the numbers that are multiples of 5 in a given list.
- Write and test a function named countZeroes that
will count the number of zeroes in a list. For example, if the list
is [0 1 0 0 2 0 2], the function should return 4.
- The factorial of N is defined as N × N-1 × N-2
... 2 × 1. Write a function to compute the factorial of
N, where N is the argument. HINT: Try to see this
as a running total problem.