There are 12 coins. One is fake and its weight is different from that of legit coins.
After 3 weighings tell which coin is fake.
Run npm install
or yarn install
to install
required dependencies (Jest test framework).
Scripts:
yarn start
ornpm start
will run main algorithm testsyarn dev
ornpm dev
will runexperimenting.js
supposed to use at algorithm development phaseyarn test
ofnpm test
runs test of the built-in weight balancing helper
Your task is to write the code that would identify the fake coin among 12 given.
Add some meat to findFake
function in ./find-fake.js
.
The function parameters are:
array
- an array of coins weight, exactly 12 entries, all values are the same with exception for one which may be greater or lesser than othersbalance
- a function to weigh the coins
Your findFake
function is expected to return an array of two
items where first item is a fake coin index in the
input array and the second item is fake coin's weight/value.
So, findFake([21,21,21,21,21,21,29,21,21,21,21,21], balanceMaker())
would return [6,29]
representing a fake coin where
6
is an index of fake coin and 29
is its weight.
Use balance
function inside your findFake
to make weighings.
You may use your own balancing function but final tests will employ
the one provided by the author. It also calculates
number of its own invocations for you
(remember, 3 invocations is a maximum).
The balance
function takes 3 arguments that are:
array
- a source array representing 12 coinsset1indices
- indices of coins to put on the left plateset2indices
- indices of coins to put on the right plate
The function returns 0
if the weight on both plates is equal,
a value lesser than 0
if coins on the left are lighter, and a value greater than 0
if coins on the left are heavier.
For example balance([2,5,2,2,2,2,2,2,2,2,2,2], [0,1], [7,8])
would return 3
since (2+5) - (2+2) == 3
, which means that
there is a heavier coin on the left plate.
Launch your experiments from ./experimenting.js
.
This module imports what is required and also
demonstrates a pattern of findFake
invocation
using a balance function maker.
Run yarn dev
or npm dev
to conduct your experiments.
Once you feel done run yarn start
or npm start
to have a complete test of your algorithm.
There gonna be 24 tests. All must pass for a good algorithm.
The only thing it cannot test is when you use your own method of weighing that doesn't restrict the number of weighings.
Consider addressing to the hints below only after a couple of attempts to solve the problem.
Hint 1. Switch plates
Change of balance may help. So consider moving coins
between the plates. For example,
if 1 + 2 + 3 < 4 + 5 + 6
and 1 + 2 + 4 > 3 + 5
then the fake coin is either 3
or 4
'cause moving them
changes the balance.
Hint 2. Exclude
If 1 + 2 + 3 < 4 + 5 + 6
and 1 + 2 == 5 + 6
then the fake coin is obviously either 3
or 4
.
Hint 3. Mix approaches
Employing both approaches above in a single weighing can save you a weighing attempt.