OIM3640 - Problem Solving and Software Design

2026 Spring

Session 10 (2/24) - via Webex

contain

Today's Agenda

  • Announcements/Updates
  • What We've Learned So Far
  • Review Questions
  • Lecture: Chapter 8 - Strings
  • Work Time: Spelling Bee / Exercises / Mini Project

Announcements/Updates

  • Quiz 1 next session (Thursday 2/26)
    • Scope: code reading, loops, return patterns, predict output
    • Similar format to Quiz 0
  • Mini Project 1 - how's your progress?
    • Have you submitted PROPOSAL.md?
    • Come to office hours if you're stuck
  • Getting Help
    • Office Hours: Walk-in or by appointment
    • Email: Specify course # in subject, e.g., "OIM3640: GitHub settings"
  • Questions?

💻 Remote Class Today

Due to the snowstorm, we're meeting on Webex today.

  • Keep your camera on if possible
  • Use the chat to answer questions and ask questions
  • Raise your hand (Webex reaction) or unmute to speak
  • I'll share my screen for demos - follow along in your own VS Code

What We've Learned So Far

  • Variables & Types: int, float, str, bool, NoneType
  • Operators: +, -, *, /, //, %, **
  • Functions: def, parameters, return vs print(), composition
  • Conditionals: if/elif/else, boolean expressions
  • Iteration: for loops (range, sequences), while loops, break, continue
  • Search patterns: the in operator, early return, counters
  • Turtle: Drawing shapes with loops and functions

🙋 Review Questions

Type your answers in the Webex chat!

  1. What is the output?

    count = 0
    for letter in 'mississippi':
        if letter == 's':
            count += 1
    print(count)
    
  2. What keywords can exit a loop early? Skip to the next iteration?

🙋 Review Questions (continued)

  1. What will be the last line of output? Type in chat.

    n = 6
    while n > 0:
        print(n)
        n = n - 2
    
    • What if line 2 is changed to while n >= 0:?
    • What about while n != 0:?
    • What about:
      n = 5
      while n != 0:
          print(n)
          n = n - 2
      

🙋 Review Questions (continued)

  1. What does this function return for uses_any('hello', 'xyz')? Type in chat.

    def uses_any(word, letters):
        for letter in word:
            if letter in letters:
                return True
        return False
    
    • What would uses_any('hello', 'aeiou') return?
    • What if return False was inside the else branch of the if?

🙋 Review Questions (continued)

  1. What does each function do differently? Type in chat.

    def version_a(word):           def version_b(word):
        for letter in word:            for letter in word:
            if letter in 'aeiou':          if letter in 'aeiou':
                print(letter)                  return letter
        print('Done')                  return 'None found'
    

    What happens when you call version_a('hello')? What about version_b('hello')?

🙋 Review Questions (continued)

  1. for or while? Which loop type is better for each task?

    a. Print each character in a string
    b. Keep rolling a die until you get a 6
    c. Count the vowels in a word
    d. Ask the user for input until they type "done"

for vs while - Reference

  • Use a for loop when:
    • The number of iterations is known in advance
    • Iterating over a range of integers
    • Looping through elements in a sequence (string, list, ...)
  • Use a while loop when:
    • The number of iterations is unknown
    • Continue until a condition becomes False
    • Continue until a break is encountered
    • Implementing input validation or retry logic

Chapter 8 - Strings

What we'll learn:

  • Strings as sequences (indexing, slicing)
  • String immutability
  • String methods
  • Brief intro to regular expressions
  • Applying it all: the Spelling Bee puzzle

Strings Are Sequences

Access individual characters with bracket notation (zero-indexed):

fruit = 'banana'
fruit[0]     # 'b'
fruit[1]     # 'a'
fruit[-1]    # 'a' (last character)
len(fruit)   # 6
fruit[6]     # IndexError! (valid indices: 0 to 5)

We already used for letter in word: - now we understand why it works!

String Slices

Extract a portion of a string with [start:end]:

fruit = 'banana'
fruit[0:3]   # 'ban'  (includes start, excludes end)
fruit[3:6]   # 'ana'
fruit[:3]    # 'ban'  (omit start = from beginning)
fruit[3:]    # 'ana'  (omit end = to end)
fruit[:]     # 'banana' (copy the whole string)

Strings Are Immutable

You cannot change a string in place:

greeting = 'Hello, world!'
greeting[0] = 'J'   # TypeError!

Instead, create a new string:

new_greeting = 'J' + greeting[1:]
new_greeting   # 'Jello, world!'

This is a key difference from lists (coming in Chapter 9).

String Methods

Methods are functions called on an object using dot notation:

word = 'banana'
word.upper()       # 'BANANA'
word.lower()       # 'banana'
word.startswith('ban')  # True
word.count('a')    # 3
word.replace('a', 'o')  # 'bonono'

Important: these return new strings. The original is unchanged!

word   # still 'banana'

Regular Expressions (Self-Study)

Python has a built-in re module for pattern matching in strings:

import re
text = "Call me at 617-555-1234"
result = re.search(r'\d{3}-\d{3}-\d{4}', text)
result.group()   # '617-555-1234'

🐝 Spelling Bee: The Puzzle

The NYT Spelling Bee gives you 7 letters (one is the center):

    T   L
  N  [O]  C
    F   I

Rules:

  • Use only these 7 letters (letters can repeat)
  • Must include the center letter (O in this case)
  • Words must be 4+ letters long
  • Find as many valid words as possible!

🐝 Spelling Bee: Designing the Solution

Let's think before we code. What checks does each word need to pass?

  1. Is it at least 4 letters long?
  2. Does it contain the center letter?
  3. Does it use only the 7 allowed letters?
  4. Is it a real English word? (check words.txt)

What functions should we write?

uses_only(word, letters)   # Ch7 search pattern!
is_bee_valid(word, letters, center)

What patterns from last session do you recognize here?

🐝 Spelling Bee: Try It Yourself

Use what you've learned about strings and search patterns to build this!

  1. Write uses_only(word, letters) - which search pattern fits?
  2. Combine checks into is_bee_valid(word, letters, center)
  3. Loop through words.txt and find all valid words
  4. How many words can you find for the puzzle?

Feel free to use AI to help, but understand every line before moving on.

🐝 Spelling Bee: Extensions

Scoring:

  • 4-letter words = 1 point
  • Longer words = 1 point per letter
  • Pangram (uses all 7 letters) = +7 bonus!

Challenges:

  • Write score_word(word, letters)
  • Write is_pangram(word, letters) - how would you check this?
  • Find all pangrams in the puzzle

⏰ Work Time

Use the remaining time to work on any of these (stay on the call if you have questions):

  • Spelling Bee puzzle (slides above)
  • Chapter 8 exercises from the textbook
  • Mini Project 1 - proposal or coding
  • Quiz 1 prep - practice predicting output of loop/return patterns

I'm here to help - ask in chat or unmute!

Before You Leave

  • Any questions?
  • Study for Quiz 1 (Thursday 2/26)
    • Practice predicting output of loop/return patterns
  • Start your Learning Log for this week (logs/wk06.md)
  • Continue working on Mini Project 1
  • Push your work to GitHub

Next session: Quiz 1 + Lists (Chapter 9)

global styles