OIM3640 - Problem Solving and Software Design

2026 Spring

Session 15 (3/12)

contain

Today's Agenda

  • Announcements/Updates
  • What We've Learned So Far
  • Review Questions + Python Challenge
  • Lecture: Tuples, Sets, and Choosing Data Structures
  • Your Turn

Announcements/Updates

  • Mini Project 2 is posted - check it out!
  • Mini Project reminder - keep working on elective projects
  • Communication
    • Office Hours: Walk-in or by appointment
    • Email: Specify course # in subject, e.g., "OIM3640: GitHub settings"
    • You are required to meet with me at least once this semester
  • Questions?

What We've Learned So Far

  • Python Fundamentals
    • Variables & types, operators, expressions
    • Functions: def, parameters, return vs print()
    • Conditionals: if/elif/else
  • Iteration & Patterns
    • for loops, while loops, break, continue
  • Strings: indexing, slicing, immutability, methods
  • Lists: mutable sequences, aliasing, sorting
  • Dictionaries: key-value mappings, counting pattern

🙋 Review: Dictionaries

prices = {'AAPL': 260.81, 'NVDA': 186.00, 'MSFT': 404.88, 'GOOG': 308.42}
  • How do you get the price of 'AAPL'?
  • What happens if you do prices['TSLA']?
  • How would you find the stock with the highest price?
  • How would you get a list of all stocks priced above $200?
  • If all prices go up 10%, how do you update all values?

Counting by Using get()

def histogram(s):
    d = {}
    for c in s:
        d[c] = d.get(c, 0) + 1
    return d

result = histogram('bookkeeper')
print(result['o'])
print(result.get('o', 0))
print(result.get('z', 0))

🧩 Python Challenge

Try these puzzles at pythonchallenge.com:

  • Puzzle 0: Math (2 ** 38)
  • Puzzle 1: String manipulation (character shifting)
  • Puzzle 2: Character frequency counting (use a dict!)
  • Puzzle 3: Pattern matching in text (advanced)

Work in pairs. See how far you get in 10 minutes!

Chapter 11 - Tuples

What we'll learn:

  • Tuples as immutable sequences
  • Tuple assignment and unpacking
  • Tuples as dictionary keys
  • zip() for combining sequences

Tuples: Immutable Sequences

t = ('a', 'b', 'c', 'd')
t[0]       # 'a'
t[1:3]     # ('b', 'c')
len(t)     # 4

But you cannot modify them:

t[0] = 'X'   # TypeError! Tuples are immutable.

Like strings, unlike lists.

Tuple Assignment & Unpacking

# Swap two variables without a temp
a, b = b, a

# Unpack a tuple into variables
point = (3, 4)
x, y = point    # x = 3, y = 4

Works great with dict.items():

for key, value in prices.items():
    print(f'{key}: ${value}')

Returning Multiple Values

Tuples let functions return more than one value:

def min_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = min_max([3, 1, 4, 1, 5])
# lowest = 1, highest = 5

The return creates a tuple; unpacking assigns both at once.

zip(): Combine Two Sequences

names = ['AAPL', 'GOOG', 'MSFT']
prices = [182.30, 141.80, 415.20]

for name, price in zip(names, prices):
    print(f'{name}: ${price}')

Create a dict from two lists:

stock_prices = dict(zip(names, prices))
# {'AAPL': 182.30, 'GOOG': 141.80, 'MSFT': 415.20}

Sets: Unique Collections

A set holds unique items only, unordered:

fruits = {'apple', 'banana', 'apple', 'cherry'}
print(fruits)   # {'apple', 'banana', 'cherry'}
len(fruits)     # 3

Useful for removing duplicates:

nums = [1, 2, 2, 3, 3, 3]
unique = set(nums)       # {1, 2, 3}
len(set(nums))           # 3 unique values

Set Operations

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

a & b    # {3, 4}       intersection
a | b    # {1, 2, 3, 4, 5, 6}  union
a - b    # {1, 2}       difference

Check membership (very fast!):

3 in a   # True

Sets are great when you need fast lookup or uniqueness.

Choosing Data Structures

Type Mutable? Ordered? Access by Duplicates?
str No Yes index Yes
list Yes Yes index Yes
tuple No Yes index Yes
dict Yes Yes* key Keys: No
set Yes No membership No

* Dicts preserve insertion order (Python 3.7+).

When to Use What?

  • list: ordered collection, may have duplicates
    • Student grades, stock ticker history
  • tuple: fixed data that shouldn't change
    • GPS coordinates, function returning multiple values
  • dict: look up values by a meaningful key
    • Stock prices by ticker, word frequency counts
  • set: unique items, fast membership testing
    • Unique words in a document, valid usernames

📝 Your Turn: Ch 9 Lists

  • Exercise 9.2: is_anagram - check if two words are anagrams
  • Exercise 9.3: is_palindrome - find palindromes (7+ letters)
  • Exercise 9.4: reverse_sentence - reverse word order in a string
  • Exercise 9.5: total_length - sum lengths of all strings in a list

📝 Your Turn: Ch 10 Dicts

  • Exercise 10.2: Rewrite value_counts using get() (no if!)
  • Exercise 10.3: has_duplicates - check for repeated elements
  • Exercise 10.4: find_repeats - return keys with count > 1
  • Exercise 10.5: add_counters - combine two frequency dicts
  • Exercise 10.6: is_interlocking - split word into two valid words

📝 Your Turn: Ch 11 Tuples

  • Exercise 11.3: Caesar cipher (shift_word) - same idea as Puzzle 1!
  • Exercise 11.4: Sort letters by frequency (most_frequent_letters)
  • Exercise 11.5: Find all anagram groups from a word list
  • Exercise 11.6: word_distance - count differing positions (use zip)
  • Exercise 11.7: Find metathesis pairs (single letter swaps)

Questions? Ask now!

Before You Leave

  • Any questions?
  • Continue your Learning Log for this week (logs/wk08.md)
  • Work on Chapter 9-11 exercises
  • Review and improve your Mini Project 1 code and start to think about MP2
  • Push your work to GitHub

Next session: Text Analysis and Generation

global styles