Problem Set 7

Softcopy Due Friday, March 18, 2016 at 11:59pm

Reading

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.

  1. All code for this assignment is available in the ps07 folder in the cs111/download directory within your cs server account.
  2. In Task 1 (Guided Task), you will define a function named hourglass that prints an hourglass shape of characters to the screen. .
  3. In Task 2 (Guided Task), you will write a recursive function that draws a pattern using Turtle World.
  4. Last semester, CS111 students spent an average of 2.16 hours on Task 1 and 2.25 hours on Task 2.
  5. 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).

  1. indent is the number of spaces printed to the left of each line
  2. width is the maximum number of characters printed per line at the topmost and bottommost lines of the hourglass.
  3. char1 and char2 are the two characters in the hourglass. The hourglass should consist of alternating lines of each character, beginning with char1.

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:

  1. size indicates the length of the vertical edge of the "L"
  2. 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 named drawLs.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() inside drawLs.py which sets up the canvas and puts the turtle in a good starting position for creating Ls).

How to turn in

  1. Soft-copy submission:
    Save your hourglass.py and modified drawLs.py files in your ps07 folder. hourglass.py will be directly in your ps07 folder, and drawLs.py will be inside your DrawLs folder, which is inside the ps07 folder. Submit the entire ps07 folder to your drop folder on the cs server using FTP software (Fetch, WinSCP, CyberDuck, etc). Do NOT change the name of the ps07 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.

  2. 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.