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


Begin of class downloads (string manipulation)


Later in class downloads (problem solving)


To do list


String manipulation problems

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 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 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


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

  • 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



  • 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.
  • 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.

( 0.05 × | s1 – s2 | ) + ( 0.15 × | a1 – a2 | )

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)

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





street map nyc times square area

 


 

 

 
   
   
   
 
  © 2019 Jim Cohoon   Resources from previous semesters are available.