11.4.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly 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()`.
# 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.
### 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.
### 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.
# Print exactly:
# [1200, 1200, 2100, 2100, 2100, 2100]
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
### 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 is below 500.
# Use `all()` with a generator expression to check if every sale is at least 500.
# Print exactly these three lines:
# First three discounted: [1080.0, 765.0, 1890.0]
# Any sale below 500: True
# All sales at least 500: False
import itertools
sales = [1200, 850, 2100, 430, 975, 1600]
### Your code starts here.
### Your code ends here.
### 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)`.
# Print exactly:
# Projected sales: [1000, 1100.0, 1210.0, 1331.0, 1464.1]
import itertools
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5