5.9 Homework and popcorn hacks
5.9 Homework and popcorn hacks
Popcorn Hack 1:
What do you think a random algorithm is? What would be reason to use random algorithms in real-life coding situations? What kind of questions do you think College Board will ask regarding Random Algorithms?
A random algorithm uses random choices or numbers to influence its behavior, so the output can change even with the same input. Programmers use them for things like simulations, games, randomized testing, or choosing samples fairly. On the AP exam, the College Board might ask you to predict possible outcomes, explain the purpose of randomness, or trace code that uses random functions.
# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['[play soccer]', 'read a book', 'go for a walk', 'sleep', 'Watch a movie', 'eat food', 'Go for a run', 'Write in a journal', 'Meditate', 'Listen to music', ]
# Step 2: Randomly choose an activity
random_activity = random.choice(activities)
# Step 3: Display the chosen activity
print(f"Today’s random activity: {random_activity}")
Today’s random activity: read a book
# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['Hannah', 'Shriya', 'Gaheera']
activities = ['code code code', 'read', 'snack', 'photo booth', 'beach']
# Randomly shuffle the list of activities to assign them randomly to the guests
random.shuffle(activities)
# Loop through each guest and assign them a random activity
for i in range(len(hosts)):
print(f"{hosts[i]} will be monitoring {activities[i]}!")
Hannah will be monitoring snack!
Shriya will be monitoring beach!
Gaheera will be monitoring code code code!
Mc practice = Answer D
import random
def spin_number():
return random.randint(1, 20)
spinner_result = spin_number()
print("Spinner Result:", spinner_result)
Spinner Result: 7
import random
def play_rock_paper_scissors():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("Enter your choice (rock, paper, or scissors): ")
if user_choice not in choices:
print("Invalid choice. Please try again.")
return
print("Computer chose:", computer_choice)
print("You chose:", user_choice)
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'paper' and computer_choice == 'rock') or \
(user_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
else:
print("You lose!")
play_rock_paper_scissors()
Computer chose: rock
You chose: paper
You win!
Mc Question = Answer is C and then other one is D
Homework
import random
# List of 15 students
students = [
"Soumini", "Ben", "Charlie", "Diana", "Ethan",
"Risha", "Shriya", "Hannah", "Gaheera", "Abby",
"Kevin", "Lily", "Michael", "Nina", "Oscar"
]
# Creative team names
teams = {
"Team Phoenix": [],
"Team Kraken": [],
"Team Dragon": []
}
# Shuffle students for random distribution
random.shuffle(students)
# Assign students to teams in round-robin fashion
team_names = list(teams.keys())
for i, student in enumerate(students):
team = team_names[i % len(team_names)] # Cycle through team names
teams[team].append(student)
# Print the team assignments
for team, members in teams.items():
print(f"\n{team}:")
for member in members:
print(f" - {member}")
Team Phoenix:
- Diana
- Lily
- Abby
- Nina
- Michael
Team Kraken:
- Charlie
- Ben
- Shriya
- Kevin
- Oscar
Team Dragon:
- Ethan
- Hannah
- Gaheera
- Soumini
- Risha
Homework 2
import random
# Possible weather conditions
weather_types = ["Sunny", "Cloudy", "Rainy"]
# Generate and print a random weather forecast for 7 days
print("7-Day Weather Forecast:\n")
for day in range(1, 8):
weather = random.choice(weather_types)
print(f"Day {day}: {weather}")
7-Day Weather Forecast:
Day 1: Cloudy
Day 2: Sunny
Day 3: Rainy
Day 4: Rainy
Day 5: Rainy
Day 6: Sunny
Day 7: Rainy
Homework 3
import random
# Simulate 5 customers
num_customers = 5
service_times = []
# Generate random service times (1 to 5 minutes) for each customer
for i in range(1, num_customers + 1):
time = random.randint(1, 5)
service_times.append(time)
print(f"Customer {i} will take {time} minute(s) to be served.")
# Calculate total service time
total_time = sum(service_times)
print(f"\nTotal time to serve all {num_customers} customers: {total_time} minute(s)")
Customer 1 will take 4 minute(s) to be served.
Customer 2 will take 3 minute(s) to be served.
Customer 3 will take 3 minute(s) to be served.
Customer 4 will take 5 minute(s) to be served.
Customer 5 will take 5 minute(s) to be served.
Total time to serve all 5 customers: 20 minute(s)