Pattern Recognition Exercises
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:
- Make Jo a sandwich.
- Put carrots in a bag for Jo.
- Put Jo’s sandwich in their lunch bag.
- Put Jo’s carrots in their lunch bag.
- Put a juice box in Jo’s lunch bag.
- Make Shauna a sandwich.
- Put carrots in a bag for Shauna.
- Put Shauna’s sandwich in her lunch bag.
- Put Shauna’s carrots in her lunch bag.
- Put a juice box in Shauna’s lunch bag.
- Make Luis a sandwich.
- Put carrots in a bag for Luis.
- Put Luis’ sandwich in his lunch bag.
- Put Luis’ carrots in his lunch bag.
- 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!')