The same coins?
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- sorting
- booleans
Problem
Mateo and Sofía emptied their pockets onto the table. Each of them wrote down, in a list, the value of every coin they were carrying.
Write a function that takes the two lists and returns true if the two of them carry exactly the same coins, no matter what order they wrote them in. With 10, 5, 5 and 1 on one side, and 5, 1, 10 and 5 on the other, you return true: they are the same coins, only shuffled.
The rules: repeats count. With 10, 10 and 5 against 10, 5 and 5 you return false, even though the same values show up in both lists: one carries two 10 coins and the other one carries a single one. If one list has more coins than the other, they are not equal either. And two empty lists are equal: neither one carries anything.
You can count how many times each coin shows up, but there is a shorter road: sort both lists and compare them position by position. If they are the same coins, once sorted they end up identical.
Examples
The same ones, shuffled
[10, 5, 5, 1], [5, 1, 10, 5] → true
Same values, different counts
[10, 10, 5], [10, 5, 5] → false
One list is missing a coin
[10, 5, 5, 1], [10, 5, 1] → false
Nothing alike
[10, 20], [1, 2, 5] → false
A single coin, and it matches
[20], [20] → true
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def same_items(mine, yours):
passJavaScript
function sameItems(mine, yours) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.