strings iv warm up write a func1on called count dups that
play

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:


  1. Strings ¡IV ¡

  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"

  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 ¡ Is ¡s[pos] ¡the ¡same ¡ ¡ ¡return ¡total ¡ character ¡as ¡the ¡ ¡ character ¡immediately ¡ to ¡the ¡right? ¡

  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 ¡ Is ¡s[pos] ¡the ¡same ¡ ¡ ¡return ¡total ¡ character ¡as ¡the ¡ ¡ character ¡immediately ¡ to ¡the ¡right? ¡

  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 ¡ Is ¡s[pos] ¡the ¡same ¡ ¡ ¡return ¡total ¡ character ¡as ¡the ¡ ¡ character ¡immediately ¡ to ¡the ¡right? ¡

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

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

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

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

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

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

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

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

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

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

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend