3.14 Libraries HW
Popcorn Hack 1
Code Runner Challenge
Popcorn Hack 1
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1
import math
import random
# Read the API before calling a procedure
print("random.randint:", random.randint.__doc__)
print("math.ceil:", math.ceil.__doc__)
points_per_game = [24, 18, 31, 27, 22]
# random.choice picks one item out of a list
print("Random game to review:", random.choice(points_per_game), "points")
# CHANGED: Jersey number range set to 1-55
jersey = random.randint(1, 55)
print("Random jersey number:", jersey)
# CHANGED: Court size changed to a 225 sq ft court
side = math.sqrt(225)
print("Square court side:", side, "feet")
# math.ceil rounds up to the next whole number
average = sum(points_per_game) / len(points_per_game)
rounded_up = math.ceil(average)
print("Points per game:", average, "-> rounded up:", rounded_up)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 2
Code Runner Challenge
Popcorn Hack 2
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2
from flask import Flask, jsonify
app = Flask(__name__)
# Route 1: Score
@app.route("/score")
def score():
return jsonify({"home": 58, "away": 52, "quarter": 3})
# Route 2: Roster (Customized roster)
@app.route("/roster")
def roster():
return jsonify({"players": ["Jordan", "LeBron", "Kobe", "Curry"]})
# Route 3: Added route /coach
@app.route("/coach")
def coach():
return jsonify({"head_coach": "Coach Carter", "assistant": "Phil Jackson"})
# Route 4: Added route /record
@app.route("/record")
def record():
return jsonify({"wins": 12, "losses": 3, "streak": "W4"})
# test_client visits the addresses without needing a browser
client = app.test_client()
print("/score says:", client.get("/score").get_json())
print("/roster says:", client.get("/roster").get_json())
print("/coach says:", client.get("/coach").get_json())
print("/record says:", client.get("/record").get_json())
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Hack
Code Runner Challenge
Homework Hack - Season Stats Report
View IPYNB Source
# CODE_RUNNER: Homework Hack - Season Stats Report
import json
import statistics
from datetime import date
import random
# Sample match data for a soccer season
matches = [
{"date": "2026-09-10", "opponent": "Real Madrid", "goals": 3, "minutes": 90},
{"date": "2026-09-18", "opponent": "Barcelona", "goals": 1, "minutes": 75},
{"date": "2026-09-24", "opponent": "Real Madrid", "goals": 2, "minutes": 90},
{"date": "2026-10-01", "opponent": "Bayern Munich", "goals": 4, "minutes": 88},
]
def summarize_season(season):
"""
Summarizes team performance across a list of sports matches.
Parameters:
season (list of dict): A list of match dictionaries containing
'date', 'opponent', 'goals', and 'minutes'.
Returns:
dict: A summary dictionary with the following keys:
- 'total_goals' (int): Sum of all goals scored.
- 'average_goals' (float): Mean goals per match rounded to 1 decimal place.
- 'best_game' (int): Maximum goals scored in a single match.
- 'goals_by_opponent' (dict): Total goals scored against each opponent.
- 'mvp_jersey_number' (int): A randomly assigned player number for match recognition.
"""
goals = [match["goals"] for match in season]
goals_by_opponent = {}
for match in season:
opponent = match["opponent"]
goals_by_opponent[opponent] = goals_by_opponent.get(opponent, 0) + match["goals"]
return {
"total_goals": sum(goals),
"average_goals": round(statistics.mean(goals), 1),
"best_game": max(goals),
"goals_by_opponent": goals_by_opponent,
"mvp_jersey_number": random.randint(1, 99)
}
# Execute summary procedure
report = summarize_season(matches)
# Calculate days until next game using datetime
next_match_date = date(2026, 11, 15)
days_remaining = (next_match_date - date.today()).days
report["days_until_next_game"] = days_remaining
# Output structured report in JSON format
print("--- SEASON REPORT (JSON) ---")
print(json.dumps(report, indent=2))
# requirements.txt configuration
requirements_txt = """# Requirements for the Soccer Dashboard Application
# Note: Standard libraries (json, statistics, datetime, random) are built into Python
# and must NOT be listed here.
pandas
Flask
scikit-learn
"""
print("\n--- generated requirements.txt ---")
print(requirements_txt)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...