Demo entry 2241280

Something about it

   

Submitted by anonymous on Jul 17, 2015 at 12:57
Language: Python. Code size: 1.3 kB.

import itertools
import timeit
import re

def main():
    start_time = timeit.default_timer()

    #expression = 'ALFA+BETA+GAMA==DELTA'
    expression = '4*x+2z==3*yz-x*3'

    letters = list(set([e for e in expression if e.isalpha()]))
    solutions = list(formula_daemon(expression, letters))

    print('\n'.join(solutions))
    elapsed = timeit.default_timer() - start_time
    print('### Done (solutions = %s, time = %.2fs, letters = %d) ###' % (len(solutions), elapsed, len(letters)))

def formula_daemon(expression, letters):
    for c in itertools.permutations(range(10), len(letters)):
        bench = expression
        for l in letters:
            bench = bench.replace(l, str(c[letters.index(l)]))
        try:
            if eval(re.sub(r'\b0+(?!\b)', '', bench)):
                yield bench
        except ZeroDivisionError:
            pass

def min_formula_calc():
    #expression = 'ALFA+BETA+GAMA==DELTA'
    expression = '4*x+2z==3*yz-x*3'

    letters = list(set([e for e in expression if e.isalpha()]))

    for c in itertools.permutations(range(10), len(letters)):
        bench = expression
        for l in letters:
            bench = bench.replace(l, str(c[letters.index(l)]))
            if eval(re.sub(r'\b0+(?!\b)', '', bench)):
                print(bench)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).