- 🏆 60 points available
- ✏️ Last updated on 9/16/2025
▶️ First, run the code cell below to import unittest, the module used for the 🧭 Check Your Work boxes and the autograder.
# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()🎯 Problem 1: Compare Nike and McDonald’s Advertising Spending 📊¶
👇 Tasks¶
You are given two list variables named nike_spending and mcdonalds_spending. Each list contains advertising spending (in millions) for fiscal years 2015–2019.
Create the following three variables using one or more for loops:
nike_total: The sum of Nike’s ad spending for FY 2015–2019 (in millions).mcdonalds_total: The sum of McDonald’s ad spending for FY 2015–2019 (in millions).nike_higher_spending_years: The number of years Nike’s ad spending surpassed McDonald’s.
💡 Expected Output¶
Nike Total: 7240, McDonald's Total: 7560, Nike spent more on advertising 0 time(s)!nike_spending = [1400, 1470, 1460, 1440, 1470]
mcdonalds_spending = [1430, 1460, 1510, 1540, 1620]
# YOUR CODE BEGINS
# YOUR CODE ENDS
# DO NOT CHANGE THE CODE BELOW
print(
f"Nike Total: {nike_total}, McDonald's Total: {mcdonalds_total}, Nike spent more on advertising {nike_higher_spending_years} time(s)!"
)🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "compare-nike-and-mcdonalds-advertising-spending"
_points = 7
tc.assertEqual(nike_total, 7240)
tc.assertEqual(mcdonalds_total, 7560)
tc.assertEqual(nike_higher_spending_years, 1)🎯 Problem 2: McDonald’s Employees-to-Locations Ratio 🍔¶
👇 Tasks¶
You are given a list variable named mcdonalds, which contains a list of dictionaries.
Each dictionary represents a fiscal year and contains the following keys:
"year": The fiscal year"num_employees": The number of employees in that year"num_locations": The number of locations in that year
👉 Create a new list variable named loc_emp_ratios. Use a for loop to calculate the ratio of employees to locations for fiscal years 2015–2019. During each iteration in the for loop, append the new ratio to the loc_emp_ratios list.
🧭 Example¶
If num_employees is 11500 and num_locations is 1000 for a given year,
the ratio is 11.5 (11500 / 1000).
💡 Sample Output¶
Your printed values will be different — this only demonstrates the format.
[11.0, 10.2, 8.7, 5.6, 5.4]mcdonalds = [
{"year": 2015, "num_employees": 401775, "num_locations": 36525},
{"year": 2016, "num_employees": 376380, "num_locations": 36900},
{"year": 2017, "num_employees": 236988, "num_locations": 27240},
{"year": 2018, "num_employees": 211988, "num_locations": 37855},
{"year": 2019, "num_employees": 208953, "num_locations": 38695},
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(loc_emp_ratios)🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "mcdonalds-employees-to-locations-ratio"
_points = 8
tc.assertEqual(loc_emp_ratios, [11.0, 10.2, 8.7, 5.6, 5.4])🎯 Problem 3: Is McDonald’s a fast-food company? 🏬¶
👇 Tasks¶
Is McDonald’s a fast-food company? Well duh, right? McDonald’s is also a brilliant real estate company. We will examine the franchise revenue percentages from real estate.
You are given a list of dictionaries named franchise_revenues. Each dictionary contains the amount of franchise revenues from rents and royalties each year in millions (from fiscal years 2015-2019).
👉 Using a for loop, add a new key-value pair to each dictionary in the franchise_revenues list.
- Key should be named
rents_percent. - Value should be the percent of
rentsout of the total franchise revenues. As an example, ifrentsis3000androyaltiesis2000,rents_percentwill be60(3000 / (3000 + 2000) * 100).
🧭 Example¶
As an example, the first dictionary in franchise_revenues should be {'rents': 5860.6, 'royalties': 2980.7, 'rents_percent': 66.2866...} after you run your code.
💡 Expected Output¶
FY 2015: 66.3% of franchise revenues came from real estate.
FY 2016: 66.1% of franchise revenues came from real estate.
FY 2017: 64.9% of franchise revenues came from real estate.
FY 2018: 64.6% of franchise revenues came from real estate.
FY 2019: 64.6% of franchise revenues came from real estate.franchise_revenues = [
{"rents": 5860.6, "royalties": 2980.7},
{"rents": 6107.6, "royalties": 3129.9},
{"rents": 6496.3, "royalties": 3518.7},
{"rents": 7082.2, "royalties": 3886.3},
{"rents": 7500.2, "royalties": 4107.1},
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
# DO NOT CHANGE THE CODE BELOW
fy = 2015
for o in franchise_revenues:
print(
f"FY {fy}: {'{:.1f}%'.format(o['rents_percent'])} of franchise revenues came from real estate."
)
fy += 1🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "is-mcdonalds-a-fast-food-company"
_points = 8
for o in franchise_revenues:
o["rents_percent"] = int(o["rents_percent"])
tc.assertListEqual(
franchise_revenues,
[
{"rents": 5860.6, "royalties": 2980.7, "rents_percent": 66},
{"rents": 6107.6, "royalties": 3129.9, "rents_percent": 66},
{"rents": 6496.3, "royalties": 3518.7, "rents_percent": 64},
{"rents": 7082.2, "royalties": 3886.3, "rents_percent": 64},
{"rents": 7500.2, "royalties": 4107.1, "rents_percent": 64},
],
)🎯 Problem 4: Find the Company with Highest Revenue per Employee 🛢️¶
👇 Tasks¶
You are given the following three lists containing information about five companies in Oman.
company: Names of the companyrevenue: Revenue of each company (in millions)num_employees: Number of employees
Revenue per Employee is calculated by revenue / num_employees.
👉 Use a for loop to find the company with the highest revenue per employee and update the following two keys in the highest dictionary.
company: Name of the company with the highest revenue per employee. (highest["company"])revenue_per_employee: Revenue per employee (highest["revenue_per_employee"])
💡 Expected Output¶
Shell Oman's revenue per employee is the highest at 5.25!company = [
"Oman Telecommunications",
"Bank Dhofar",
"Bank Muscat",
"Shell Oman",
"Oman Oil Marketing",
]
revenue = [5685.3, 583.0, 1624.3, 1375.5, 1626.5]
num_employees = [2511, 1600, 3779, 262, 800]
highest = {"company": "", "revenue_per_employee": 0}
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(
f"{highest['company']}'s revenue per employee is the highest at {highest['revenue_per_employee']}!"
)🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "company-with-highest-revenue-per-employee"
_points = 9
tc.assertEqual(highest, {"company": "Shell Oman", "revenue_per_employee": 5.25})🎯 Problem 5: Sales Tax Calculation 💵¶
👇 Tasks¶
👉 Create a function that calculates the after-tax price given a before-tax price and the sales tax percentage. The function, calculate_total(), should take the following two parameters.
price: before-tax price of an itemtax: the sales tax percentage in decimal
0.08indicates 8%0.2indicates 20%
🧭 Example¶
# The code below should print 108
# This after-tax price can be calculated by 100 * (1 + 0.08)
print(calculate_total(100, 0.08))💡 Expected Output¶
108.0
24.0
10.5# YOUR CODE BEGINS
# YOUR CODE ENDS
print(calculate_total(100, 0.08))
print(calculate_total(20, 0.2))
print(calculate_total(10, 0.05))🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "sales-tax-calculation"
_points = 9
tc.assertAlmostEqual(calculate_total(100, 0.08), 108.0)
tc.assertAlmostEqual(calculate_total(20, 0.2), 24.0)
tc.assertAlmostEqual(calculate_total(10, 0.05), 10.5)
tc.assertAlmostEqual(calculate_total(200, 0.12), 224)
tc.assertAlmostEqual(calculate_total(100, 1), 200)🎯 Problem 6: Mean population of cities in Texas 🏠¶
👇 Tasks¶
You are given a list variable named cities that contains a list of dictionaries. Each dictionary contains the name, state, and population of a city.
👉 Use a for loop to find the mean population of cities in Texas (TX). Store the result in a new variable named texas_mean_pop.
🧭 Hint¶
Use the following two variables with a for loop (this is only a suggestion, we only check your texas_mean_pop result):
pop_sum: Incrementpop_sumbypopulationif a city’s state is"TX".num_citiesIncrementnum_citiesby 1 if a city’s state is"TX".
Then, texas_mean_pop would be pop_sum / num_cities.
💡 Expected Output¶
Mean city population of Texas is 1560856.6666666667.cities = [
{"name": "Austin", "state": "TX", "population": 1011790},
{"name": "Boston", "state": "MA", "population": 695506},
{"name": "Houston", "state": "TX", "population": 2323660},
{"name": "Los Angeles", "state": "CA", "population": 3983540},
{"name": "Dallas", "state": "TX", "population": 1347120},
{"name": "San Diego", "state": "CA", "population": 1427720},
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Mean city population of Texas is {texas_mean_pop}.")🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "mean-population-of-cities-in-texas"
_points = 9
tc.assertAlmostEqual(texas_mean_pop, 1560856.6666666667)🎯 Problem 7: Mean population of cities in a given state 🔮¶
👇 Tasks¶
Let’s make your logic from the previous question reusable by creating a function.
👉 Create a function named get_mean_pop that takes the following two inputs (parameters):
cities- a list of dictionaries. Each dictionary in the list contains the name, state, and population of a city.state- a string value containing the 2-letter state code (e.g.,"CA","CO").
- The
get_mean_pop()should return the mean population of cities in a givenstateinside the givencities.
🧭 Example 1¶
get_mean_pop(cities_group1, "CA") should return the mean population of cities in California ("Los Angeles" and "San Diego") present in cities_group1. The exact value is 2705630.0.
🧭 Example 2¶
get_mean_pop(cities_group2, "CO") should return the mean population of cities in Colorado ("Colorado Springs" and "Denver") present in cities_group2. The exact value is 597552.5.
💡 Expected Output¶
1560856.6666666667
2705630.0
695506.0
597552.5cities_group1 = [
{"name": "Austin", "state": "TX", "population": 1011790},
{"name": "Boston", "state": "MA", "population": 695506},
{"name": "Houston", "state": "TX", "population": 2323660},
{"name": "Los Angeles", "state": "CA", "population": 3983540},
{"name": "Dallas", "state": "TX", "population": 1347120},
{"name": "San Diego", "state": "CA", "population": 1427720},
]
cities_group2 = [
{"name": "Colorado Springs", "state": "CO", "population": 489529},
{"name": "Denver", "state": "CO", "population": 705576},
]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(get_mean_pop(cities_group1, "TX"))
print(get_mean_pop(cities_group1, "CA"))
print(get_mean_pop(cities_group1, "MA"))
print(get_mean_pop(cities_group2, "CO"))🧭 Check Your Work¶
Run the code cell below to test your solution.
- ✔️ If the code cell runs without errors, you’re good to move on.
- ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "mean-population-of-cities-in-any-state"
_points = 10
test_cities = [
{"name": "Las Vegas", "state": "NV", "population": 667501},
{"name": "Denver", "state": "CO", "population": 749103},
{"name": "Colorado Springs", "state": "CO", "population": 489529},
{"name": "Aurora", "state": "CO", "population": 388723},
{"name": "Henderson", "state": "NV", "population": 341531},
{"name": "Anaheim", "state": "CA", "population": 349699},
]
tc.assertAlmostEqual(get_mean_pop(cities_group1, "TX"), 1560856.6666666667)
tc.assertAlmostEqual(get_mean_pop(cities_group1, "CA"), 2705630.0)
tc.assertAlmostEqual(get_mean_pop(cities_group1, "MA"), 695506.0)
tc.assertAlmostEqual(get_mean_pop(cities_group2, "CO"), 597552.5)
tc.assertAlmostEqual(get_mean_pop(test_cities, "NV"), 504516.0)
tc.assertAlmostEqual(get_mean_pop(test_cities, "CO"), 542451.6666666666)
tc.assertAlmostEqual(get_mean_pop(test_cities, "CA"), 349699.0)