Beta

Find the Counterfeit

Description:

The Plot

The king is collecting taxes from his people, and he is receiving his payment in gold coins. He has a large bag of money, but he suspects that he might have been given a counterfeit coin! He turns to you for help. He needs to find if there are any fake coins, and if so, which coin is the fake one. However, the only tool you have at your disposal is a simple balancing scale. The king knows that if there is a fake coin, it will weigh a different amount than all of the other coins.

Your Code

Create a funciton find_counterfeit which takes a list of coins and returns the index of the counterfeit coin. If there are no fake coins, then it returns None. There will always be at least three coins in the input list, and there will be either one or no fake coins.

The Coins

A new class coin has already been created for you. Every coin has a weight attribute, but you can't directly access the value. The only way to determine a coin's weight is to use the pre-defined function balanced, which takes two inputs. Each input can either be a single coin or a list of coins. balanced returns True or False depending on if its two inputs have the same weight.

For example:

coins = [coin(1), coin(1), coin(1), coin(2), coin(1)]
    # The coin in position 3 has a different weight, so it is counterfeit.

balanced(coins[0], coins[1])
    # Returns True: The coins have the same weight.

balanced([coins[0], coins[1]], [coins[2], coins[4]])
    # Returns True: The coins 0 and 1 combined weigh the same as coins 2 and 4 combined.

balanced(coins[2], coins[3])
    # Returns False: The coins have different weights.

find_counterfeit(coins)
    # Returns 3: The coin in position 3 is counterfeit.
Logic
Lists
Fundamentals

More By Author:

Check out these other kata created by FixedGrey

Stats:

CreatedFeb 3, 2017
PublishedFeb 3, 2017
Warriors Trained138
Total Skips21
Total Code Submissions263
Total Times Completed58
Python Completions58
Total Stars5
% of votes with a positive feedback rating82% of 33
Total "Very Satisfied" Votes23
Total "Somewhat Satisfied" Votes8
Total "Not Satisfied" Votes2
Total Rank Assessments34
Average Assessed Rank
7 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • FixedGrey Avatar
Ad