This lab has four components, “GCD”, “Stairs”, “Sierpinski”, and “Recursive H”, all of which should be implemented in the same file. Get started by downloading the starter file lab9_fractals.py. Replace each pass
statement in the required functions with your implementation.
The purpose of this lab is practice recursion and recursive implementations, so all function must be implemented recursively (the only loops allowed are for drawing triangles).
One of the oldest algorithms, dating back to the 5th century BCE, is Euclid’s algorithm for computing the greatest common divisor (GCD) of two integers. It works as follows:
gcd(a,b)
is b.gcd(a,b)
is the same as gcd(b,rem)
.Write a recursive function gcd(a,b)
that implements Euclid’s algorithm to return the GCD of any two positive integer parameters a
and b
. Your function should return the GCD and not use the input
or print
functions. For example:
>>> gcd(36, 81)
9
>>> gcd(13, 6)
1
Using turtle graphics, write a recursive function stairs(length, levels)
to draw pictures such as those shown above. Assume the turtle starts in the top left corner, facing right (always test using stairs_demo
, which will set the turtle’s starting position for you). The first square should have a side length of length
, and each subsequent square should have 1/2 the size. There should be a total of levels
squares. Do this all in one continuous drawing motion; do not pick up the pen, and do not repeat any lines. Employ the following strategy:
Use the stairs_demo
function in the skeleton file for testing.
Next we’ll draw another interesting fractal shape called the Sierpinski triangle. Here is what it looks like taken out to six generations:
There are many ways to draw this shape, but here is one. Start with the assumption that when you draw the triangle, the turtle will be end up in the same position and orientation it started from. Given that assumption, drawing a generation n triangle just involves drawing three n-1 triangles with sides half the length, like shown below.
Draw a generation n-1 triangle, move to the location of the next n-1 triangle, draw again, move to the last triangle and draw that one, then restore the turtle to the original location and orientation. Generation 1 is just a single equilateral triangle.
More specifically, here are the drawings for generations 0, 1, 2, and 3:
Write a function sierpinski(length, iterations)
. The parameter length
will be the length of the base of the figure and iterations will determine how many generations to draw. Use the sierpinski_demo
function in the starter file for testing.
A recursive H is an H where the end of each vertical line of the H may have another recursive H. The recursive H is defined by a level.
A level 1 recursive H is just an H:
A level 2 recursive H has another smaller H at the end of each line:
A level 3 recursive H has another smaller H at the end of the each of these Hs:
Notice that the smallest level of H always ends in a dot (a “level 0” H).
The size of an H is defined by the parameter length
. The vertical bars are of length length
and the horizontal bar of length 2 * length
. Assume that the turtle starts in the middle of the drawing window, and thus the middle of the horizontal bar, facing to the right. At the end of drawing, the turtle should be at the same position and heading as when it started.
Write a function named recursive_h
that takes two parameters: the length of the vertical bars (the horizontal bar will be twice the length) and number of recursive levels to draw. Level 1 should just draw an H with four dots at the end, level 2 an H with 4 smaller H’s at the end of the vertical bars, and so on. The smallest H’s will all have black dots at the end of their vertical bars. As shown in the images, the length of the vertical bar (length
) should be halved with each level.
Use the recursive_h_demo
function in the starter file for testing.
You may earn up to 2 creativity points on this assignment. Below are some ideas, but you may incorporate your own if you’d like. Make sure to document your extra point additions in comments at the top of the file.
[2 points] Challenge Problem 1: “Sum Nested”
Write a recursive function sum_nested(a_list)
that takes in a nested list of integers as its parameter and returns the sum of all the lists. A nested list of integers contains 0 or more integers and 0 or more nested lists of integers. For example:
>>> sum_nested([])
0
>>> sum_nested([1, 2, 3, 4, 5])
15
>>> sum_nested([1, [2, [3]], 4, 5])
15
>>> sum_nested([1, [2, [3], []], [], 4, 5])
15
>>> sum_nested([[2, 4, 6, 8], [3, 6, 9], [1, [1, 10]]])
50
As a hint, you can use the isinstance
function to check the type of a value, e.g.,
>>> isinstance([1,2], list)
True
>>> isinstance(1, list)
False
speed(0)
to your demo functions to speed up the drawing somewhat. Or, to get the drawing immediately (without watching the turtle draw at all) use tracer(False)
as in the provided demo functions.Make sure that your program is properly documented:
In addition, make sure that you’ve used good code design style (including meaningful variable names, constants where relevant, vertical white space, etc.). For this lab, when run your program should call no functions and produce no output.
When you’re done, call the function drawing_demo()
to run all three drawing functions, and then take a screenshot.
Submit your program via Gradescope. Your program
program file must be named lab9_fractals.py and your screenshot named lab9_screenshot.png (produced from the output of drawing_demo()
). You can submit multiple times,
with only the most recent submission (before the due date) graded. Note that
the tests performed by Gradescope are limited. Passing all of the visible tests
does not guarantee that your submission correctly satisfies all of the
requirements of the assignment.
Gradescope will import your file for testing so that make sure that no code executes on import. That is when imported your program should not try to draw any images.
Features | Points |
---|---|
gcd |
4 |
stairs |
5 |
sierpinski |
5 |
recursive_h |
6 |
Code design and style | 5 |
Creativity points | 2 |
Total | 27 |