--- # A Real-World Example * Me and Prof. Scheidegger have vastly different working styles and workflows. * He works on Ubuntu Linux with Kate, and I use a combination of Mac and Arch Linux laptops with an array of esoteric editors. * He prefers to write code in an elegant, minimalist way, while I like to think in terms of modular, reusable components and pipelines with OOP. * But we collaborate on research projects, as he is my PhD supervisor. * How do we make this work? * We use modern programming tools and practices to bridge the gap between our different styles!
This helps us understand where you're starting from in terms of development environments!
# Part 3: A Quick, Live Coding Puzzle **(No pressure! This is just to warm up our brains.)** --- # The Puzzle: "Character Frequency Counter" **The Goal:** Write a simple Python function that takes a string of text and returns a dictionary counting the frequency of each character. **Example:** * **Input:** `"hello world"` * **Expected Output:** `{'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}` --- # Let's Solve It Together (Live) How would we approach this? Let's brainstorm. 1. What do we need to start with? (A function definition). 2. How can we store the counts? (A dictionary seems right). 3. How do we go through the input string? (A `for` loop). 4. Inside the loop, what's the logic? * If we've seen this character before... * If this is a new character... *(Here, you would live-code a solution, engaging the audience for suggestions. Start with a simple, slightly inefficient version, and then perhaps show a more "Pythonic" one if the audience is advanced).* --- ### A Possible Solution ```python def count_characters(text: str) -> dict[str, int]: # Create an empty dictionary to store our counts frequency = {} # Loop through each character in the input text for char in text: # If we have already seen this character, increment its count if char in frequency: frequency[char] += 1 # Otherwise, this is the first time we've seen it, so add it else: frequency[char] = 1 return frequency # Let's test it! result = count_characters("hello world") print(result) ```