The fake doubloon
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- indexes
- loops
Problem
"One of these doubloons is fake," Captain Blackwood growls over the chest. The real ones all weigh the same; the fake weighs something else, sometimes more and sometimes less. Nobody tells you what a real one weighs.
Write a function that takes the list of weights, positive integers, and returns the position of the fake doubloon, as an integer. Positions start at 0. The list holds at least three doubloons, and always exactly one that differs from the rest.
The fake can be anywhere, the first spot included: don't assume the first doubloon is a real one.
With [12, 12, 10, 12, 12] you return 2: the real ones weigh 12, and the only one weighing 10 sits at position 2.
Examples
The one from the example
[12, 12, 10, 12, 12] → 2
The fake is last, and heavier
[15, 15, 15, 15, 17] → 4
Only three doubloons
[6, 9, 6] → 1
A long chest
[8, 8, 8, 8, 8, 8, 8, 5, 8, 8] → 7
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def fake_coin(weights):
passJavaScript
function fakeCoin(weights) {
}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.