CS 110 Lab Project 5
Sequences and Loops
In this lab, you will practice with both Sequences and with Python Repetition statements -- while-loops and for-loops. The work with sequences is intended to give you practice with the thinking required to properly design and to solve problems with loops in a programming language.
Problem 1: Sequences
The first questions (1 through 4) in this section contain the initial entries in a few sequences of numbers. For each of these problems, your answer should specify the following three things:-
What are the next three elements of the sequence?
-
If one element in the sequence has value x, what is the value of the next element in the sequence?
-
What is element i in the sequence (as a function of i)? Note that I'm assuming that the first element in the sequence is element 0.
For example, if the given sequence was 1, 3, 5, 7, 9, 11,... then your answers should be:
-
13, 15, 17 (since these are clearly just listing odd numbers)
-
x + 2 (since each odd number is two larger than the previous odd number)
-
2i + 1 (since odd number i is twice i plus 1)
Write your solutions in a text file called sequences.rtf.
1. 5, 6, 7, 8, 9, 10,...
2. 0, 3, 6, 9, 12, 15,...
3. 0, 1, 4, 9, 16, 25,...
4. 0, 1, 3, 7, 15, 31,...
The Fibonacci numbers are one of the most famous sequences of numbers, and are defined as follows. The first two Fibonacci numbers are defined to be F(0) = 0 and F(1) = 1. Each later Fibonacci number is defined to be the sum of the previous two. More formally, we'd say that F(i) = F(i-1) + F(i-2) for all i > 1. So the first few Fibonacci numbers are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.
5. Specify the next three Fibonacci numbers.
6. Let S(x) be the sum of the first x Fibonacci numbers. So S(0) = 0 (F(0) = 0). And S(1) = 1 (F(0) + F(1) = 1). And S(2) = 2 (F(0) + F(1) + F(2) = 2). Compute the values of S(x) for all values of x from 0 through 8.
7. Conjecture a general formula for the value of S(x) in terms of the Fibonacci numbers (without the summation).
Problem 2: Pattern Recognition
Below are a few patterns. Give the following line in the pattern, and write a description (in English) of what line n of each pattern would look like, for a general n.
For example, if the pattern was
1
12
123
1234
12345
Then a valid answer would be: 123456 is the next line, and in general, line n consists of all integers from 1 to n in increasing order.
8.
111
222
333
444
555
9.
1
22
333
4444
55555
10.
1
2211
333222111
4444333322221111
5555544444333332222211111
Problem 3: Shades of Blue
There are a number of methods (functions) associated with the GraphWin class that are used to interact with individual pixels (rather than drawing larger shapes). In particular, the method
plotPixel(x, y, Color)
sets the color of the pixel at coordinates (x,y) to have the color given by the string Color. Recall that you can create an appropriate color string by invoking the function:
color_rgb(red, green, blue)
where the parameters red, green, and blue are integers with values from 0 to 255 that specify the intensity of each of the color components.
Create a program called shaded.py that creates an image which is 256 x 256 pixels large. Loop over all pixels in the image (nested loops will probably be needed here) and set their color according to the following scheme: set the red value of any given pixel to be the value of its x coordinate; set the blue value to be the value of its y coordinate; and set the green value to be 0. Then display the image. Remember, in an image that is x pixels wide, the index of the last column is x-1 (and similarly for the vertical indexing).
Use good functional design, commenting, and variable names so that your program is exceptionally clear. For extra credit, repeat with a different color pattern, but the new design must exhibit a more complex progression, in which the numbers and step size are not so "nice".
Problem 4: Circles and Grid Redux (Last Time!)
1. (circles.py) Consider your pattern for drawing circles from the last couple of labs. Depending on your design, you should have at least one, and as many as three or even more dimensions in which you exhibit a sequence, much like the practice ones from above. For instance, in class we had our x variable change in a sequence from an initial value of 50 to the values 100, 150, 200, and 250. Our fill color exhibited the sequence 'red', 'blue', 'red', 'blue', etc.
In this problem, I want you to redesign your circles.py program to use just one loop, and I require at least two independently changing sequences. The sequence dimensions could be the x values, the y values, the radius values, the colors, or maybe some other dimension. Using variables and either incremental change in the loop, or using the loop index to compute the ith value in the sequence, use a loop to draw your pattern of circles. Use functions, comments, and variable names to good effect.
Note that, given your original design, some sequences may not be linear in nature. Even in those cases, see if you can 'solve' the sequence problem by writing down the sequence and attempting the kind of relative and/or index based analysis you did in Problem 1 with your own sequence. If you really need to, you can adjust your circles pattern to accommodate the requirements of this problem.
2. (grid.py) I want you to adapt your grid.py program to also use loops. If your grid.py from the previous lab did not solve the problem in a draw_row() kind of solution, you will first need to change to that kind of solution. ... so we need to have a function whose responsibility is to draw an entire row of grid squares, not just a single square.
Then you will introduce loops in two places: first, within the draw_row_red or draw_row_blue functions, you can use a loop whose body draws each of the eight boxes in the row. Also consider unifying, through the use of parameters, what we are doing in draw_row_red() and draw_row_black() so that there is just one draw_row() function.
The other place to introduce loops is at the top level: use a loop that iterates and, in its body, calls the draw_row() function(s).
Good functional design is particularly important in this program, and I expect it, along with good comments and good variable names.