CPSC120A
Fundamentals of Computer Science

Activity 1

Variables and Expressions

Age in Seconds

Now that you know expressions, you can use them to compute values that are very difficult to compute in your head. For example, what if an alien species shows up on Earth, and demands to know how old you are? Being aliens they have no concept of years, only seconds. While computing that by hand might be a chore, Python can rescue us from certain doom.

Details

Create a Python program that prints your age in seconds. Assume there are 365 days in a year. You don't need to compute your exact age, just convert your age in years to seconds. The program should use Python's arithmetic operations to perform the calculations and should print the number of seconds nicely formatted an labeled.

Example

  Durell's age in seconds is 1198368000

You first need to compute how many seconds there are per year. Recall that there are 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute. If you multiply these values together, you get the number of seconds in a year.

Challenge

Computing your age in seconds is pretty easy if you assume that every year has 365 days. But this ignores leap years, how many days it has been since your last birthday, and what time it is now. Modify the program so that it prints out your current age in seconds more precisely.


Boring Origami

A single sheet of paper is 0.004 inches thick. If you fold the paper in half, the two halves of the sheet of paper are together 0.008 inches thick. If you fold the paper in half again the four quarters are together 0.016 inches thick. How thick would the paper be if you folded it in half 10 times?

Details

Create a Python program that prints how thick a piece of paper would be if you folded it in half 10 times. The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

  A piece of paper folded in half 10 times would be 4.096 inches thick.
  • If a single sheet of paper is 0.004 inches thick then when it is folded in half once, it will be twice as thick, 0.004 * 2 = 0.008.
  • If a single sheet of paper is is folded in half twice, it will be twice as thick as when it is folded once, (0.004 * 2) * 2 = 0.016.
  • If a single sheet of paper is folded in half three times, it will be twice as thick as if it were folded twice, ((0.004 * 2) * 2) * 2 = 0.032
  • You can probably see where this is going. Just repeat this until you have found the thickness of folding the paper 10 times.

Challenge

How many times do you have to fold a piece of paper in half for its height to reach the moon? Use your program to figure it out. The moon is about 238857 miles from the Earth. The actual distance varies because the moon's orbit is not circular. Change the variable for the number of folds until you find the smallest number of foldes that is greater than the distance to the Moon. Note, you will need to convert the thickness to miles or the distance to the moon to inches to make the comparison correctly.


Finding Your Pace

You might think that running has very little to do with Computer Science. However, there are a lot of useful computations we can do to help a runner figure out how fast they ran. Race distances are typically given in kilometers: 5k, 10k, etc. A fast runner might run a 5K in 20 minutes, but the winning time is usually closer to 15 minutes. In the US, runners typically measure their pace in minutes per mile. We will write a program to perform these computations.

Details

Create a Python program that print a runner's pace in minutes per mile. Your program should have variables that store:

  • the distance of the race in kilometers
  • the minutes part of their finish time
  • the seconds part of their finish time

The program should use meaningful names for values and simple statements to make the program more readable. The output should be nicely formatted and labeled.

Example

  If you ran a 5 kilometer race in 27 minutes and 15 seconds,
  your pace will be 8.790322580645162 minutes per mile.
  • Begin by defining the variables and constants. You will need variables for the distance in kilometers, distance in miles, minutes part of the time, seconds part of the time, and minutes per mile.
  • Then, compute the distance in miles and the time in minutes. Use the approximation that 5 kilometers is about 3.1 miles.
  • The pace of the runner is just their time divided by the distance of the race in miles.

Challenge

Many runners set goals to improve their pace. Add to your program a variable for the user to store their desired minutes per mile into. Compute how quickly they would have to run the race, and print this value as well.