Assignment 21 — functional chrestomathics
Wednesday, October 26
Module beanery.py (code will be available at the start of class)
- The module defines three functions
volume()
,count()
, andguess()
.
- A simple tester yum.py for the functions is provided.
- None of your functions should get input or print output.
- Examine and think about algorithms for the problems. However, do not write any code before class.
Function volume( a, b )
- Returns the volume of jelly bean with length
a
and heightb
.
- Based upon hours of research, zillions of jelly beans, and a bunch of mason jars, it has been concluded that the volume of a jelly bean can be roughly modeled as the average of the volume of the exterior bounding cylinder and of the volume of the interior bounding ellipsoid.
- Our formula for the volume
s
of a single jelly bean is:
- To make your calculation as accurate as possible, use Python's
math
module constantmath.pi
to estimate π when needed.
- A run of of the tester should produce output
volume( 1.3 , 0.7 ): 0.41691552507014545
volume( 2.0 , 1.0 ): 1.3089969389957472
Function count( s, j, f )
- Returns the number of beans that can fit in a jar with volume
j
, given the loading capacity isf
and the size of a bean iss
.
- The count formula is: ⌊
j
×f
/s
⌋, where the ⌊ ⌋ operators are the mathematical symbols for rounding down to the nearest integer; that is, for a positive number dropping the fractional amount.
- A run of of the tester should produce output
count( 0.42 , 125 , 0.7 ): 208
count( 1.31 , 500 , 0.6 ): 229
- Questions
- What built-in function converts a decimal to an integer?
- Should function
volume()
be helpful?
Function guess( a, b, j )
- Returns the number of number jelly beans with length
a
and heightb
that can fit in a jar with volumej
. Exhaustive research has determined the jelly bear jar loading factor is typically 69.8% (i.e., .698).
- A run of of the tester should produce output
guess( 1.3 , 0.7 , 125 ): 209
guess( 2.0 , 1.0 , 500 ): 266
guess( 1.52 , 0.9 , 1550 ): 1342
- Questions
- Should function
volume()
be helpful?
- Should function
count()
be helpful?