Lab 6, CSC 101

This lab provides additional exercises on iteration over lists and introduces clock events in pygame.

Download lab6.zip, place it in your cpe101 directory, and unzip the file.

Fold Pattern

Develop these functions in the fold directory in files fold.py and fold_tests.py.

Each of the functions for this part of the lab is to be implemented using the fold pattern. In this pattern, the values in the input list are combined in some manner (e.g., an arithmetic or relational operation) to compute a single result value. Each of these functions will use a local variable (or local variables) to hold the computed value as the list is traversed. This variable is often updated at each step of the iteration.

sum

The sum function returns the sum of all values in the input list.

index_of_smallest

The index_of_smallest function returns the index of the smallest value in the input list. If the list is empty (i.e., the length is ≤ 0), then the function returns -1.

nearest_origin

The nearest_origin function returns the index of the point nearest the origin. If the list is empty, then the function returns -1.

Clock Events

In the clock directory you will find a couple of source files. The file ordered_list.py provides a wrapper around a basic list that keeps elements in order as determined by a value named ord. You should read through the file to see which operations are provided on this ordered list. Note that when an element is retrieved from this list (using either head or pop), a ListItem value is returned giving access to the original item and the order value.

The second file, clock_example.py, provides some skeleton code with which to work to experiment with clock events. This file sets a timer to "tick" every 100 milliseconds. Your goal is to set up actions to occur at given times or frequencies.

First Action

In actions.py, define a new class (e.g., PrintAction) to represent an action that will result in a simple message printing to the screen. This means that an object of this class will need to store the message that will later be printed.

In initialize_actions in the clock_example file, create an instance of your action class and "schedule" it by inserting the action into the action_list with a time of a few seconds from the current ticks value.

In handle_ticks, process your action by checking for the appropriate kind using isinstance as provided by Python (e.g., isinstance(action.item, actions.PrintAction), though the names may differ for your solution). If the action is of the appropriate kind, then print the message and the current ticks (e.g., Hello (3518)).

Repeating Action

Create a new kind of action that will repeatedly print a message to the screen. You will want to store not only the message, but also the amount of time to wait until the next print. Repeating can be accomplished by rescheduling the action each time it is processed. As above, print the message and the current ticks.

Counted Repeating Action

Create a new kind of action similar to the repeating action, but that will only repeat a specified number of times. Print the message, the number of ticks, and the number of remaining times to repeat.

Demonstration

Demonstrate each part of the lab to your instructor to have this lab recorded as completed. Be prepared to show the source code to your instructor.