Class 11 – Friday, September 20

Looping and nested looping

An optimist thinks — Elevator close button — Really does something


Look both ways


Agenda


Word of the day


Downloads


To do list


Strings

# some strings

s = "immUTAble!"

t = "banana"

u = "AN"

v = "an"

w = "___"

# produce variants of a string

s_c = s.capitalize()

s_l = s.lower()

s_u = s.upper()

r1 = t.replace( u, w )

r2 = t.replace( v, w )

r3 = t.replace( v, "" )

# examine a string for its substrings

c_u = t.count( u )

c_v = t.count( v )

c_v1 = t.count( v, 1 )

c_v2 = t.count( v, 2 )

c_v8 = t.count( v, 8 )

i_u = t.find( u )

i_v = t.find( v )

i_v1 = t.find( v, 1 )

i_v2 = t.find( v, 2 )

i_v8 = t.find( v, 8 )

Program run

s: immUTAble!

t: banana

u: AN

v: an

w: ___

 

s.lower(): immutable! # all lower case version of s

s.upper(): IMMUTABLE! # all upper case version of s

s.capitalize(): Immutable! # version of s initial character capitalized, rest lower case

 

t.replace( u, w ): banana # version of t with its u's replaced with w

t.replace( v, w ): b______a # version of t with its v's replaced with w

t.replace( v, "" ): ba # version of t with its v's replaced with ""

 

t.count( u ): 0 # number of string u in t

t.count( v ): 2 # number of string v in t

 

t.count( v, 1 ): 2 # number of string v in t, starting at index 1

t.count( v, 2 ): 1 # number of string v in t, starting at index 2

t.count( v, 8 ): 0 # number of string v in t, starting at index 8

 

t.find( u ): -1 # first occurrence of string u in t

t.find( v ): 1 # first occurrence of string v in t

 

t.find( v, 1 ): 1 # first occurrence of string v in t, starting at index 1

t.find( v, 2 ): 3 # first occurrence of string v in t, starting at index 2

t.find( v, 8 ): -1 # first occurrence of string v in t, starting at index 8


Chrestomathics

Two possible program runs

Enter a word: subbookkeeper

length: 13

vowels: 6

Enter a word: supercalifragilisticexpialidocious

length: 34

vowels: 16


Two possible program runs with preferred form of output

Enter string: computer

Enter list of indices: 5 1 7

tor

Enter string: gosh look what the cat is up to now

Enter list of indices: 24 5 17 17 27 23 32 0

sleeping

Two possible program runs whose output form is acceptable for today

Enter string: computer

Enter list of indices: 5 1 7

t

o

r

Enter string: gosh look what the cat is up to now

Enter list of indices: 24 5 17 17 27 23 32 0

s

l

e

e

p

i

n

g

A program run

Wa-Hoo-Wa

Rah-Rah-Rah

Wa-Hoo-Wa!!!Rah-Rah-Rah

Wa-Hoo-Wa !!! Rah-Rah-Rah

t-a-t-t-a-r-r-a-t-t-a-t-

saippuakivikauppias



i: 0 1 2 ... i

Two possible program runs

Enter number: 5

0: 0

1: 0 1

2: 0 1 2

3: 0 1 2 3

4: 0 1 2 3 4

Enter number: 12

0: 0

1: 0 1

2: 0 1 2

3: 0 1 2 3

4: 0 1 2 3 4

5: 0 1 2 3 4 5

6: 0 1 2 3 4 5 6

7: 0 1 2 3 4 5 6 7

8: 0 1 2 3 4 5 6 7 8

9: 0 1 2 3 4 5 6 7 8 9

10: 0 1 2 3 4 5 6 7 8 9 10

11: 0 1 2 3 4 5 6 7 8 9 10 11




Tiger and Turtle Magic Mountain



I can smile, see

scary person

 


 

 

 
   
   
   
 
  © 2019 Jim Cohoon   Resources from previous semesters are available.