class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 8:] .title[Iteration and strings] ] .footer[[/lecture/8/](/lecture/8/)] --- # Designing with loops -- <img src="https://media.wired.com/photos/5909536dd8c8646f38eef3c0/4:3/w_929,h_697,c_limit/ap_insideout_ff1_HPA.jpg" width="450" align="right" alt="Characters from the Pixar film Inside Out" style="mix-blend-mode: multiply" /> -- ### Work from the _inside out:_ -- 1. What should this loop do **every time**? -- 2. How do we start and finish? -- ### Example: summation ??? Example: 1. Ask a user to enter some numbers. 2. Add them up until the user enters an empty string. 3. Print the total. -- ### [Project Euler](https://projecteuler.net) ??? After today's lecture, you will be equipped to tackle [problem 16 in Project Euler](https://projecteuler.net/problem=16). It's a puzzle, so expect to spend some time thinking about different ways to tackle it, but you will have everything you need to know in order to do it! And remember: don't think like a _mathemetician_, think like a _programmer_. --- # Iteration -- ### Walking through a bunch of things, _one at a time_ -- ### IRL: design _iterations_ -- ### Computing: loops! -- .floatright[ ```python for letter in 'Jonathan Anderson': print(letter) ``` ] -- * get "next" letter, use it -- * ... until no more letters -- #### Later: lists, arrays and `iter()` --- # Iteration problems -- 1. Count frequency of "e" in a string (upper- and lower-case) -- 2. For each letter in a string, count instances of that letter -- 3. Count upper-case characters in a string -- **(??)** --- # More string detail ### What is a string? -- ### What is a character? -- ## A, B, C -- ... 🧙? -- ### Some history: -- [ASCII](https://en.wikipedia.org/wiki/ASCII#Character_set) ??? How do you represent language to others? * sounds * writing How would you transmit letters over a distance? https://en.wikipedia.org/wiki/Telegraph_code#Electrical_telegraph_codes -- and [Unicode](https://en.wikipedia.org/wiki/Unicode) -- ### Python: `chr()` and `ord()` --- # Iteration problems 1. Count frequency of "e" in a string (upper- and lower-case) 2. For each letter in a string, count instances of that letter 3. **Count upper-case characters in a string** -- 4. Does a string have more upper- or lower-case characters? 5. Add up a string's letter values (A=1, B=2, etc.) 6. Compare two strings by alphabetical order --- class: big, middle (here endeth the lesson)