Homework Hacks: Algorithms & Debugging


Hack 1: Create Your Own Algorithm

Activity chosen: Making a pizza from scratch

Algorithm:

  1. Gather all ingredients and equipment - Collect flour, yeast, salt, water, olive oil, tomato sauce, mozzarella cheese, and whatever toppings you want. You’ll also need mixing bowls, measuring cups, a rolling pin, and a baking sheet or pizza stone.

  2. Prepare the pizza dough - In a large bowl, combine 2¼ cups of warm water (about 110°F), 1 tablespoon of sugar, and 2¼ teaspoons of active dry yeast. Let it sit for around 5 minutes until it becomes foamy.

  3. Mix and knead the dough - Add 3 cups of flour, 2 tablespoons of olive oil, and 2 teaspoons of salt to the yeast mixture. Stir until everything is combined, then knead it on a floured surface for about 8-10 minutes until the dough is smooth and elastic.

  4. Let the dough rise - Place the dough in a greased bowl, cover it with a damp towel, and let it rise in a warm place for 1-2 hours. It should double in size during this time.

  5. Preheat the oven and prepare toppings - Set the oven to 475°F (245°C). While the oven is heating up, shred the mozzarella cheese, slice any vegetables, and prepare whatever meat toppings you’re planning to use.

  6. Shape the pizza base - Punch down the risen dough to release the air bubbles. On a floured surface, stretch or roll the dough into a 12-14 inch circle. Try to make the edges a bit thicker to form the crust.

  7. Add sauce and toppings - Transfer the dough to a greased baking sheet or pizza stone. Spread about ½ to ¾ cup of tomato sauce evenly across the dough, leaving roughly a 1-inch border around the edges. Add the shredded mozzarella cheese and then arrange your toppings on top.

  8. Bake the pizza - Place the pizza in the preheated oven and bake for 12-15 minutes. The crust should turn golden brown and the cheese should be bubbly with some light browning.

  9. Cool and serve - Carefully remove the pizza from the oven. Let it cool for 2-3 minutes, then slice it into 8 pieces using a pizza cutter or sharp knife and serve.


Hack 2: Identify the Bug

Corrected Algorithm: Send an Email

  1. Open email application - Launch your email program or go to your email website in a web browser.

  2. Log into your account - Enter your username and password to access your email account.

  3. Compose a new email - Click the “Compose,” “New Message,” or “+” button to start creating a new email.

  4. Enter recipient’s email address - Type the recipient’s email address in the “To:” field.

  5. Write subject line - Add a brief subject line in the “Subject:” field that summarizes what the email is about.

  6. Type the message - Write your email message in the main text area. Include all the information you need to communicate.

  7. Click “Send” - Review the email to make sure everything looks correct, then click the “Send” button to deliver the message.

Explanation of fixes: The original algorithm had the steps in the wrong order. You need to open the email application before you can log in, and you have to log in before you can compose a new message. The recipient information, subject line, and message body all need to be filled in before you can send the email. Clicking “Send” should always be the last step.


Hack 3: Code the Algorithm

```python

def calculate_grade(score1, score2, score3):
"""Calculate letter grade from three test scores"""
total = score1 + score2 + score3
average = total / 3

if average >= 90:
    grade = 'A'
elif average >= 80:
    grade = 'B'
elif average >= 70:
    grade = 'C'
elif average >= 60:
    grade = 'D'
else:
    grade = 'F'

return grade

print("Test 1:", calculate_grade(95, 92, 88))
print("Test 2:", calculate_grade(85, 80, 82))
print("Test 3:", calculate_grade(75, 70, 72))
print("Test 4:", calculate_grade(65, 60, 62))
print("Test 5:", calculate_grade(55, 50, 52))

Test 1: A
Test 2: B
Test 3: C
Test 4: D
Test 5: F