Created
August 28, 2016 15:28
-
-
Save APIUM/2a19a2824f2392f42d2d2ea4f171541a to your computer and use it in GitHub Desktop.
My solution for the Reddit /r/dailyprogrammer challenge 277 - Fake Coins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
# Reddit Daily Programming Challenge 277 - Fake Coins | |
# The challenge input | |
challenge = """abcd efgh equal | |
abci efjk left | |
abij efgl equal | |
mnopqrs tuvwxyz equal""" | |
# Global variables | |
equal = [] | |
finalEqual = {} | |
possibilities = [] | |
fake = 0 | |
fakeCheck = 0 | |
# Splits the input up into a list of lists | |
coins = challenge.split('\n') | |
for i in range(len(coins)): | |
coins[i] = coins[i].split() | |
def dupeCheck(): | |
# Defines the inner vars as the global ones | |
global equal | |
global finalEqual | |
global possibilities | |
# Checks all inputs for equal ones | |
for i in range(len(coins)): | |
# Upon finding ones puts it in another list called equal | |
if coins[i][2] == 'equal': | |
equal.append(coins[i][0] + coins[i][1]) | |
# For this list find which letters are the same as others | |
for j in range(len(equal)): | |
for k in range(len(equal[j])): | |
# This gives an out of range error at the end so need the try | |
try: | |
for o in range(len(equal)): | |
if equal[j][k] in equal[j+o]: | |
if equal[j][k] not in finalEqual: | |
for l in range(len(equal[j])): | |
finalEqual[equal[j][l]] = equal[j+o] | |
possibilities.append(equal[j][l]) | |
# This just gets rid of duplicates to speed | |
# Up processing | |
possibilities = list(set(possibilities)) | |
except: | |
None | |
for m in range(len(possibilities)): | |
for n in possibilities: | |
if possibilities[m] in finalEqual[n]: | |
if finalEqual[n] not in finalEqual[possibilities[m]]: | |
finalEqual[possibilities[m]] += finalEqual[n] | |
# Removes all duplicate characters in the finalEqual dict strings | |
finalEqual[possibilities[m]] = "".join(set(finalEqual[possibilities[m]])) | |
# This subsitutes all the normal a, b, whatever | |
# for all their possible combinations | |
# For example: a = abcdf | |
def subsitute(): | |
global coins | |
for n in finalEqual: | |
for i in range(len(coins)): | |
for j in range(2): | |
coins[i][j] = coins[i][j].replace(str(n), finalEqual[n], 1) | |
# Gets rid of dublicate characters in the coins list, making it possible | |
# To see by eye which coin is lighter, and much easier to validate the | |
# correct return. | |
def cleanUp(): | |
for i in range(2): | |
for j in range(len(coins)): | |
coins[j][i] = "".join(set(coins[j][i])) | |
print(coins) | |
# The main solve function | |
# Goes through all possibilites and outputs response | |
def solve(): | |
global fake | |
global fakeCheck | |
for i in range(len(coins)): | |
if coins[i][2] == 'left': | |
for x in coins[i][1]: | |
if x not in possibilities: | |
return x + ' is Lighter' | |
if coins[i][2] == 'right': | |
for x in coins[i][0]: | |
if x not in possibilities: | |
return x + ' is Lighter' | |
if coins[i][2] == 'left': | |
if coins[i][0] == coins[i][1]: | |
return 'Data is inconsistent' | |
if coins[i][2] == 'right': | |
if coins[i][0] == coins[i][1]: | |
return 'Data is inconsistent' | |
if coins[i][2] == 'equal': | |
if coins[i][0] == coins[i][1]: | |
# fake is here because it is possible for | |
# It to seem like there are no fake coins just because | |
# One is equal (happens almost every time) | |
# So we must check that there are no other possible | |
# Returns | |
fake = 1 | |
# This is the one that makes sure there are no other | |
# possible returns | |
else: fakeCheck = 1 | |
# The main fuction calls | |
dupeCheck() | |
subsitute() | |
cleanUp() | |
solve() | |
# The final incorrect return check and printing | |
# of result. | |
if fake == 1: | |
if solve() == None: | |
print('No fake coins detected') | |
else: | |
print(solve()) | |
else: | |
print(solve()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment