Class 26 — Friday October 23
Tasty problem solving
To bean, not to bean? — That is the question we ask. – The answer, to bean!
Look both ways
Agenda
- Being functional
Buddying
Set up
- Extra credit awarded for buddying with someone today.
- 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 my beanery.py for this effort. It will be available at the start of class.
- Review problem set description.
Module of the day — beanery.py
- Module
beanery.py
defines three functionsvolume()
,count()
, andguess()
related to jelly beans.
- None of your functions should get input or print output.
- Tester program yum.py performs some basic testing of
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, 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 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 activities.
- Try CodingBat – it’s batting practice for writing functions.
- Take another look at function epistle.
- Take another look at today's lesson.
- Preview next class activities activities
- Take another look at the Hands on Python Tutorial.
© 2020 Jim Cohoon | Resources from previous semesters are available. |