Stáhnout: 6.2_11.py   Zobrazit: jednoduše   6.2_11.pl

#!/usr/bin/env python
# encoding=utf-8 (pep 0263)

# je zapotrebi nainstaloval balicek python-constraint
# <https://github.com/python-constraint/python-constraint>
from constraint import Problem, AllDifferentConstraint

problem = Problem()
domain = range(10)
variables = ("S", "E", "N", "D", "M", "O", "R", "Y")
for name in variables:
    problem.addVariable(name, domain)
problem.addConstraint(lambda s: s > 0, ("S"))
problem.addConstraint(lambda m: m > 0, ("M"))
problem.addConstraint(AllDifferentConstraint())
problem.addConstraint(lambda s, e, n, d, m, o, r, y: \
                      1000 * s + 100 * e + 10 * n + d +
                      1000 * m + 100 * o + 10 * r + e ==
                      10000 * m + 1000 * o + 100 * n + 10 * e + y, variables)

# demonstracni vypis
if __name__ == "__main__":
    print("CLP - Algebrogram\n")

    print("  S E N D")
    print("+ M O R E")
    print("---------")
    print("M O N E Y\n")

    print("Vysledek volani problem.getSolutions():")
    solution = problem.getSolutions()[0]
    print(" S = %s, E = %s, N = %s, D = %s, M = %s, O = %s, R = %s, Y = %s" % \
            tuple((solution[x] for x in variables)))

% nacteni:
/* ['6.2_11.pl']. */

:- retractall(start/0).

:- use_module(library(clpfd)). % clpq , clpr

moremoney([S,E,N,D,M,O,R,Y],Type) :- [S,E,N,D,M,O,R,Y] ins 0..9,
                                      S #> 0, M #> 0,
                                      all_distinct([S,E,N,D,M,O,R,Y]),
                                      sum(S,E,N,D,M,O,R,Y),
                                      labeling(Type, [S,E,N,D,M,O,R,Y]).
                                      
sum(S,E,N,D,M,O,R,Y):-              1000*S + 100*E + 10*N + D
                                  + 1000*M + 100*O + 10*R + E
                       #= 10000*M + 1000*O + 100*N + 10*E + Y.

:- dynamic
       start/0.

start:- 
    write('CLP - Algebrogram'),nl,nl,
    write('  S E N D'),nl,
    write('+ M O R E'),nl,
    write('----------'),nl,
    write('M O N E Y'),nl,nl,
    write('Vysledek dotazu "moremoney([S,E,N,D,M,O,R,Y],[])":'),nl,
    moremoney([S,E,N,D,M,O,R,Y],[]),
     write(' S = '),write(S),write(', E = '),write(E),write(', N = '),write(N),
    write(', D = '),write(D),write(', M = '),write(M),write(', O = '),write(O),
    write(', R = '),write(R),write(', Y = '),write(Y),nl.
    
?-start.

 Stáhnout: 6.2_11.py   Zobrazit: jednoduše   6.2_11.pl