Last active
April 13, 2018 04:42
-
-
Save Neverik/3d0639f66f9ca727d682747de926657a to your computer and use it in GitHub Desktop.
Rebus problem solver (repl.it/repls/SizzlingBisqueIntegers to run)
This file contains hidden or 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
from itertools import permutations | |
from functools import reduce | |
import sys | |
si = "" | |
print("Rebus SolvR by Neverik. Copyright 2018.") | |
print("Enter text or 'exit' to escape.") | |
while True: | |
sinp = input("Enter: ") | |
for i in sinp: | |
if not i.isalnum(): | |
raise ValueError("Bad text") | |
if sinp == "exit": | |
break | |
si += sinp | |
sinp = input("Operator: ") | |
if sinp == "exit": | |
break | |
if sinp in "+-**/=": | |
if sinp == "=": | |
si += "==" | |
else: | |
si += sinp | |
else: | |
raise ValueError("Bad operator") | |
s = "".join([(("{" + i + "}")*(str(i).isalpha()) or str(i)) for i in si]) | |
print("Processing...") | |
keys = [i for i in si if str(i).isalpha()] | |
ns = [i for i in si if str(i).isnumeric()] | |
seps = [i for i in si if not (str(i).isnumeric() or str(i).isalpha())] | |
def r(a, t): | |
p = list(a)[:] | |
try: | |
p.remove(int(t)) | |
except ValueError: | |
pass | |
return p | |
nums = reduce(r, ns, list(range(10))) | |
print("Bruteforcing...") | |
print() | |
solved = False | |
for i in permutations(nums, r=len(keys)): | |
d = {key:t for key,t in zip(keys, i)} | |
n = s.format(**d) | |
try: | |
if eval(n): | |
print("Result found: ", end='') | |
print(n) | |
solved = True | |
break | |
except ZeroDivisionError: | |
continue | |
except SyntaxError: | |
continue | |
if solved: | |
print() | |
print("Thanks for using Rebus SolvR!") | |
rate = input("Leave a rating from 1 to 3: ") | |
try: | |
a = int(rate) | |
except ValueError: | |
print("Sorry, you didn't enter an integer.") | |
else: | |
if a == 42: | |
print("You are the true master.") | |
elif a > 3: | |
print("Thanks a LOT!") | |
elif a == 3: | |
print("Thanks!") | |
elif a == 2: | |
print("Please write a comment at gist.github.com/Neverik/3d0639f66f9ca727d682747de926657a. Let me know what's wrong.") | |
elif a < 2: | |
print("I'm sorry it didn't help. Please write a comment at gist.github.com/Neverik/3d0639f66f9ca727d682747de926657a. Let me know what's wrong.") | |
else: | |
print() | |
print("Sorry, no solution found. Please check that you've entered everything correctly.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awsum.