GithubHelp home page GithubHelp logo

12coins-3weighings's Introduction

12 coins, 3 weighings

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.

Installation

Run npm install or yarn install to install required dependencies (Jest test framework).

Scripts:

  • yarn start or npm start will run main algorithm tests
  • yarn dev or npm dev will run experimenting.js supposed to use at algorithm development phase
  • yarn test of npm test runs test of the built-in weight balancing helper

The task

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 others
  • balance - 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 coins
  • set1indices - indices of coins to put on the left plate
  • set2indices - 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.

Final tests

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.

Hints

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.

12coins-3weighings's People

Contributors

dependabot[bot] avatar oleksiyrudenko avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.