Welcome to lab zero! As we bridge into algorithm design, this lab is meant to help you dust off your Python skills.
These problems are deliberately open-ended. You have the flexibility to design the specific logic, but you must correctly utilize the required language features. Be sure to write some test cases to ensure your code is working as intended.
Instructions: clone this repo and create a file lab00.py containing the functions below. You may work in groups of up to 3, but everyone should submit their own lab. Push your work back to your repo and submit the link to your repo in this Google form.
The Scenario: You need to process a 2D grid of raw sensor readings, flattening the data into a single sequence while filtering out noise.
Your Task: Write a function flatten_and_filter(matrix) that takes a list of lists containing integers.
- Use nested
forloops to iterate through each row, and each item within that row. - Filter the items to keep only even
intelements. - Cube each of these filtered elements (e.g.,
x ** 3). - Append the results to a single 1D list and return it.
The Scenario: You need to calculate the number of steps required for a starting integer to resolve to 1 under the rules of the Collatz conjecture.
Your Task: Write a function collatz_steps(n) that uses a while loop.
- Define a loop that continues as long as
n > 1. - Inside the loop, if
nis even, divide it by 2 (using integer division//). - If
nis odd, multiply it by 3 and add 1. - Keep track of how many total steps (iterations) it takes to reach 1, and return that count.
The Scenario: You are analyzing a string of genetic data and need to determine the frequency of each nucleotide.
Your Task: Write a function nucleotide_count(sequence).
- Accept a string representing a sequence (e.g.,
"GATTACA"). - Iterate through the string and populate a dictionary counting the occurrences of each character.
- Ensure your code safely handles the first time it encounters a character without throwing a
KeyError(you may use.get()or a manualif/elsecheck). - Return the resulting dictionary.
The Scenario: You have two separate lists of student IDs representing rosters for CS 202 and CS 303, and you need to find the enrollment overlaps and differences.
Your Task: Write a function compare_enrollments(roster_a, roster_b).
- Convert the two input lists into Python
setobjects. - Using built-in set operations, return a new dictionary containing:
"both": IDs present in both rosters."only_a": IDs exclusively in roster A."only_b": IDs exclusively in roster B."all_unique": A combined set of every unique ID across both rosters.
-
As you wrote this Python code, which concept felt the most unfamiliar?
-
Did you encounter any technical issues during this lab?
-
Looking back at Part 2, how did you logically verify that your
whileloop would not result in an infinite loop?