Reading
- Think Python, Chapter 4: Case Study: Interface Design (introduces turtles)
- Think Python, Chapter 5: Recursion (Secs 5.8-5.10)
About this Problem Set
This problem set is intended to give you practice with recursion.
There is no starter code to download for this assignment. You should develop each file from scratch.
This problem set will be graded using this grade sheet.
Working in Pairs
On Tasks 3 (and only this task), you are required to work with a partner as part of a two-person team and you must work with a different partner than you did for PS5. (We are now relaxing our partner policy to allow you to work with partners you've worked with before. You just can't work on two consecutive assignments with the same partner. And you are still encouraged to work with partners that you haven't worked with before! Our CS majors report that while they found it a hassle to find new partners for CS111 when they were taking the course, in retrospect, working with as many different partners as possible was incredbibly valuable.) Both team members will submit softcopies of your team-solution to Task 3 and only ONE team member will submit hardcopy of your team-solution for Tasks 3. Both team members will receive the same grade for Task 3.
All work by a team must be a true collaboration in which members actively work together on all parts of the Task. It is not acceptable for team members to split up the Task and work on parts independently. All programming should be done with both team members working at the same computer console. It is strongly recommended that both team members share the responsibility of "driving" (typing at the keyboard), swapping every so often. The only work that you may do alone is debugging code that you have written together and talking with the instructors and drop-in tutors. Use this shared Google Doc to find a pair programming partner and record who your pair partner is.
How to turn in this Problem Set
You must submit both soft-copy (electronic) and hard-copy (printed) versions of your problem set.
Soft-copy submission
Save your rectangle.py and steps.py
files in a ps06_programs folder that you create. BOTH
team members should save the sierpinski.py file in their
ps06_programs folder.
This pair programming file should have the names of both partners at the top.
Submit the entire
ps06_programs folder (renamed to yourname_ps06_programs)
to your drop folder on the
cs server using Fetch (for Macs) or WinSCP (for PCs).
Hard-copy submission
Print out your rectangle.py and steps.py
files.
One and only one member of your
team should
additionally print out your
sierpinski.py file
with the names of
both partners at the top.
Staple these pages together with
a cover page,
and submit this hardcopy package in lab on Wed. Mar. 11.
The hardcopy files must exactly match the sofcopy files submitted the
previous night.
Task 1: Rectangle
In this task, you will define a function named rectangle that prints a rectangle of characters to the screen. The function should have width and height parameters that denote the number of characters printed on each line and the number of lines printed, respectively. Additionally, the function should have two parameters, char1 and char2, corresponding to characters. The rectangle should consist of alternating lines of a character, beginning with char1. Two example invocations of the rectangle function are shown below.
>>> rectangle(5, 8, 'X', 'O')XXXXXOOOOOXXXXXOOOOOXXXXXOOOOOXXXXXOOOOO
>>> rectangle(7, 5, '~', '+')~~~~~~~+++++++~~~~~~~+++++++~~~~~~~
You should define your rectangle function in a file named rectangle.py that you develop from scratch. You must use recursion to solve this problem (no loops).
Task 2: Turtle Steps
If there is one thing that turtles love, it is climbing stairs (obviously). Here, you must a write a function named steps that causes a turtle to draw spaced squares in a pattern of steps, i.e., a flight of stairs. The steps function should have a parameter number indicating the number of stairs, a parameter size indicating the side length of each square in the pattern, and two parameters clr1 and clr2 corresponding to the alternating colors of the squares, where the bottom-leftmost square is drawn with clr1.
Requirements:
- All your code must be in a file named
steps.py. - No square should be adjacent to another square of the same color.
- The horizontal and vertical distance between each square should be one quarter of the side length of the squares.
- You may define and use any helper functions that you deem useful.
- You should not use loops anywhere in your solution.
- Your
stepsfunction, as well as every helpful function that you design, must be invariant with respect to the turtle's heading and position.
As an example, invocation of steps(8, 30, 'red', 'blue')
should cause the turtle to draw a pattern such as that shown below.
Task 3: Sierpinski's Gasket
For Tasks 3, you are required to work with a partner as part of a two-person team and you must work with a partner that you did not work with on PS5. Your team will submit a softcopy and hardcopy of your team-solution to Tasks 3 and the same grade will be given to both team members for the task. Please see the top of this web page for details about pair programming. (Note: In extenuating circumstances, you may ask an instructor for an exception to the pair programming requirement.)
Sierpinski's gasket is a self-similar triangular entity discovered in 1916 by Polish mathematician Waclaw Sierpinski (1882-1969). In this problem, we will investigate an approach for approximating Sierpinski's gasket.
You will define a function, sierpinski, that causes a turtle to draw a pattern similar to Sierpinski's gasket. Your function should have two parameters: levels indicates the number of levels of recursion and side indicates the side length of the largest triangle. Below are examples invocations of the sierpinski function with different levels.
sierpinski(0, 400)
|
sierpinski(1, 400)
|
sierpinski(2, 400)
|
sierpinski(3, 400)
|
sierpinski(4, 400)
|
sierpinski(5, 400)
|
Notes:
- Your
sierpinskifunction should be in a file namedsierpinski.py. - Your
sierpinskifunction must be recursive (no loops allowed). - You do not need any helper functions to solve this problem.
- The
sierpinskifunction must be invariant with respect to the turtle's heading and position. - Think very carefully about how to decompose this problem into subproblems. There are different ways that an
n-level sierpinski gasket can be created out of (n-1)-level sierpinski gaskets. Some decompositions will lend themselves to easier solutions than others. - When testing your function, consider slowing the turtle down with the turtle
speedfunction to get a sense as to precisely what the turtle is doing.
