3.14 Libraries Homework

POPCORN HACK 1

Code Runner Challenge

POPCORN HACK 1

View IPYNB Source
# CODE_RUNNER: POPCORN HACK 1

import math
import random

points_per_game = [24, 18, 31, 27, 22]

print("random.randint doc starts:", random.randint.__doc__.split("\n")[0])
print("math.ceil doc starts:", math.ceil.__doc__.split("\n")[0])

print("Random game to review:", random.choice(points_per_game), "points")

jersey = random.randint(1, 55)
print("Random jersey number:", jersey)

side = math.sqrt(225)
print("Square court side:", side, "feet")
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

try:
    from flask import Flask, jsonify

    app = Flask(__name__)

    @app.route("/score")
    def score():
        return jsonify({"home": 64, "away": 59, "quarter": 4})

    @app.route("/roster")
    def roster():
        return jsonify({"players": ["Jasan", "Aarav", "Ryden", "Sam"]})

    @app.route("/coach")
    def coach():
        return jsonify({"coach": "Coach Lee", "focus": "defense"})

    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())
except ModuleNotFoundError:
    print("Flask is not installed in this environment. In the class runner, this cell defines /score, /roster, and /coach.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

HOMEWORK HACK

Code Runner Challenge

HOMEWORK HACK

View IPYNB Source
# CODE_RUNNER: HOMEWORK HACK

import json
import math
import random
import statistics
from datetime import date

games = [
    {"date": "2026-09-12", "opponent": "Westview", "points": 16, "minutes": 24},
    {"date": "2026-09-18", "opponent": "Del Norte", "points": 21, "minutes": 29},
    {"date": "2026-09-22", "opponent": "Poway", "points": 13, "minutes": 20},
    {"date": "2026-09-29", "opponent": "Westview", "points": 25, "minutes": 31},
]

def summarize_season(season):
    """
    Summarize a basketball season.

    Parameters:
        season: list of dictionaries. Each dictionary has date, opponent, points, and minutes.
    Returns:
        dictionary with total_points, average_points, best_game, points_by_opponent,
        games_played, and estimated_full_game_points.
    """
    points = []
    points_by_opponent = {}
    best_game = season[0]

    for game in season:
        points.append(game["points"])

        if game["opponent"] in points_by_opponent:
            points_by_opponent[game["opponent"]] += game["points"]
        else:
            points_by_opponent[game["opponent"]] = game["points"]

        if game["points"] > best_game["points"]:
            best_game = game

    average_points = statistics.mean(points)
    average_minutes = statistics.mean([game["minutes"] for game in season])
    estimated_full_game_points = math.ceil(average_points / average_minutes * 32)

    return {
        "games_played": len(season),
        "total_points": sum(points),
        "average_points": round(average_points, 2),
        "best_game": best_game,
        "points_by_opponent": points_by_opponent,
        "estimated_full_game_points": estimated_full_game_points,
    }

report = summarize_season(games)

today = date(2026, 10, 1)
next_game = date(2026, 10, 15)
days_until_next_game = (next_game - today).days

report["next_game"] = str(next_game)
report["days_until_next_game"] = days_until_next_game
report["featured_game_to_review"] = random.choice(games)

print(json.dumps(report, indent=2))
print("Days until my next game:", days_until_next_game)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

requirements.txt