Get a printable PDF version of this activity!

Here are some examples of level 3 or level 2 solutions to problems. For each example, can you find a pattern and rewrite it more efficiently?

A. Falak is writing pseudocode for a tic-tac-toe game.

First player places an X
Second player places an O
First player places an X
Second player places an O
First player places an X
Second player places an O
First player places an X
Second player places an O
First player places an X
But stop earlier if someone gets three in a row

B. Leo is making lunch for his three siblings. He wrote down the instructions like this:

  1. Make Jo a sandwich.
  2. Put carrots in a bag for Jo.
  3. Put Jo’s sandwich in their lunch bag.
  4. Put Jo’s carrots in their lunch bag.
  5. Put a juice box in Jo’s lunch bag.
  6. Make Shauna a sandwich.
  7. Put carrots in a bag for Shauna.
  8. Put Shauna’s sandwich in her lunch bag.
  9. Put Shauna’s carrots in her lunch bag.
  10. Put a juice box in Shauna’s lunch bag.
  11. Make Luis a sandwich.
  12. Put carrots in a bag for Luis.
  13. Put Luis’ sandwich in his lunch bag.
  14. Put Luis’ carrots in his lunch bag.
  15. Put a juice box in Luis’ lunch bag.

C. I’m trying to explain to my friend how to draw a flower.

  • Draw a circle for the center.
  • Draw a long oval sticking out the right side of the circle.
  • Draw a long oval sticking out the top right of the circle.
  • Draw a long oval sticking out the top of the circle.
  • Draw a long oval sticking out the top left of the circle.
  • Draw a long oval sticking out the left side of the circle.
  • Draw a long oval sticking out the bottom left of the circle.
  • Draw a long oval sticking out the bottom of the circle.
  • Draw a long oval sticking out the bottom right of the circle.
  • Draw a straight line down from the bottom.
  • Draw a diagonal-up football shape partway up the line.

D.

⋙ print('*****')
⋙ print('****')
⋙ print('***')
⋙ print('**')
⋙ print('*')

E.

⋙ if animal == 'gorilla':
⋙   ape = 'yes'
⋙ elif animal == 'chimpanzee':
⋙   ape = 'yes'
⋙ elif animal == 'orangutan':
⋙   ape = 'yes'
⋙ elif animal == 'gibbon':
⋙   ape = 'yes'
⋙ elif animal == 'bonobo':
⋙   ape = 'yes'
⋙ else:
⋙   ape = 'no'

F.

⋙ vowel1 = 'A'
⋙ vowel2 = 'E'
⋙ vowel3 = 'I'
⋙ vowel4 = 'O'
⋙ vowel5 = 'U'
⋙ if letter == vowel1 or letter == vowel2 or letter == vowel3 or
letter == vowel4 or letter == vowel5 or letter == ' ':
⋙   print(letter)

G.

⋙ dieRoll = random.randint(1, 6)
⋙ score = dieRoll
⋙ dieRoll = random.randint(1, 6)
⋙ score += dieRoll
⋙ dieRoll = random.randint(1, 6)
⋙ score += dieRoll
⋙ if score > 12:
⋙   print('You win!')