Strings IV WARM UP: Write a func1on called count_dups - - PowerPoint PPT Presentation

strings iv warm up write a func1on called count dups that
SMART_READER_LITE
LIVE PREVIEW

Strings IV WARM UP: Write a func1on called count_dups - - PowerPoint PPT Presentation

Strings IV WARM UP: Write a func1on called count_dups that counts the number of back-to-back duplicated characters in a string. Example:


slide-1
SLIDE 1

Strings ¡IV ¡

slide-2
SLIDE 2

WARM ¡UP: ¡

  • Write ¡a ¡func1on ¡called ¡count_dups ¡that ¡counts ¡

the ¡number ¡of ¡back-­‑to-­‑back ¡duplicated ¡characters ¡ in ¡a ¡string. ¡

Example: ¡count_dups("balloon") returns ¡2. ¡

  • (Harder) ¡Write ¡a ¡func1on ¡called ¡strange ¡that ¡

keeps ¡all ¡the ¡digits ¡in ¡a ¡string, ¡but ¡only ¡digits ¡that ¡ are ¡immediately ¡preceded ¡by ¡a ¡le3er. ¡ ¡The ¡first ¡ character ¡in ¡the ¡string ¡is ¡guaranteed ¡to ¡be ¡a ¡leFer. ¡

Example: ¡strange("a16.3j4LM19") returns ¡ "141"

slide-3
SLIDE 3
  • Count ¡duplicates: ¡

¡ def ¡count_dups(s): ¡ ¡ ¡total ¡= ¡0 ¡ ¡ ¡for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡ ¡ ¡if ¡<test ¡s[pos] ¡for ¡something>: ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡total ¡+ ¡1 ¡ ¡ ¡return ¡total ¡ ¡

Is ¡s[pos] ¡the ¡same ¡ character ¡as ¡the ¡ character ¡immediately ¡ to ¡the ¡right? ¡

slide-4
SLIDE 4
  • Count ¡duplicates: ¡

¡ def ¡count_dups(s): ¡ ¡ ¡total ¡= ¡0 ¡ ¡ ¡for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡ ¡ ¡if ¡s[pos] ¡== ¡s[pos+1]: ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡total ¡+ ¡1 ¡ ¡ ¡return ¡total ¡ ¡

Is ¡s[pos] ¡the ¡same ¡ character ¡as ¡the ¡ character ¡immediately ¡ to ¡the ¡right? ¡

slide-5
SLIDE 5
  • Count ¡duplicates: ¡

¡ def ¡count_dups(s): ¡ ¡ ¡total ¡= ¡0 ¡ ¡ ¡for ¡pos ¡in ¡range(0, ¡len(s)-­‑1, ¡1): ¡ ¡ ¡ ¡ ¡if ¡s[pos] ¡== ¡s[pos+1]: ¡ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡total ¡+ ¡1 ¡ ¡ ¡return ¡total ¡ ¡

Is ¡s[pos] ¡the ¡same ¡ character ¡as ¡the ¡ character ¡immediately ¡ to ¡the ¡right? ¡

slide-6
SLIDE 6

s.startswith(t) ¡ True ¡if ¡the ¡string ¡s ¡begins ¡with ¡ the ¡string ¡t. ¡ s.endswith(t) ¡ True ¡if ¡the ¡string ¡s ¡ends ¡with ¡the ¡ string ¡t. ¡

slide-7
SLIDE 7

s.find(t) ¡ Returns ¡the ¡lowest ¡index ¡at ¡ which ¡substring ¡t ¡is ¡found ¡ inside ¡s. ¡ s.find(t, ¡p) ¡ Same ¡as ¡above, ¡but ¡starts ¡ searching ¡at ¡posi1on ¡p. ¡ s.replace(t, ¡t2) ¡ Returns ¡a ¡copy ¡of ¡s ¡with ¡all ¡

  • ccurrences ¡of ¡t ¡replaced ¡by ¡t2. ¡
slide-8
SLIDE 8

s.upper() ¡ Returns ¡a ¡copy ¡of ¡s ¡with ¡all ¡ leFers ¡converted ¡to ¡uppercase. ¡ s.lower() ¡ Returns ¡a ¡copy ¡of ¡s ¡with ¡all ¡ leFers ¡converted ¡to ¡lowercase. ¡

slide-9
SLIDE 9

Three ¡common ¡string ¡computa1ons ¡

  • Count ¡the ¡number ¡of ¡1mes ¡something ¡

happens ¡in ¡a ¡string. ¡

  • Filter ¡a ¡string ¡to ¡keep ¡only ¡the ¡characters ¡that ¡

sa1sfy ¡some ¡condi1on. ¡

  • Transform ¡a ¡string ¡into ¡a ¡new ¡string ¡by ¡

changing ¡each ¡character ¡in ¡some ¡fashion. ¡

slide-10
SLIDE 10

Coun1ng ¡

look ¡at ¡each ¡character: ¡ ¡ ¡does ¡this ¡character ¡match ¡a ¡paFern? ¡ ¡ ¡ ¡ ¡if ¡yes, ¡increment ¡total ¡ ¡ total ¡= ¡0 ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡if ¡<test ¡s[pos] ¡for ¡something>: ¡ ¡ ¡ ¡ ¡total ¡= ¡total ¡+ ¡1 ¡ ¡

slide-11
SLIDE 11

Filtering ¡

look ¡at ¡each ¡character: ¡ ¡ ¡does ¡this ¡character ¡match ¡a ¡paFern? ¡ ¡ ¡ ¡ ¡if ¡yes, ¡aFach ¡the ¡character ¡to ¡the ¡answer ¡ ¡ answer ¡= ¡"" ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡if ¡<test ¡s[pos] ¡for ¡something> ¡ ¡ ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡s[pos] ¡ ¡

slide-12
SLIDE 12

Transforming ¡

look ¡at ¡each ¡character: ¡ ¡ ¡turn ¡this ¡character ¡into ¡a ¡new ¡character ¡and ¡ ¡ ¡aFach ¡it ¡to ¡the ¡answer ¡ ¡ ¡

slide-13
SLIDE 13

Transforming ¡

look ¡at ¡each ¡character: ¡ ¡ ¡turn ¡this ¡character ¡into ¡a ¡new ¡character ¡and ¡ ¡ ¡aFach ¡it ¡to ¡the ¡answer ¡ answer ¡= ¡"" ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡newchar ¡= ¡<do ¡something ¡to ¡s[pos]> ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡newchar ¡ ¡

slide-14
SLIDE 14

Transforming ¡

Turn ¡every ¡character ¡into ¡a ¡hyphen: ¡ ¡ answer ¡= ¡"" ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡newchar ¡= ¡"-­‑" ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡newchar ¡ ¡

slide-15
SLIDE 15

Transforming ¡

Common ¡to ¡use ¡an ¡if ¡statement ¡inside ¡a ¡transform: ¡ answer ¡= ¡"" ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡if ¡<something>: ¡ ¡ ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡<something> ¡ ¡ ¡else: ¡ ¡ ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡<something ¡else> ¡ ¡

slide-16
SLIDE 16

Transforming ¡

Switch ¡the ¡case ¡of ¡all ¡leFers ¡(lower ¡<-­‑-­‑-­‑> ¡upper) ¡ answer ¡= ¡"" ¡ for ¡pos ¡in ¡range(0, ¡len(s), ¡1): ¡ ¡ ¡if ¡<something>: ¡ ¡ ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡<something> ¡ ¡ ¡else: ¡ ¡ ¡ ¡ ¡answer ¡= ¡answer ¡+ ¡<something ¡else> ¡ ¡

slide-17
SLIDE 17

Transforming ¡

Transforming ¡is ¡oRen ¡combined ¡with ¡filtering. ¡ ¡ How ¡can ¡we ¡change ¡our ¡func5on ¡so ¡uppercase/ lowercase ¡are ¡switched, ¡and ¡everything ¡else ¡is ¡ removed? ¡ ¡

slide-18
SLIDE 18
  • Write ¡a ¡func1on ¡called ¡change_nums ¡that ¡

increments ¡all ¡numbers ¡in ¡a ¡string ¡by ¡one: ¡

– Example: ¡change_nums("a1b2") ¡returns ¡"a2b3" ¡

  • Write ¡a ¡func1on ¡called ¡encode ¡that ¡takes ¡a ¡

string ¡and ¡encodes ¡it ¡using ¡the ¡simple ¡cipher ¡ A=1, ¡B=2, ¡C=3, ¡and ¡so ¡on. ¡

  • Example: ¡encode("abc") ¡returns ¡"1-­‑2-­‑3". ¡
  • Hint: ¡use ¡a ¡variable ¡leFers ¡= ¡"abcdefgh…" ¡and ¡

the ¡find ¡func1on. ¡

– What ¡is ¡leFers.find("a")? ¡ ¡leFers.find("b")? ¡

  • Challenge ¡(hard): ¡write ¡a ¡decode ¡func1on. ¡