CS101 - Homework 3

Due: 2018-03-07 11:59p

Objectives

[20 points] Prelab

Before coming to lab this week, get acquainted with the Turtle library and defining some recursive functions by completing as much as possible of the HW3 Prelab exercises.

[10 points] Snowflake

In the Prelab, you learned about the Koch curve. The Koch Snowflake is another fractal that makes use of the Koch curve. Once your drawKoch() function works correctly, write a new function drawSnowflake(length, generations). The Koch snowflake is really just an equilateral triangle that uses a Koch curve for each of its sides. This should be a relatively simple function to write – there is no recursion. All you need to do is call your drawKoch() function for each of the three sides, turning after each curve is drawn.

Hw03 Snowflake

Write a function called drawSnowflakeScene(). The function should take no parameters and should draw a scene of your own design that includes 3 snowflakes. The snowflakes should all be different sizes and in different locations, with no connecting lines. There should be at least two different colors and at least one filled snowflake. Take a screenshot of the drawing and upload it as part of your HW3 submission using the submit script.

[10 points] Reverse

Write a recursive function reverse(text) that will reverse any string passed in as a parameter. Start by identifying the base case. Think of the simplest string that you could pass to the function – one from which you can immediately return an answer. Then think about the recursive case. Can you decompose the problem into a smaller version of the original problem and a simple operation using the result of solving that problem?

When you are done, you should be able to call reverse() on the command line and get behavior like this:

>>> reverse('csci0101')
'1010icsc'

[10 points] Spiral

For the final part of this assignment, write a recursive function drawSpiral(initialLength, angle, multiplier) that uses the turtle to draw a spiral. This will draw a loop that either collapses in to a fixed point or grows without bound depending on the angle. As such, you should stop drawing the spiral when initialLength is either less than or equal to 1 or greater than 10000. The initialLength variable determines how long a line the turtle should draw, the angle determines how the turtle should turn after drawing the line, and the multiplier determines how the length of the line should change for the next iteration.

Here is an example that was run with the input (300,90,0.9):

Spiral

Write a second function called drawSpiralScene(), that takes no arguments. Use this function to set up the turtle, set the color, and then call your drawSpiral() function. When you start, just draw one spiral and use it to debug your code and experiment with colors, angles and sizes. When you have some variants you are happy with, change the drawSpiralScene() function to draw at least two spirals on the canvas. Take a screenshot of the drawing and upload it as part of your HW3 submission using the submit script.

[2 extra points] Optional Challenge

Can you figure out how to get this one?

Spiral2

Optional extra reading and challenges

A nice write-up about recursion is at Python3 recursive functions. Read the text and try some of the exercises there.


Turning in your work

Put all of your functions into a single Python file called username_hw3.py, where username is your Middlebury username.

Be sure to comment your code. The top of the file should include a multiline comment that lists your name, the name of the assignment, and the date, at a minimum. Each function should also include a multiline comment (enclosed in triple-quotes) at the beginning of the body of the function describing what the function does.

As mentioned above, take screenshots of your output for the snowflake scene and the spiral scene. Save those screenshots and submit them along with your code. (On a Mac use Cmd-Shift-4 to select a region on the screen; on Windows use the Snipping Tool application. The resulting screenshot will be saved to the desktop.)

Before submitting, see the grading rubric and make sure you have followed all instructions correctly. Please submit your file username_hw3.py as well as the two screenshots using the CS 101 submit script. You will need to use the script three times for the three files. Please enter the same amount of time each time you fill out the form, and be sure to spell your name the same each time you submit.