CS 115 Lab 5: Setup and Practice


Setup

  1. If the machine was on when you started using it, reboot it to MacOS.
  2. Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab05). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.
  3. Download the files points.txt and points-test.txt. Put them in the directory Lab05 you just created in step 2 above.

Part 1: Nested loop practice

  1. Enter the following code into the Online Python 3 Tutor:
    for i in range(1, 4):
        for j in range(0, i):
            # end="\t" makes the print statement end with a tab
            # instead of ending the line
            print(i, ',', j, sep="", end="\t")
    
    print()  # Prints a blank line or ends the current line
    
  2. Try to predict what your program will do, and answer Questions 1-3 in Moodle. Then run the code and check your answers.

Part 2: Lab preview

In Lab 5, you will solve a problem involving points in the Cartesian coordinate system. This is the one you're familiar with from math courses, rather than the one used by the graphics library.

Consider the graph below:

Graph with (x, y) coordinates labeled

As you move from left to right along the horizontal (x) axis, the y-values fluctuate. For example, you might see a series of y-values that are increasing and then decreasing, and then increasing again, and then decreasing again, etc.

Your eventual job will be to read a sequence of numbers that represent the y-coordinates of consecutive points. Your program will identify the peaks and valleys in the graph represented by these points. You will see a precise definition of peaks and valleys shortly.

You are allowed to assume that two consecutive points will always have different values.

To gain more insight into peaks and valleys, consider the graph below, which has the peaks colored in black and the valleys in blue:

Graph with (x, y) coordinates labeled

You would want your program to identify 4 peaks and 3 valleys for this dataset.

Definition of peak

A peak is a point in the graph whose y-value is larger than the y-values of both of its immediate neighbors.

For example, (42, 50) is a peak because its y-value (50) is greater than its left neighbor's y-value (46) and its right neighbor's y-value (17).

If a point only has one neighbor, we will consider it a peak as long as its y-value is larger than its neighbor's y-value. In the sample graph, the first point is a peak because its y-value (40) is greater than its only neighbor's y-value (35). The last point is also a peak because its y-value (42) is greater than its only neighbor's y-value (6).

Definition of valley

The definition of valley is similar: a point is a valley point if its y-value is smaller than the y-values of both of its immediate neighbors.

For example, point (20, 25) is a valley because its y-value (25) is smaller than its left neighbor's y-value (30) and its right neighbor's y-value (29).

Similarly, the first point is a valley if its y-value is smaller than that of the second point. The last point is a valley if its y-value is smaller than that of the second-to-last point.

To get some practice with peaks and valleys, consider the following dataset. This dataset represents the y-values of 20 consecutive points:

14
6
9
28
13
22
4
11
42
18
45
10
14
8
49
42
40
6
8
47

Sketch these points on a piece of paper, and answer Question 4 in Moodle.

After you have answered the question, Begin Part A.