<s> from simpleai . search import backtrack , CspProblem , convert_to_binary <EOL> variables = ( '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ) <EOL> domains = { <EOL> '<STR_LIT:A>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> '<STR_LIT:B>' : [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:4> ] , <EOL> '<STR_LIT:C>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> } <EOL> def const_different ( variables , values ) : <EOL> return len ( values ) == len ( set ( values ) ) <EOL> def const_one_bigger_other ( variables , values ) : <EOL> return values [ <NUM_LIT:0> ] > values [ <NUM_LIT:1> ] <EOL> def const_one_odd_one_even ( variables , values ) : <EOL> if values [ <NUM_LIT:0> ] % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> return values [ <NUM_LIT:1> ] % <NUM_LIT:2> == <NUM_LIT:1> <EOL> else : <EOL> return values [ <NUM_LIT:1> ] % <NUM_LIT:2> == <NUM_LIT:0> <EOL> def const_not_1 ( variables , values ) : <EOL> return values [ <NUM_LIT:0> ] != <NUM_LIT:1> <EOL> constraints = [ <EOL> ( ( '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ) , const_different ) , <EOL> ( ( '<STR_LIT:A>' , '<STR_LIT:C>' ) , const_one_bigger_other ) , <EOL> ( ( '<STR_LIT:A>' , '<STR_LIT:C>' ) , const_one_odd_one_even ) , <EOL> ( ( '<STR_LIT:A>' , ) , const_not_1 ) <EOL> ] <EOL> variables , domains , constraints = convert_to_binary ( variables , domains , constraints ) <EOL> problem = CspProblem ( variables , domains , constraints ) <EOL> result = backtrack ( problem ) <EOL> print result </s>
