Homework

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.

Hide code cell source

### Solution
# True
# Rows and columns describe the dimensions of an array-like dataset.

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.

Hide code cell source

### Solution
# True
# Vectorized operations apply one calculation across many values.

3. Index Positions

In Python-style array indexing, position 1 always refers to the first value in a sequence.

Hide code cell source

### Solution
# False
# Python indexing starts at 0, so position 1 is the second value.

4. Boolean Masks

A Boolean mask can represent which stores met a sales target before selecting only those stores.

Hide code cell source

### Solution
# True
# Masks mark which values satisfy a condition.

5. Random Seeds

Setting a random seed guarantees that every possible future simulation will produce different values each time it runs.

Hide code cell source

### Solution
# False
# A seed is used for reproducibility, not guaranteed difference.
### 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.

Hide code cell source

### Solution
sales = [[20, 22], [21, 26]]

week_1_total = sum(sales[0])
week_2_total = sum(sales[1])

print("Week 1 total:", week_1_total)
print("Week 2 total:", week_2_total)
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.

Hide code cell source

### Solution
prices = [100, 150, 200]
discounts = [10, 15, 20]

final_prices = []
index = 0
for price in prices:
    final_prices.append(price - discounts[index])
    index = index + 1

print("Final prices:", final_prices)
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.

Hide code cell source

### Solution
demand = [28, 30, 32, 35, 37]

recent_demand = demand[2:]
print("Recent demand:", recent_demand)
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.

Hide code cell source

### Solution
sales = [80, 105, 110, 95]
target = 100

met_target = []
for sale in sales:
    met_target.append(sale >= target)

print("Met target:", met_target)
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.

Hide code cell source

### Solution
simulated_demand = [48, 52, 55, 49]

average_simulated_demand = sum(simulated_demand) / len(simulated_demand)
print("Average simulated demand:", average_simulated_demand)
Average simulated demand: 51.0