Class 8 – Friday, January 31
I know that or now I know that, that is the question
We are humankind — What a mantra that could be — I'm human and kind
Look both ways
Agenda
- Gain experience with string manipulation and problem solving
Downloads
-
Expanding your consciousness
- Program temperature.py
- Program whats_the_point.py
-
Do that again
- Program kinda_stringy.py
- Program amore.py
- Program again_and_again.py
-
Maybe quizzical
- Program prowess.py
-
Homework
- Program manhattan_distance.py
To do list
- Review class artifacts
- Check out my documentation on Python
random
capabilities
- Check out my epistle on Python
list
capabilities
Program temperature.py
- Prompts user for a integer Celsius temperature and separately computes its integer and decimal Fahrenheit equivalent using the formula
- Two different possible runs
Enter Celsius temperature: 24
24 C = 75.2 F
24 C = 75 F
Enter Celsius temperature: 8
8 C = 46.4 F
8 C = 46 F
Program whats_the_point.py
- Prompts user for a decimal value and echoes (displays) the value back showing on separate lines 5, 4, 3, 2, 1, and 0 decimal places of accuracy. It finally displays the number rounded to an integer.
- FYI: there is a built-in Python function
round()
that can take one or two arguments. The first argument is the value to be rounded, the second optional value is the wanted number of places after the decimal. When a second value is not supplied, it rounds to an integer.
- Three different possible runs
Enter number: 3.14159265359
3.14159265359
3.14159
3.1416
3.142
3.14
3.1
3.0
3
Enter number: -3.14159265359
-3.14159265359
-3.14159
-3.1416
-3.142
-3.14
-3.1
-3.0
-3
Enter number: 4.998234
4.998234
4.99823
4.9982
4.998
5.0
5.0
5
5.0
Problem solving — chrestomathics
- Program
prowess.py
- Practices some string manipulation. The program
- Prompts the user for a line of text.
- Determines and prints the length of the text.
- Determines and prints the last character of the text.
Two sample runs
Enter a line of text: Hoos got your back
The input has length: 18
The last character ( i.e., at index 17 ) is: k
Enter a line of text: And our winner is
The input has length: 17
The last character ( i.e., at index 16 ) is: s
Program kinda_stringy.py
- Prints out the characters of two user-supplied strings, character by character. The first without a loop, the second with a loop.
Enter a three-letter string: abc
a
b
c
Enter a string: four score and seven
f
o
u
r
s
c
o
r
e
a
n
d
s
e
v
e
n
Enter a three-letter string: def
d
e
f
Enter a string: years ago our
y
e
a
r
s
a
g
o
o
u
r
Program amore.py
- Prints out a user-supplied string list of words, one by one
Some program runs
Enter three words: am or e
am
or
e
Enter several words: amor mapenzi amour liebe rakkaus
amor
mapenzi
amour
liebe
rakkaus
Enter three words: abcd efg hijklm
abcd
efg
hijklm
Enter several words: ljubav aroha ghaoil uthando
ljubav
aroha
ghaoil
uthando
Program again_and_again.py
- Prints a requested phrase a requested number of times
Some program runs
Enter string to be printed: one more time
Enter number of times to be printed: 5
one more time
one more time
one more time
one more time
one more time
Enter string to be printed: I said how are you today
Enter number of times to be printed: 10
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
Homework manhattan_distance.py
- FYI: the built-in Python function abs() computes absolute values. It should prove helpful for this assignment.
- The program takes as its integer input, the street and avenue of one corner in Manhattan and the street and avenue of another corner in Manhattan. The program then computes an estimate of the distance between the two locations using the above formula. The only program output besides the prompts is the estimate.
- Note: the program is to make only two prompts for data, each with two inputs on a single line, as demonstrated in the sample runs below.
- New Yorkers approximate distance between two Manhattan locations using the number of streets and avenues separating the starting and ending locations.
- The approximation uses the following estimates.
- The distance of a Manhattan city block running from one street to the next street is on average 1/20th of a mile.
- The distance of a Manhattan city block running from one avenue to the next avenue is on average 3/20th of mile.
- Based on these block length estimates, the distance from the corner at street s1 and avenue a1 to the corner of street s2 and avenue a2 is
( 0.05 × | s1 – s2 | ) + ( 0.15 × | a1 – a2 | )
- If you do not remember | |’s from high school algebra, | s1 – s2 | is the absolute value of s1 – s2 and | a1 – a2 | is the absolute value of a1 – a2.
- It is important to calculate the absolute values of the street differences and avenue differences separately.
- Because both input prompts should cause the user to supply two values, string function
split()
should prove helpful.
Two different sample runs
Starting corner (street and avenue): 59 6
Ending corner (street and avenue): 34 7
1.4
Starting corner (street and avenue): 47 2
Ending corner (street and avenue): 238 6
10.15
Problem for the interested student (and are you all not interested)
- Program shifty.py
- The program prompts for three strings and prints them in shifted order.
- That is, the third word is printed first, the first word is printed second, and the second word is printed last. This tyype of shifting is called a right circular shift. The printing of the words should be on a single line. Two sample runs could be
- Your solution is not allowed to modify any existing statement.
Two possible program runs
Enter three strings: a b c
Before the shift w1, w2, and w3 are respectively: a b c
After the shift w1, w2, and w3 are respectively: c a b
Enter three strings: kiwi cucumber nectarine
Before the shift w1, w2, and w3 are respectively: kiwi cucumber nectarine
After the shift w1, w2, and w3 are respectively: nectarine kiwi cucumber
© 2020 Jim Cohoon | Resources from previous semesters are available. |