Class 25 — Friday, October 22
Tasty problem solving
To bean, not to bean? — That is the question we ask. – The answer, to bean!
Look both ways
Agenda
- Being functional
Background
Downloads
- Module beanery.py
- Defines three functions
volume()
,count()
, andguess()
related to jelly beans.
- Available at the start of class
- Program yum.py
- Performs some basic testing of
beanery.py
.
Set up
- The only apps to be running on your laptops are a browser and PyCharm. The browser usage should be limited to CS 1112 materials.
- You must use our
beanery.py
for this effort.
- Review problem set description.
Module beanery.py
Function volume( a, b )
- Returns the decimal 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 of π 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 jar loading capacity isf
and the size of a bean iss
.
- The count formula is ⌊
f
×j
/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. Question: what built-in function does that?
- 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 by throwing away the fractional amount?
- 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?
To do
- Preview next class's activities
- Try CodingBat – it’s batting practice for writing functions.
- Take another look at the function epistle.
- Take another look at the Hands on Python Tutorial.