3.14 Libraries HW
Homework submission for OCS Python 3.14 Libraries HW
Popcorn Hack 1: Read the API, Then Use It (5 minutes)
Prediction: Changing jersey from 1-99 to 1-55 would limit the range of jersey to simply 1-55.
Code Runner Challenge
Popcorn Hack 2
View IPYNB Source
%%python
# !pip install math
#!pip install random
# CODE_RUNNER: Popcorn Hack 2
import math
import random
# Read the API before calling a procedure
print("random.randint:", random.randint.__doc__)
print("math.ceil:", math.ceil.__doc__)
print(dir(math))
print(dir(random))
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")
# random.randint(a, b) returns a whole number from a to b, including both
jersey = random.randint(1, 55)
print("Random jersey number:", jersey)
# math.sqrt returns the square root, so a 144 square foot court is this wide
side = math.sqrt(144)
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: Write a Flask Route (5 minutes)
No code runner for this one
from flask import Flask, jsonify
app = Flask(__name__)
# A route is a procedure with a web address attached to it
@app.route("/score")
def score():
return jsonify({"home": 58, "away": 52, "quarter": 3})
@app.route("/roster")
def roster():
return jsonify({"players": ["Harry", "Bob", "Alice"]})
@app.route("/coach")
def coach():
return jsonify({"name": "Coach M"})
# 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())
/score says: {'away': 52, 'home': 58, 'quarter': 3}
/roster says: {'players': ['Harry', 'Bob', 'Alice']}
/coach says: {'name': 'Coach M'}
Homework Hack: Season Stats Report
Code Runner Challenge
Homework Hack
View IPYNB Source
%%python
# CODE_RUNNER: Homework Hack
import json
import statistics
from datetime import date
matches = [
{"date": "2026-09-14", "alliance": "Red", "score": 115, "autonomous_bonus": True, "hang_bonus": False},
{"date": "2026-09-15", "alliance": "Blue", "score": 98, "autonomous_bonus": False, "hang_bonus": True},
{"date": "2026-09-15", "alliance": "Red", "score": 142, "autonomous_bonus": True, "hang_bonus": True},
{"date": "2026-09-16", "alliance": "Blue", "score": 105, "autonomous_bonus": True, "hang_bonus": True},
]
def summarize_season(season):
"""
Summarize a list of competition matches.
Parameters:
season: list of dictionaries, each with "date", "alliance", "score", "autonomous_bonus", and "hang_bonus"
Returns:
dictionary with total_points, average_points, highest_score, and points_by_alliance
"""
scores = []
points_by_alliance = {}
for match in season:
effective_score = match["score"]
if match["autonomous_bonus"]:
effective_score += 30
if match["hang_bonus"]:
effective_score += 45
scores.append(effective_score)
alliance = match["alliance"]
points_by_alliance[alliance] = points_by_alliance.get(alliance, 0) + effective_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)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...