Challenge
PastOne concept, three rungs. Reaching rung 1 is the goal, so climb as far as you can.
Counting Colours
loops + counting matches
Count the red ones
Write a function called count_red
You get a list of colour words. Return how many of them are "red". An empty list has 0 red ones.
Examples
| Input | Output |
|---|---|
| ["red", "blue", "red"] | 2 |
| [] | 0 |
| ["blue"] | 0 |
| ["red"] | 1 |
There are extra hidden tests, so make it work in general, not just for these.
Count any colour
Write a function called count_color
Now the colour you are looking for is given to you as a second argument. Return how many times it appears in the list. If it never appears, return 0.
Examples
| Input | Output |
|---|---|
| ["red", "blue", "red"], "blue" | 1 |
| ["red", "blue"], "green" | 0 |
| [], "red" | 0 |
| ["cat", "cat", "cat"], "cat" | 3 |
There are extra hidden tests, so make it work in general, not just for these.
The most popular colour
Write a function called most_common_color
Return the colour that appears the most times. If two colours appear the same number of times, return the one that comes first in the list. An empty list returns None.
Examples
| Input | Output |
|---|---|
| ["red", "blue", "red"] | "red" |
| [] | None |
| ["blue"] | "blue" |
| ["red", "blue"] | "red" |
There are extra hidden tests, so make it work in general, not just for these.
Finish Rung 1 and today counts. Rungs 2 and 3 are there if you want to push further.