Class 29 — Friday, April 1
Problem solving at its best
The Magnificent – Seven problems for practice – Let's do them today
Look both ways
Agenda
- Being functional
- Mindful collaboration
- Appreciation for buddying
- Better able to convert problem specifications into working functions
Paired Programming
- Paired programming is very specific about who is typing when:
- One person poses as the navigator, who is not typing-- this is the person who pitches ideas about how to solve problems, or what line of code to add next.
- One person is the driver, and they are the only person touching the keyboard. This person is often listening to the ideas that the navigator is laying out, maybe catching a few errors, and translating actions into code.
- Today, we require that you switch roles at least once, halfway through the class. You are welcome to switch more often!
Requirements
- The buddy system is required for this effort.
- None of the functions you develop should get user-typed input or print any output.
- Examine and think about algorithms for the problems. However, do not write any code before class.
- My module file
magnificent.py
must be used.
Module magnificent.py
- Implements seven functions most of which are related to tasks performed this semester.
- Module file magnificent.py
- Tester program seven.py
Function future_me( a, y )
- Returns how a person whose
a
years-old will be in the yeary
. You can assume the current year is 2022.
- Partial output from a sample run of
seven.py
a 20 year-old will be 84 in 2086
a 19 year-old will be 96 in 2099
- Possible algorithm
- Compute difference between 2021 and
y
.
- The future age is total of
a
and the difference.
Function manhattan_distance( a1, s1, a2, s2 )
- Returns the approximate distance in miles between a person at the corner avenue
a1
and streets1
in Manhattan and a person at the corner of avenuea2
and streets2
in Manhattan.
- NYC distance rules:
- Distance of a Manhattan city block running from one street to the next is on average 1/20th of a mile.
- Distance of a Manhattan city block running from one avenue to the next is on average 3/20th of a mile.
- For example, if you were to run the two locations from the map above, you should get the output "Corners ( 1 , 55 ) and ( 3 , 51 ) are 0.5 miles apart." This is because getting from 1st to 3rd avenue contributes 0.3 miles, and the distance from 55th to 51st street adds 0.2 miles. So the function ran at the map's arguments
manhattan_distance( 1, 55, 3, 51 )
shouldreturn
0.5.
- Partial output from a sample run of
seven.py
Corners ( 6 , 59 ) and ( 7 , 34 ) are 1.4 miles apart
Corners ( 2 , 47 ) and ( 6 , 238 ) are 10.15 miles apart
- Possible algoithm
- Compute absolute value of the street differences
- Compute absolute value of the avenue differences
- The distance between the two locations is the street difference multiplied by 1/20 plus the avenue difference multiplied by 3/20.
Function relate( x, y )
- Returns
'<'
,'=='
, or'>'
, depending whether the relationships between stringsx
andy
are respectively:
x
alphabetically comes beforey
,
x
is equal toy
, or
x
alphabetically comes aftery
.
- Note: in comparing
x
andy
letter case does not matter
- Partial output from a sample run of
seven.py
kiwi == Kiwi
apple < banana
orange > melon
- Possible algorithm
- Convert
x
andy
into lower case equivalents.
- Determine which case applies:
x
comes beforey
,x
equalsy
, orx
comes aftery
.
- Return respectively
'<'
,'=='
, or'>'
depending upon the possible relationships
Function youngest( y )
- Returns the youngest acceptable age for a
y
year-old to date according to the dating folk rule that you should only date someone who is at least seven years older than than half your age.
- Partial output from a sample run of
seven.py
a 19 year-old can date a 16 year-old
a 22 year-old can date a 18 year-old
- Possible algorithm
- Compute and return y divided by 2 plus 7 using integer arithmetic
Function is_dateable( y1, y2 )
- Returns
True
orFalse
whether ay2
year-old is an acceptably-aged date for ay1
year-old.
- Your implementation should make use of function
youngest()
.
- Partial output from a sample run of
seven.py
a 15 year-old can date a 22 year-old is True
a 22 year-old can date a 15 year-old is False
a 19 year-old can date a 18 year-old is True
- Possible algorithm
- Determine minimum dateable age for
y1
- Return whether or not
y2
is at at least that minimum dateable age ofy1
Function mutually_dateable( y1, y2 )
- Returns
True
orFalse
whether both ay2
year-old is an acceptably-aged date for ay1
year-old, and ay1
year-old is an acceptably-aged date for ay2
year-old
- Your implementation should make use of function
is_dateable()
.
- Partial output from a sample run of
seven.py
a 25 year-old can date a 65 year-old and vice-versa is False
a 20 year-old can date a 18 year-old and vice-versa is True
- Possible algorithm
- Determine whether
y1
is dateable fory2
.
- Determine whether
y2
is dateable fory1
.
- Return whether both dateable conditions are true.
Function middle( s )
- If the length of
s
is odd, the function returns the middle character ofs
; otherwise, the function returns the two middle characters ofs
.
- Partial output from a sample run of
seven.py
should be.
Middle of abcde is c
Middle of abcdef is cd
Middle of abcd is bc
- Questions
- How does the remainder operator
%
help you check whether a number is odd?
- Suppose
n
is the length ofs
andm
is isn
//
2
.
- If
n
is odd, what is the middle index ofs
?
- If
n
is even, what are the middle indices ofs
?
- Possible algorithm
- Determine the length of
s
- Use your answer to first question above to test whether the length of
s
is odd.
- Use your answer to the second question above to determine the result.
- Return the result.
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |