3.14 Libraries HW
Popcorn Hack 1
# Popcorn Hack 1: Run it, then change the jersey range to 1-55 and predict the new output
import math
import random
print("random.randint:", random.randint.__doc__)
print("math.ceil:", math.ceil.__doc__)
print()
points_per_game = [24, 18, 31, 27, 22]
print("Random game to review:", random.choice(points_per_game), "points")
# Jersey range updated to 1-55
jersey = random.randint(1, 55)
print("Random jersey number:", jersey)
# Court size changed to 256
side = math.sqrt(256)
print("Square court side:", side, "feet")
average = sum(points_per_game) / len(points_per_game)
rounded_up = math.ceil(average)
print("Points per game:", average, "-> rounded up:", rounded_up)
random.randint: Return random integer in range [a, b], including both end points.
math.ceil: Return the ceiling of x as an Integral.
This is the smallest integer >= x.
Random game to review: 31 points
Random jersey number: 45
Square court side: 16.0 feet
Points per game: 24.4 -> rounded up: 25
Popcorn Hack 2
# Popcorn Hack 2: Run it, then change the roster and add a route of your own, such as /coach
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/score")
def score():
return jsonify({"home": 58, "away": 52, "quarter": 3})
@app.route("/roster")
def roster():
# Roster updated with new players
return jsonify({"players": ["Maxwell", "Trevor", "Oliver", "Mateo"]})
# New custom route added
@app.route("/coach")
def coach():
return jsonify({"head_coach": "Smith", "assistant_coach": "Jones"})
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())
/score says: {'away': 52, 'home': 58, 'quarter': 3}
/roster says: {'players': ['Maxwell', 'Trevor', 'Oliver', 'Mateo']}
/coach says: {'assistant_coach': 'Jones', 'head_coach': 'Smith'}
Homework Hack
# Homework Hack: Run it, then use your own games and your own next game date
import json
import statistics
from datetime import date
# Custom "sport" data based on VEX Robotics tournament matches
matches = [
{"date": "2026-09-14", "alliance": "Red", "score": 115, "autonomous_bonus": True},
{"date": "2026-09-15", "alliance": "Blue", "score": 98, "autonomous_bonus": False},
{"date": "2026-09-15", "alliance": "Red", "score": 142, "autonomous_bonus": True},
{"date": "2026-09-16", "alliance": "Blue", "score": 105, "autonomous_bonus": True},
]
def summarize_season(season):
"""
Summarize a list of competition matches.
Parameters:
season: list of dictionaries, each with "date", "alliance", "score", and "autonomous_bonus"
Returns:
dictionary with total_points, average_points, highest_score, and points_by_alliance
"""
scores = [match["score"] for match in season]
points_by_alliance = {}
for match in season:
alliance = match["alliance"]
points_by_alliance[alliance] = points_by_alliance.get(alliance, 0) + match["score"]
return {
"total_points": sum(scores),
"average_points": round(statistics.mean(scores), 1),
"highest_score": max(scores),
"points_by_alliance": points_by_alliance,
}
report = summarize_season(matches)
# Calculate days until the next robotics tournament
next_tournament = date(2026, 11, 2)
report["days_until_next_tournament"] = (next_tournament - date.today()).days
print(json.dumps(report, indent=2))
print()
# requirements.txt lists external packages like pandas and Flask.
# Standard libraries like json, datetime, and statistics come with Python and are never listed.
requirements_txt = """pandas
Flask
"""
print("requirements.txt:")
print(requirements_txt)
{
"total_points": 460,
"average_points": 115,
"highest_score": 142,
"points_by_alliance": {
"Red": 257,
"Blue": 203
},
"days_until_next_tournament": 33
}
requirements.txt:
pandas
Flask