Skip to main content
Post Made Community Wiki by risky mysteries
I forgot the code!
Source Link

The code:

from itertools import count, permutations

OPERATORS = '+-*( )='
VARIABLES = 'abcdefghijklmnopqrstuvwxyz'
NUMERALS = '1234567890'
VALID_CHARACTERS = OPERATORS + VARIABLES + NUMERALS


def is_expression_valid(expression):
    return all(c in VALID_CHARACTERS for c in expression)


def is_expression_consistent(expression, variables):
    for index, value in zip(count(1), variables):
        expression = expression.replace(value, str(index))
    try:
        eval(expression.replace("=", "=="))
    except SyntaxError:
        return False
    return True


def extract_variables(expression):
    variables = {char for char in expression if char in VARIABLES}
    if len(variables) > 10:
        raise ValueError()
    return variables


def get_expression():
    while True:
        expression = input(">>> ")
        if not is_expression_valid(expression):
            print(f"Invalid characters in the expression. Use only from: {VALID_CHARACTERS}")
            continue
        try:
            variables = extract_variables(expression)
        except ValueError:
            print("Expected at most 10 variable/letters!")
            continue
        if not is_expression_consistent(expression, variables):
            print("Invalid syntax!")
            continue
        break
    return expression, variables


def substitute_expression_format(expression):
    return "".join(f"{{{c}}}" if c in VARIABLES else c for c in expression)


def valid_results(expression, variables):
    formatted_expression = substitute_expression_format(expression)
    for permute in permutations(range(10), len(variables)):
        variation = formatted_expression.format(**dict(zip(variables, permute)))
        try:
            if eval(variation.replace("=", "==")):
                yield variation
        except SyntaxError:
            continue


def main():
    while True:
        expression, variables = get_expression()
        for result in valid_results(expression, variables):
            print(result)


if __name__ == "__main__":
    main()

Experimenting with different word combos, I had a great day :)

Experimenting with different word combos, I had a great day :)

The code:

from itertools import count, permutations

OPERATORS = '+-*( )='
VARIABLES = 'abcdefghijklmnopqrstuvwxyz'
NUMERALS = '1234567890'
VALID_CHARACTERS = OPERATORS + VARIABLES + NUMERALS


def is_expression_valid(expression):
    return all(c in VALID_CHARACTERS for c in expression)


def is_expression_consistent(expression, variables):
    for index, value in zip(count(1), variables):
        expression = expression.replace(value, str(index))
    try:
        eval(expression.replace("=", "=="))
    except SyntaxError:
        return False
    return True


def extract_variables(expression):
    variables = {char for char in expression if char in VARIABLES}
    if len(variables) > 10:
        raise ValueError()
    return variables


def get_expression():
    while True:
        expression = input(">>> ")
        if not is_expression_valid(expression):
            print(f"Invalid characters in the expression. Use only from: {VALID_CHARACTERS}")
            continue
        try:
            variables = extract_variables(expression)
        except ValueError:
            print("Expected at most 10 variable/letters!")
            continue
        if not is_expression_consistent(expression, variables):
            print("Invalid syntax!")
            continue
        break
    return expression, variables


def substitute_expression_format(expression):
    return "".join(f"{{{c}}}" if c in VARIABLES else c for c in expression)


def valid_results(expression, variables):
    formatted_expression = substitute_expression_format(expression)
    for permute in permutations(range(10), len(variables)):
        variation = formatted_expression.format(**dict(zip(variables, permute)))
        try:
            if eval(variation.replace("=", "==")):
                yield variation
        except SyntaxError:
            continue


def main():
    while True:
        expression, variables = get_expression()
        for result in valid_results(expression, variables):
            print(result)


if __name__ == "__main__":
    main()

Experimenting with different word combos, I had a great day :)

Source Link

Words to numbers equation solver

With this program, you can type in equations in the form of

cat + d0g = pets
bird - seed = hunger
you * me = happ9 = h3 * she
etc.
etc.

The program will find all possible values for all the letters used that will satisfy the equation. If a = 3, b = 6, c = 0, then abc = 360. Each letter can only have one number assigned to it, so you will be able to use a maximum of 10 different hidden letters.

Here are some examples of word equations and all of their possible number forms:

bread - yeast = stone

>>> bread - yeast = stone
81390 - 53927 = 27463
81690 - 26954 = 54736
92461 - 34657 = 57804
40389 - 23816 = 16573
70159 - 41528 = 28631
60179 - 31728 = 28451
70359 - 23546 = 46813

he * she = magic

>>> he * she = magic
58 * 358 = 20764
67 * 467 = 31289
73 * 573 = 41829
67 * 367 = 24589
59 * 459 = 27081
52 * 752 = 39104

seed + water = plant

>>> seed + water = plant
6771 + 28079 = 34850
6779 + 28071 = 34850
6885 + 13489 = 20374
6889 + 13485 = 20374
1442 + 69543 = 70985
1443 + 69542 = 70985
5668 + 14769 = 20437
5669 + 14768 = 20437
7331 + 42938 = 50269
7338 + 42931 = 50269

puppy - baby = doggy

>>> puppy - baby = doggy
41440 - 8780 = 32660
51550 - 7670 = 43880
31330 - 6560 = 24770
25220 - 8780 = 16440

mom + dad = baby

>>> mom + dad = baby
878 + 434 = 1312
868 + 545 = 1413
848 + 767 = 1615
474 + 838 = 1312
565 + 848 = 1413
747 + 868 = 1615

site + bobble = better

>>> site + bobble = better
8915 + 242235 = 251150
8917 + 262257 = 271174
7816 + 353346 = 361162
2317 + 868857 = 871174

deusov + puzzle = solved

>>> deusov + puzzle = solved
291703 + 416689 = 708392

Experimenting with different word combos, I had a great day :)