diff options
Diffstat (limited to 'prolog/problems/algol/algol_3')
-rw-r--r-- | prolog/problems/algol/algol_3/common.py | 36 | ||||
-rw-r--r-- | prolog/problems/algol/algol_3/sl.py | 32 |
2 files changed, 67 insertions, 1 deletions
diff --git a/prolog/problems/algol/algol_3/common.py b/prolog/problems/algol/algol_3/common.py index ff38469..e997bde 100644 --- a/prolog/problems/algol/algol_3/common.py +++ b/prolog/problems/algol/algol_3/common.py @@ -1,3 +1,10 @@ +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup + id = 176 number = 10 visible = True @@ -163,5 +170,32 @@ eval(E1 * E2, State, Val) :- !, eval(E1 / E2, State, Val) :- !, eval(E1, State, V1), eval(E2, State, V2), - Val is V1 / V2. + Val is V1 / V2. ''' + +test_cases = [ + ('findall(W, (W = [_], algol(_, W, [])), Words), Words == []', + [{}]), + ('algol(F, [begin,a,:=,a+2,end], []), apply([a=3], Out, F), msort(Out, OutSorted), OutSorted == [a=5,printout=[]]', + [{}]), +] + +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 + diff --git a/prolog/problems/algol/algol_3/sl.py b/prolog/problems/algol/algol_3/sl.py new file mode 100644 index 0000000..6fa9b0b --- /dev/null +++ b/prolog/problems/algol/algol_3/sl.py @@ -0,0 +1,32 @@ +name = 'algol/3' +slug = 'interpreter za mini-algol' + +description = '''\ +<p>DCG za mini-algol.</p> +<pre> +% uporabi (apply) funkcijo nad začetnim stanjem +?- apply([a=2], Out, fun(_In, Out, eval(a+3, _In, Out))). + Out = 5. + +% a := a+b +% b := a-b +% a := a-b +?- _Program = [begin,a,:=,a+b,b,:=,a-b,a,:=,a-b,end], + algol(_F, _Program, []), + apply([a=3,b=5], Output, _F). + Output = [a=5,b=3,printout=[]]. + +% a := 0 +% while a < 10 do +% begin +% print(a) +% a := a+1 +% end +?- _Program = [begin,a,:=,0,while,a,<,10,do,begin,print(a),a,:=,a+1,end,end], + algol(_F, _Program, []), + apply([a=3], Output, _F). + Output = [a=10,printout=[0,1,2,3,4,5,6,7,8,9]]. +</pre> +''' + +hint = {} |