Reading
- Think Python, Chapter 5.8-5.10: Recursion
About this Problem Set
This problem set is intended to give you practice with recursion. You must complete this problem set entirely on your own.
This assignment will be graded for the correctnes and quality of your solution. You are required to submit a honor code form for this assignment. However, there will be no quiz and reflection for this problem set.
- All code for this assignment is available in the ps07 folder in the cs111/download directory within your cs server account.
- In Task 1 (Guided Task), you will define a function named
hourglass
that prints an hourglass shape of characters to the screen. . - In Task 2 (Guided Task), you will write a recursive function that draws a pattern using Turtle World.
- Last semester, CS111 students spent an average of 2.16 hours on Task 1 and 2.25 hours on Task 2.
- Guidelines for checking the correctness and quality of your solution are here.
Task 1: Hourglasses
This is a guided problem which you must complete on your own, but for which you may ask for help from the CS111 staff.
In this task, you will create a new file named hourglass.py
and define in it a recursive function named hourglass
that prints an hourglass shape of characters to the screen. You must use recursion (no loops are allowed).
The function is invoked as hourglass(indent, width, char1, char2)
.
indent
is the number of spaces printed to the left of each linewidth
is the maximum number of characters printed per line at the topmost and bottommost lines of the hourglass.char1
andchar2
are the two characters in the hourglass. The hourglass should consist of alternating lines of each character, beginning withchar1.
Each line in the hourglass shape has either two more or two fewer characters than the line above/below. The following invocations of the hourglass
function produce the output shown below.
print('') hourglass(0, 11, '#', '@') print('') hourglass(0, 12, 'x', 'o') print('') hourglass(5, 7, '*', '-')
########### @@@@@@@@@ ####### @@@@@ ### @ ### @@@@@ ####### @@@@@@@@@ ########### xxxxxxxxxxxx oooooooooo xxxxxxxx oooooo xxxx oo xxxx oooooo xxxxxxxx oooooooooo xxxxxxxxxxxx ******* ----- *** - *** ----- *******
Task 2: Drawing Ls
This is a guided problem which you must complete on your own, but for which you may ask for help from the CS111 staff.
Inside the ps07
folder, look for the DrawLs directory, and open the file called drawLs.py
. Within drawLs.py
, you will define a new function, drawLs
, so that it causes a turtle to draw a pattern of capital L letters. Your function should take two arguments:
- size indicates the length of the vertical edge of the "L"
- levels indicates the number of levels of recursion.
Each successive L is half the size of its predecessor. Below are example invocations of the drawLs() function with different levels.
drawLs(200,0) ![]() |
drawLs(200,1) ![]() |
drawLs(200,2) ![]() |
drawLs(200,3) ![]() |
drawLs(200,4) ![]() |
drawLs(200,5) ![]() |
Here is what a single letter L looks like in detail (all angles in the letter L are right angles):
Notes:
- Write your
drawLs()
function in the provided file nameddrawLs.py
. - Your
drawLs()
function must be recursive (no loops allowed). - You may write a helper function to solve this problem (you do not need more than one).
- The
drawLs()
function must be invariant with respect to the turtle's heading and position. - When testing your function, consider slowing the turtle down with the turtle speed function to get a sense as to precisely what the turtle is doing (see the given
initialize_turtle()
insidedrawLs.py
which sets up the canvas and puts the turtle in a good starting position for creating Ls).
How to turn in
-
Soft-copy submission:
Save yourhourglass.py
and modifieddrawLs.py
files in yourps07
folder.hourglass.py
will be directly in yourps07
folder, anddrawLs.py
will be inside yourDrawLs
folder, which is inside theps07
folder. Submit the entireps07
folder to yourdrop
folder on thecs
server using FTP software (Fetch, WinSCP, CyberDuck, etc). Do NOT change the name of theps07
folder. You should submit this softcopy folder by 11:59pm on Friday, March 18th, 2016. Failure to submit your code before the deadline will result in zero credit for PS7. - Submit Honor Code for PS7 Fill out this form before 11:59pm on Friday, March 18th, 2016. Failure to fill out the Honor Code form will result in zero credit for PS7.