CPSC120
Fundamentals of Computer Science

Activity 4

Types

Range Anxiety

I want to buy an electric car, but I have range anxiety. I forget to plug my phone in at night all the time, but I can charge it in the car on the way to work if I do. If I forget to plug my car in, I may not make it to class! Help me assuage my anxiety by calculating how many times I could forget to plug my car before it becomes a problem.

Details

Create a Python program that calculates how many times I can drive back and forth from home to school. I live 11 miles from work, and the electric car can go 254 miles on a single charge. The program should print the number of times I can successfully drive to and from work on a single charge. The program should also print the number of miles to walk when the battery does run out. This program should use arithmetic operations to perform the calculations necessary and use variables where appropriate.

Example

Number of successful trips:

23

Number of miles short on last trip:

1

Hint

  • Note that all of the inputs to the problem are integers, so declare int variables for the trip distance and the car range.
  • When working with ints, don’t forget that the division operator is // and the remainder operator is %.

Elbow Bumps

One of the significant advantages of a small campus like Roanoke College is that it feels like you know everybody on campus. How long would it take for everybody to meet everybody else? Let's find out.

Details

Create a Python program that prints the number of elbow bumps it would take for every student in a classroom to bump elbows with every other student. Your program should declare a variable for the number of students in the classroom. Changing this variable should change the output of the program. This program should use arithmetic operations to perform the calculations necessary and use variables where appropriate.

Example

For

16

students to meet each other, it would take

120

elbow bumps.

For

20

students to meet each other, it would take

190

elbow bumps.

Hint

  • If there are n students, and each student bumps elbows with every other student, then a student will bump elbows n - 1 times. It’s n - 1 times because a student doesn’t bump their own elbow.
  • So, n students will bump elbows n - 1 times, this is a total of n * (n - 1) elbow bumps.
  • But wait! This counts elbow bumps multiple times. For example, if there are 3 students, Alice, Bob, and Carol. Then the elbow bumps being counted are Alice-Bob, Alice-Carol, Bob-Alice, Bob-Carol, Carol-Alice, and Carol-Bob.
  • We only want each unique pair of students to count as a single elbow bump. Fortunately, this is easy to fix. Notice that the equation n * (n - 1) counts each elbow bump twice, so half of this is the number of unique elbow bumps.

Challenge

The average elbow bump takes about 3 seconds. Add code to your program to estimate how long it would take for this many elbow bumps to occur. Your estimate should be output in days, hours, and minutes.

For

1869

students to meet each other, it would take

1745646

elbow bumps.

This would take

14

hours

42

minutes and

18

seconds.

Voyager 1

Voyager 1 is a space probe that was launched by NASA in 1977. It is currently the farthest man-made object from Earth. It is very likely that Voyager 1 is (or will be) the first man-made object to exit the solar system. Currently, Voyager 1 is approximately 22,458,588,802 km from Earth (And yet, Voyager 1 is still closer to the Sun than any other star in our galaxy!). The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s. At such a great distance, communications take an appreciable amount of time to reach earth.

Details

Create a Python program that prints how long it takes a signal from Voyager 1 to reach the Earth. Since Voyager 1 is continually moving away from Earth, the program should use a variable for Voyager's current distance from Earth. The calculations should be performed using Python's arithmetic operations using variables and printed nicely labeled and formatted.

Example

Voyager is currently

5954572800.0

kilometers from Earth.

It takes

19862.316883235268

seconds for a message from Voyager to reach Earth.

Voyager is currently

22458588802.0

kilometers from Earth.

It takes

74913.7885316648

seconds for a message from Voyager to reach Earth.

Hint

  • Begin by figuring out how many meters Voyager 1 is from Earth. Recall that there are 1000 meters in one kilometer.
  • The speed of light above is given in meters per second. So if you divide the distance in meters by the speed of light you will get how many seconds a signal takes to reach Earth.

Challenge

Proxima Centauri is our solar system's nearest neighbor. Is it close enough that humans could reach it in a lifetime by traveling at the speed of Voyager? Voyager is moving pretty fast, but Proxima Centauri is also pretty far away, 4.2 light-years from Earth. Modify your program to also print how long it would take Voyager 1 to reach Proxima Centauri at its current velocity. The Voyager 1's velocity is 61,400 km/h.

Voyager is currently

22458588802.0

kilometers from Earth.

It takes

74913.7885316648

seconds for a message from Voyager to reach Earth.

It will take

73783.35829177561

years for Voyager to reach Proxima Centauri.