Trace Scope
Use Tracey to trace the following program:
def func(a): return a + a b = 2 c = func(b) print(c)
Trace Scope Local
Use Tracey to trace the following program:
def func(a): b = a + 1 return b c = 2 d = func(c) print(b)
Trace Scope Shadow
Use Tracey to trace the following program:
def func(a): a = a + 1 return a + a a = 2 b = func(a) print(a)
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
print('Input: 1.0\tActual:', equilateral_area(1.0), '\tExpected: 0.4330127018922193') print('Input: 2.0\tActual:', equilateral_area(2.0), '\tExpected: 1.7320508075688772') print('Input: 3.0\tActual:', equilateral_area(3.0), '\tExpected: 3.8971143170299736') print('Input: 3.5\tActual:', equilateral_area(3.5), '\tExpected: 5.304405598179686')
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
print('Input: 1.0\tActual:', sierpinski_area(1.0), '\tExpected: 0.3247595264191645') print('Input: 2.0\tActual:', sierpinski_area(2.0), '\tExpected: 1.299038105676658') print('Input: 3.0\tActual:', sierpinski_area(3.0), '\tExpected: 2.92283573777248') print('Input: 3.5\tActual:', sierpinski_area(3.5), '\tExpected: 3.9783041986347647')
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 graphics
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.
Note that there is no mechanism for drawing a filled triangle using the graphics module. You can simulate drawing a filled triangle by drawing many lines from the apex of the triangle to the base of the triangle. If you specify the line thickness accordingly, you will end up with a filled triangle.
Example
graphics.window_size(640, 480) draw_tree(320, 240)

Hint
-
Your
draw_tree
function should consist basically of one call todraw_rectangle
from the graphics module, and one call todraw_triangle
(which you are also writing in this file). - An isosceles triangle is a triangle where two of the sides have the same length. For our particular version of an isosceles triangle, we will say that the two vertical sides have the same length. The height of the triangle will be exactly the same as the width of the triangle.
-
To fill in the triangle, you will compute a series of x
coordinates along the base of the triangle. You can use a
for loop to generate these x coordinates. Inside of this
loop, you will call
draw_line
to connect the top of the triangle to the corresponding point at the bottom of the triangle. The following image is what the tree would look like if we skipped drawing some lines:
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
print('Input: 1.0, 10.0\tActual:', compute_age_difference(1.0, 10.0), '\tExpected: 0.10') print('Input: 4.0, 90.0\tActual:', compute_age_difference(4.0, 90.0), '\tExpected: 5.03') print('Input: 10.0, 90.0\tActual:', compute_age_difference(10.0, 90.0), '\tExpected: 12.58') print('Input: 10.0, 99.0\tActual:', compute_age_difference(10.0, 99.0), '\tExpected: 17.41') print('Input: 10.0, 99.9\tActual:', compute_age_difference(10.0, 99.9), '\tExpected: 19.19')
Hint
-
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.