Processing math: 100%

CPSC150A
Scientific Computing

Activity 13

Fruitful Functions

Multiply Integers

Complete the Multiply Integers activity on HumblePython

Average

Write a function called average_three(value1: float, value2: float, value3: float) -> float, which takes 3 floating point values as parameters. Your function should return average of the three values.

Test Cases

Write your own test cases using the test module. Be sure to cover all equivalence classes for all of the inputs. This means you should have a positive, negative, and 0 test case for each of the inputs.

import test
  
def average_three(value1: float, value2: float: value3: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(average_three(?, ?, ?), ?)
    # put more test cases here  
    return None

main()

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: float, percentage_of_light: float) -> float 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 299792458m/s and the number of seconds passed for the twin on earth can be computed from the speed of the rocket in meters per second and the distance the rocket traveled in meters using the equation:

time_on_earth_s=2rocket_distance_mrocket_speed_mps

The fraction of time passed on the rocket as compared to Earth can be computed from the speed of the rocket using the dilation equation:

dilation=1rocket_speed_mps2speed_of_light2

Test Cases

Write your own test cases using the test module. Note that because distance_lightyears can not be a negative number and percentage_of_light is a number between 0.0 and 100.0, there are only 2 equivalence classes for for both of the inputs, positive and 0.

import test

def compute_age_difference(distance_lightyears: float, percentage_of_light: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(compute_age_difference(?, ?), ?)
    # put more test cases here  
    return None

main()
  • Convert the distance in light years to meters: rocket_distance_m=rocket_distance_lightyear×9460730472580800
  • Convert the percentage of light to meters per second: rocket_speed_mps=percent_of_light100×299792458
  • Compute the elapsed seconds for the earth-bound twin: time_on_earth_s=2rocket_distance_mrocket_speed_mps
  • Compute the time dilation: dilation=1rocket_speed_mps22997924582
  • Compute the elapsed seconds for the spacefaring twin: time_on_rocket_s=dilation×time_on_earth_s
  • Compute the difference in age of the two twins: age_difference_s=time_on_earth_stime_on_rocket_s
  • Convert the time in seconds to years: age_difference_y=age_difference_s31557600

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.