3.4.3. Homework#
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.
1. Array Shape
A store manager tracks sales for three departments across four weeks. Treating those values as a 3 by 4 array is a reasonable way to describe the data shape.
2. Elementwise Operations
A pricing analyst wants to add the same fee to every item price. This is the kind of repeated numeric work that vectorized array operations are designed to express.
3. Index Positions
In Python-style array indexing, position 1 always refers to the first value in a sequence.
4. Boolean Masks
A Boolean mask can represent which stores met a sales target before selecting only those stores.
5. Random Seeds
Setting a random seed guarantees that every possible future simulation will produce different values each time it runs.
### 6. Revenue Array Totals
# Treat sales as a two-row array stored with nested lists.
# Print exactly:
# Week 1 total: 42
# Week 2 total: 47
sales = [[20, 22], [21, 26]]
### Your code starts here.
### Your code ends here.
Week 1 total: 42
Week 2 total: 47
### 7. Elementwise Discounts
# Subtract each discount from the matching price.
# Store the result in final_prices.
# Print exactly:
# Final prices: [90, 135, 180]
prices = [100, 150, 200]
discounts = [10, 15, 20]
### Your code starts here.
### Your code ends here.
Final prices: [90, 135, 180]
### 8. Slice the Last Values
# Select the last three demand values.
# Print exactly:
# Recent demand: [32, 35, 37]
demand = [28, 30, 32, 35, 37]
### Your code starts here.
### Your code ends here.
Recent demand: [32, 35, 37]
### 9. Compare to Target
# Build a list showing whether each store met the target.
# Print exactly:
# Met target: [False, True, True, False]
sales = [80, 105, 110, 95]
target = 100
### Your code starts here.
### Your code ends here.
Met target: [False, True, True, False]
### 10. Simulation Average
# Treat the list as saved simulation results and calculate its average.
# Print exactly:
# Average simulated demand: 51.0
simulated_demand = [48, 52, 55, 49]
### Your code starts here.
### Your code ends here.
Average simulated demand: 51.0