Squash repeats
Inputs
values is a list of 0 to 16 whole numbers, each from 0 to 4.
Your tasks
- Create an empty list
squashed. - Use a loop over
values. Add a number tosquashedonly when it differs from the last number already insquashed. - Complete
squashRepeatsso it returnssquashed. Return it, do not print it.
Example
With values = [1, 1, 2, 2, 2, 1, 3, 3], squashRepeats returns [1, 2, 1, 3].
Rules
- Only neighbours are compared. The same number can appear again later.
Test data
TEST DATA 1: values is [1, 1, 2, 2, 2, 1, 3, 3]
TEST DATA 2: values is []
Practice focus
Put an if inside the loop to keep, skip or change items. Filtering, mapping and comparing neighbours show up in nearly every array question.