Class 8 – Friday, September 13
I know that or now I know that, that is the question
Problems are at hand – Float thoughts and string them along – To improve yourself
All hoos got your back – A single community – In it together
Look both ways
Agenda
- Gain experience with string manipulation and problem solving
Begin of class downloads (string manipulation)
- Program escaping.py
- Program combine_and_multply.py
- Program lickety_strip_and_split.py
- Program subscripting.py
Later in class downloads (problem solving)
- Program prowess.py
- 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
String manipulation problems
- Program escaping.py
- Explores the use of Python escape character
\
for representing special characters.
Program output
### print strings
a = hatstall
b = a b
c = a b
d = aren't
e = a
b
### string lengths
len( a ) = 8
len( b ) = 4
len( c ) = 3
len( d ) = 6
len( e ) = 3
- Program combine_and_multply.py
- Explores the concatenation operator
+
and the replication operator*
when used with strings.
Program output
### operator + performs concatenation
a = fire
b = fighter
a + b = firefighter
### * operator produces repeated concatenation
m = Wahoo-Wah!
n = 3
m * n = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
n * m = Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!
- Program subscripting.py
- Explores selecting an individual character from a string — subscripting
- Introduces indexes.
Program output
### [] is the string index operator
s = obliviator
0123456789
n = len( s ) = 10
### If the [] operand is an integer value, it is subscripting
s[ 0 ] = o
s[ 4 ] = v
s[ n-1 ] = r
- Program lickety_strip_and_split.py
- Explores the string member functions
strip()
andsplit()
.
q = ' Look there is a Blibbering Humdinger '
### mis-attempted stripping
q = ' Look there is a Blibbering Humdinger '
### stripping
q = ' Look there is a Blibbering Humdinger '
s = 'Look there is a Blibbering Humdinger'
### splitting
f = 'bananas $0.69'
c = 'SEAS CS 1112'
bananas costs $0.69 per pound
School: SEAS subject: CS number: 1112
### list making
s1 = ['bananas', '$0.69']
s2 = ['SEAS', 'CS', '1112']
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 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
© 2019 Jim Cohoon | Resources from previous semesters are available. |