summaryrefslogtreecommitdiff
path: root/prolog/problems/clp_fd/tobase_3/common.py
blob: eabcad25441f2c7645f4387bb7d8545b37637922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from operator import itemgetter
import prolog.engine
import server.problems

id = 150
number = 70
visible = True
facts = None

solution = '''\
tobase(0, _, 0) :- !.
tobase(N, B, Nb) :-
  B in 2..10,
  indomain(B),
  N #= N1 * B + Rem,
  Rem #>= 0, Rem #< B,
  Nb #= Nb1 * 10 + Rem,
  tobase(N1, B, Nb1).'''

test_cases = [
    ('tobase(214, 2, X)',
        [{'X': '11010110'}]),
    ('tobase(321, 7, X)',
        [{'X': '636'}]),
    ('tobase(X, 8, 777)',
        [{'X': '511'}]),
    ('tobase(X, 8, 132)',
        [{'X': '90'}]),
    ('tobase(653, X, 10103)',
        [{'X': '5'}]),
    ('tobase(123, X, 146)',
        [{'X': '9'}]),
    ('setof(B/N, tobase(123, B, N), X)',
        [{'X': '[2/1111011, 3/11120, 4/1323, 5/443, 6/323, 7/234, 8/173, 9/146, 10/123]'}]),
]

def test(code, aux_code):
    n_correct = 0
    engine_id = None
    try:
        engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
        if engine_id is not None and 'error' not in map(itemgetter(0), output):
            # Engine successfully created, and no syntax error in program.
            for query, answers in test_cases:
                if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
                    n_correct += 1
    except socket.timeout:
        pass
    finally:
        if engine_id:
            prolog.engine.destroy(engine_id)

    hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
    return n_correct, len(test_cases), hints

def hint(code, aux_code):
    # TODO
    return []