Homework
Question Recap: Which is LEAST likely to improve the model’s accuracy?
Answer: C – Removing as many details from the model as possible so that calculations can be performed quickly.
import random
def roll_dice():
"""Simulates a 6-sided dice roll."""
return random.randint(1, 6)
# Simulate and print a dice roll
print("Dice roll:", roll_dice())
Dice roll: 6
import random
def biased_color():
"""Generates biased color with:
- Red (50%)
- Blue (30%)
- Other colors (20% combined)
"""
colors = ["Red", "Blue", "Green", "Yellow", "Purple"]
probabilities = [0.5, 0.3, 0.066, 0.067, 0.067] # Total = ~1.0
return random.choices(colors, probabilities)[0]
# Print 10 biased random colors
for _ in range(10):
print("Color:", biased_color())
Color: Red
Color: Red
Color: Red
Color: Red
Color: Blue
Color: Blue
Color: Red
Color: Yellow
Color: Purple
Color: Yellow
import random
def coin_flip_game():
player1_heads = 0
player2_heads = 0
rounds = 0
while player1_heads < 3 and player2_heads < 3:
rounds += 1
# Player 1 flip
if random.choice(["heads", "tails"]) == "heads":
player1_heads += 1
# Player 2 flip
if random.choice(["heads", "tails"]) == "heads":
player2_heads += 1
print(f"Round {rounds}: Player 1 = {player1_heads} heads, Player 2 = {player2_heads} heads")
# Declare the winner
if player1_heads == 3:
print(f"Player 1 wins in {rounds} rounds!")
else:
print(f"Player 2 wins in {rounds} rounds!")
coin_flip_game()
Round 1: Player 1 = 1 heads, Player 2 = 1 heads
Round 2: Player 1 = 2 heads, Player 2 = 2 heads
Round 3: Player 1 = 2 heads, Player 2 = 2 heads
Round 4: Player 1 = 2 heads, Player 2 = 3 heads
Player 2 wins in 4 rounds!