CPSC120A
Fundamentals of Computer Science I

Lab 3

Naming Values

Use the command line to create a new directory called lab3 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

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

In Emacs, create a Python program in a file called origami.py. The program should print 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

$ python3 origami.py
A piece of paper folded in half 10 times would be 4.096 inches thick.

Hint

  • 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.

World's Largest Ice Cream Cone

The world's largest ice cream cone was 9 feet tall, according to the Guinness Book of World Records. While impressive based on the height alone, one has to wonder how much ice cream actually went into making such a monstrosity. Math is of course our friend, but writing a program could make it easier to figure out how big of an ice cream cone you and your friends would have to make in order to beat the current record.

Details

In Emacs, create a Python program in a file called ice_cream.py. The program should print the volume in, in cubic feet, of the worlds largest ice cream cone. The cone is 9 feet tall with a radius of 2 feet. Assume that the entire cone is filled with ice cream and that there is a perfect half-sphere of ice cream on top of the cone, like this:

The volume of a cone can be computed using the equation:

$$v = \pi \cdot r^{2} \cdot \frac{h}{3}$$

Where \(v\) is the volume of a cone, \(r\) is the radius of the cone, and \(h\) is the height of the cone. The volume of a sphere can be computed using the equation:

$$v = \frac{4}{3} \cdot \pi \cdot r^{3}$$

Where \(v\) is the volume of a sphere and \(r\) is the radius of the sphere. 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

$ python3 ice_cream.py
The volume of the cone is 37.69911184307752 cubic feet.
The volume of the sphere is 33.510321638291124 cubic feet.
The volume of ice cream is 54.454272662223076 cubic feet.

Hint

  1. First, create variables for the input values to the program. In this case cone_height and cone_radius are the inputs to the program. Set the height variable to 9 and the radius variable to 2.
  2. Next, create variables for the computations that will be performed. In this case cone_volume, sphere_volume, and ice_cream_volume. Set each of these variables equal to arithmetic expressions that compute the correct value. Note that the ice_cream_volume variable depends on the other two variables, so it must be defined after them.
  3. Finally, create print statements that print the calculated values nicely formatted.

Challenge

So how many children can the world's largest ice cream cone feed? Modify your program to calculate and print the result. Assume that a single serving of ice cream is 4 oz and note that there are 957.506 oz in a cubic foot.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.