# CS101 - Homework 3

Due: 2018-03-07 11:59p

#### Objectives

• Get some practice with the turtle
• Learn to break problems down into recursive solutions

### [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.

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):

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?

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