Random Algorithms Homework and Popcorn hacks
Popcorn Hack #1
A random algorithm is a very complex algorithm taking in many complex inputs and factors to generate a random output.
Popcorn Hack #2
# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['work on projects', 'study for act', 'work on homework', 'fly drones', 'work out', 'play video games']
# 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: study for act
Popcorn Hack #3
# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['weston', 'arnav', 'ahaan', 'manas']
activities = ['dancing', 'singing', 'playing games', 'eating snacks', 'watching movies']
# 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]}!")
weston will be monitoring watching movies!
arnav will be monitoring playing games!
ahaan will be monitoring dancing!
manas will be monitoring eating snacks!
Popcorn Hack #4
import random
def spin_spinner():
return random.randint(1, 10000)
spinner_result = spin_spinner()
print("Spinner landed on:", spinner_result)
Spinner landed on: 459
Popcorn Hack #5
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!
Homework Hack #1
import random
students = [
"Alice", "Ben", "Cara", "David", "Ella",
"Frank", "Grace", "Hugo", "Isla", "Jack",
"Kira", "Liam", "Mona", "Nate", "Olivia"
]
teams = {
"Team Phoenix": [],
"Team Kraken": [],
"Team Griffin": []
}
team_names = list(teams.keys())
for student in students:
team = random.choice(team_names)
teams[team].append(student)
# Print results
for team, members in teams.items():
print(f"\n{team}:")
for member in members:
print(f" - {member}")
Team Phoenix:
- David
- Ella
- Mona
- Nate
Team Kraken:
- Cara
- Frank
- Isla
- Liam
Team Griffin:
- Alice
- Ben
- Grace
- Hugo
- Jack
- Kira
- Olivia
Homework Hack #2
import random
weather_options = ["Sunny", "Cloudy", "Rainy"]
def generate_forecast(days=7):
forecast = []
for day in range(1, days + 1):
weather = random.choice(weather_options)
forecast.append((f"Day {day}", weather))
return forecast
# Generate and print the forecast
weekly_forecast = generate_forecast()
print("7-Day Weather Forecast:")
for day, weather in weekly_forecast:
print(f"{day}: {weather}")
7-Day Weather Forecast:
Day 1: Sunny
Day 2: Sunny
Day 3: Cloudy
Day 4: Rainy
Day 5: Sunny
Day 6: Sunny
Day 7: Rainy
Homework Hack #3
import random
def simulate_coffee_queue(num_customers=5):
total_time = 0
service_times = []
print("Coffee Shop Queue Simulation:\n")
for i in range(1, num_customers + 1):
service_time = random.randint(1, 5)
service_times.append(service_time)
total_time += service_time
print(f"Customer {i} - Service time: {service_time} minutes")
print(f"\nTotal time to serve all customers: {total_time} minutes")
simulate_coffee_queue()
Coffee Shop Queue Simulation:
Customer 1 - Service time: 1 minutes
Customer 2 - Service time: 5 minutes
Customer 3 - Service time: 5 minutes
Customer 4 - Service time: 2 minutes
Customer 5 - Service time: 1 minutes
Total time to serve all customers: 14 minutes