Equilateral Area
Create a Python program that defines the function
equilateral_area(edge_length)
that computes the
area of an equilateral triangle. The
parameter edge_length is a float that specifies the
length of the triangle's edges. The function should return the area
of the triangle as a float. The area of an equilateral triangle
and be computed with the equation:
a=√34×l2
Where a is the area of the triangle and l is the length of an edge of the triangle.
Test Cases
Create your own! Use this template:
print('Input:', ?, '\tActual:', equilateral_area(?), '\tExpected:', ?)
Sierpinski
Create a Python program that defines the
function sierpinski_area(edge_length)
that computes
the area of a Sierpinski triangle of order one. A Sierpinski
Triangle of order one is an equilateral triangle with a
triangular hole. The vertices of the hole triangle are the mid
points of the Sierpinski triangle's edges. For example:
Copy and paste your solution to the previous exercise into this
one
and use it to write
the sierpinski_area
function.
Test Cases
Create your own! Use this template:
print('Input:', ?, '\tActual:', sierpinski_area(?), '\tExpected:', ?)
Trees
When working with functions, abstraction is key. You will soon find yourself in a position where you start thinking about problems in functions. Being able to break a complex problem, like an advanced drawing, into smaller and simpler problems which can be solved with a function can make your code a lot easier to write, and a lot easier to read.
Details
Create a Python program that defines the
function draw_tree(x, y)
that uses the turtle
module to draw a tree. The x and y location specified is the
center of the base of the triangle making up the tree. The tree
should look like the tree in the example below.
Notice that a tree is defined by a brown square, and a filled
green triangle. To simplify the draw_tree
function, also define the function
draw_triangle(x, y)
, which draws a filled, green
isosceles triangle. The triangle can be any height, but the
length of the base should be the same as the height.
Example

- Drawing the trunk of the tree is simple, just draw a filled square.
-
Drawing the tree's canopy is easier if you use
the
turtle.goto
function. To help figure out the coordinates of the triangle corners, make a drawing of the triangle inside of square. The triangle fits inside of a square since its width is the same as its height.
Challenge
Modify your program so that you can specify the size of the tree. We will define the size of the tree to be the witdh of the tree (and the height of the triangle). Use this new function to draw yourself a forest of trees.

Twin Paradox
In the year 2047, a set of twins were born. One of these twins was placed in a rocket and launched towards a distant star at a significant percentage of the speed of light. Unlike Superman, however, the rocket was programmed to return home after reaching its destination. Because of the laws of relativity, the twin who was launched into space at close to the speed of light will have aged less than their sibling. The question is, how much younger is the spacefaring twin?
Details
Create a Python program that defines the
function compute_age_difference(distance_lightyears,
percentage_of_light)
that uses the laws of relativity to
compute how much younger the space traveling twin is. The
parameter distance_lightyears is a float representing a
distance traveled, in light-years. The
parameter percentage_of_light is a float in the range
(0,100) that represents the percentage of the speed of
light that the twin traveled. The function should return how
many years younger the twin is.
The function should assume that all acceleration is instant, which simplifies the calculations. The speed of light is 2.99×108m/s2 and the number of seconds passed on earth can be computed using equation:
time_on_earth=2distance_metersspeed_traveled
The fraction of time passed on the rocket as compared to Earth can be computed using the dilation equation:
dilation=√1−speed_traveled2speed_of_light2
Test Cases
Create your own! Use this template:
print('Input:', ?, ',', ?, '\tActual:', compute_age_difference(?, ?), '\tExpected: ?')
-
Write a function called
lightyears_to_meters(lightyears)
, which converts a distance in light years to meters. One light year is 9.56×1015 meters. -
The time equation listed above returns the time that has
passed on Earth in seconds. Write a
function called
seconds_to_years(seconds)
, which returns a floating point value representing how many years that number of seconds represents. - Using the above equations, you simply need to subtract the time that passed on the ship from the time passed on earth.
Challenge
What if, instead of just launching one of the children into
space, these crazy scientists launched both? Likely, they
would have tried to send two of the siblings off into space,
but at different speeds. Write a new function
called compute_relativistic_age_difference
, which
computes the difference between two siblings who were launched
into space at differing speeds.