11.3.2. Lab#
Use this lab to practice the chapter’s coding skills in runnable Python code.
Sign in from the account menu before you start working on the lab.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: Custom Iterator: Threshold Filter
# A sales team wants to review only the transactions above a threshold.
# Implement a class AboveThreshold(values, threshold) with __iter__/__next__
# that returns, one at a time, only the values greater than threshold.
# Skip values at or below threshold instead of stopping on them.
# Raise StopIteration once every value has been checked.
# Collect the filtered values with list(), then print exactly these two lines:
# Sales above 1000: [1200, 2100, 1600]
# Count above 1000: 3
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
Sales above 1000: [1200, 2100, 1600]
Count above 1000: 3
### Question 2: Custom Iterator: Batch Sales Data
# Large sales exports are processed in fixed-size batches.
# Implement a class Batched(values, size) with __iter__/__next__ that
# returns successive lists (batches) of length size from values.
# The final batch may be shorter than size if values does not divide evenly.
# Raise StopIteration once every value has been placed in a batch.
# Loop over Batched(sales, 2) with a for loop and print each batch (one per line):
# [1200, 850]
# [2100, 430]
# [975, 1600]
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
[1200, 850]
[2100, 430]
[975, 1600]
### Question 3: Generator Function: Running Maximum
# Write a generator function running_max(numbers) that yields the
# largest value seen so far after each element (a running maximum).
# Example: running_max([1, 5, 3, 8]) yields 1, 5, 5, 8.
# Collect the results with list() on the sales data and print exactly:
# [1200, 1200, 2100, 2100, 2100, 2100]
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
[1200, 1200, 2100, 2100, 2100, 2100]
### Question 4: Generator Expressions with islice, any, and all
# Use a generator expression to compute a 10% discount on each sale:
# round(s * 0.9, 2) for each s in sales.
# Use itertools.islice to take only the first 3 discounted values.
# Use any() with a generator expression to check if any sale exceeds 1000.
# Use all() with a generator expression to check if every sale exceeds 400.
# Print exactly these three lines:
# First three discounted: [1080.0, 765.0, 1890.0]
# Any sale over 1000: True
# All sales over 400: True
import itertools
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
First three discounted: [1080.0, 765.0, 1890.0]
Any sale over 1000: True
All sales over 400: True
### Question 5: Infinite Generator with itertools.islice
# Write a generator function grow_sales(base, rate) that models
# compound growth: it yields base, then repeatedly multiplies the
# previous value by (1 + rate) and yields the result, forever (while True).
# Round each yielded value to 2 decimal places.
# Always bound consumption of an infinite generator with itertools.islice
# so the loop terminates.
# Take the first 5 values from grow_sales(1000, 0.1) and print exactly:
# Projected sales: [1000, 1100.0, 1210.0, 1331.0, 1464.1]
import itertools
### Your code starts here.
### Your code ends here.
Projected sales: [1000, 1100.0, 1210.0, 1331.0, 1464.1]
Question 1
Question 2
Question 3
Question 4
Question 5