Skip to the content.

Random Algorithms

hacks

DevOps

Random Algorithms

Popcorn Hack #2

import random

activities = ['Go for a walk', 'Read a book', 'Call a friend', 'Try cooking', 'Watch a show', 'Take a nap', 'Go for a run', 'Write in a journal', 'Meditate', 'Listen to music', 'Procrastinate your homework']

random_activity = random.choice(activities)

print(f"Today’s random activity: {random_activity}")

Popcorn Hack #3

import random
hosts = ['Gavin', 'Johan', 'Elliot', 'Jacob']
activities = ['dancing', 'games', 'snack center', 'take a selfie', 'karaoke']

random.shuffle(activities)

for i in range(len(hosts)):
    print(f"{hosts[i]} will be monitoring {activities[i]}!")
Gavin will be monitoring karaoke!
Johan will be monitoring dancing!
Elliot will be monitoring games!
Jacob will be monitoring snack center!

Simulation Games

Popcorn Hack 1

import random

def spinner():
    return random.randint(1, 12)

spin = spinner()
print("Number:", spin)
Number: 12

Popcorn Hack 2

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: scissors
You chose: scissors
It's a tie!

Homework

Homework Hack 1

import random

def assign_teams():
    students = [
        "Joe", "Joey", "Joseph", "JoJo", "Big Joe",
        "Lil Joe", "Average Joe", "Cup of Joe", "J-Dawg", "Joestar",
        "Joebama", "Joefrey", "Joezilla", "Joe Mama", "Jumpin' Joe"
    ]

    teams = ["Team Jnumber1", "Team Jnumber2", "Team Jnumber3"]
    assignments = {}

    for student in students:
        team = random.choice(teams)
        assignments[student] = team

    # Print results
    print("Student Team Assignments:\n")
    for student, team in assignments.items():
        print(f"{student}{team}")

assign_teams()
Student Team Assignments:

Joe → Team Jnumber3
Joey → Team Jnumber2
Joseph → Team Jnumber3
JoJo → Team Jnumber3
Big Joe → Team Jnumber1
Lil Joe → Team Jnumber3
Average Joe → Team Jnumber3
Cup of Joe → Team Jnumber1
J-Dawg → Team Jnumber3
Joestar → Team Jnumber2
Joebama → Team Jnumber2
Joefrey → Team Jnumber1
Joezilla → Team Jnumber1
Joe Mama → Team Jnumber1
Jumpin' Joe → Team Jnumber2

Homework Hack 2

import random

def simulate_weather():
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    weather_options = ["Sunny", "Cloudy", "Rainy"]

    print("7-Day Weather Forecast:\n")
    for day in days:
        forecast = random.choice(weather_options)
        print(f"{day}: {forecast}")

simulate_weather()
7-Day Weather Forecast:

Monday: Sunny
Tuesday: Cloudy
Wednesday: Rainy
Thursday: Sunny
Friday: Sunny
Saturday: Rainy
Sunday: Cloudy

Homework Hack 3

import random

def coffee_shop_queue():
    customers = ["Joe", "Moe", "Foe", "Bro", "Po"]
    total_time = 0

    print("Coffee Shop Queue Simulation:\n")

    for customer in customers:
        service_time = random.randint(1, 5)
        total_time += service_time
        print(f"{customer} was served in {service_time} minutes.")

    print(f"\nTotal time to serve all customers: {total_time} minutes")

coffee_shop_queue()
Coffee Shop Queue Simulation:

Joe was served in 5 minutes.
Moe was served in 3 minutes.
Foe was served in 2 minutes.
Bro was served in 5 minutes.
Po was served in 3 minutes.

Total time to serve all customers: 18 minutes