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.
Similar Kata:
Stats:
Created | Feb 3, 2017 |
Published | Feb 3, 2017 |
Warriors Trained | 138 |
Total Skips | 21 |
Total Code Submissions | 263 |
Total Times Completed | 58 |
Python Completions | 58 |
Total Stars | 5 |
% of votes with a positive feedback rating | 82% of 33 |
Total "Very Satisfied" Votes | 23 |
Total "Somewhat Satisfied" Votes | 8 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 34 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 8 kyu |