From de7b441fe4ebd88fc19abbd6542cb8129f735ced Mon Sep 17 00:00:00 2001 From: Aleksander Sadikov Date: Sun, 22 May 2016 20:37:28 +0200 Subject: Denotational semantics, part #1: tests and translations added. --- .../prog_listswap_3/common.py | 36 +++++++++++++++++++++ .../denotational_semantics/prog_listswap_3/en.py | 2 ++ .../denotational_semantics/prog_listswap_3/sl.py | 37 ++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 prolog/problems/denotational_semantics/prog_listswap_3/sl.py (limited to 'prolog/problems/denotational_semantics/prog_listswap_3') diff --git a/prolog/problems/denotational_semantics/prog_listswap_3/common.py b/prolog/problems/denotational_semantics/prog_listswap_3/common.py index 800717e..f8238c6 100644 --- a/prolog/problems/denotational_semantics/prog_listswap_3/common.py +++ b/prolog/problems/denotational_semantics/prog_listswap_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 = 174 number = 20 visible = True @@ -48,3 +55,32 @@ instr((R0,C0)-->(R,C0)) --> [swap], { swap(R0, C0, R) }. ''' + +test_cases = [ + ('findall(W, (W = [_], prog_listswap(_, W, [])), Words), Words == []', + [{}]), + ('prog_listswap([1,2,3,4]-->Out, [begin,right,right,swap,end], []), Out == [1,3,2,4]', + [{}]), + ('prog_listswap([a,b,c,d,e]-->Out, [begin,right,right,swap,left,swap,end], []), Out == [c,a,b,d,e]', + [{}]), +] + +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/denotational_semantics/prog_listswap_3/en.py b/prolog/problems/denotational_semantics/prog_listswap_3/en.py index e275335..d357e3e 100644 --- a/prolog/problems/denotational_semantics/prog_listswap_3/en.py +++ b/prolog/problems/denotational_semantics/prog_listswap_3/en.py @@ -33,6 +33,8 @@ Helper predicates (already defined): swapping the Ith element with its left neighbor in List + +

This exercise has already been solved for you. Check how it works in the console.

''' hint = {} diff --git a/prolog/problems/denotational_semantics/prog_listswap_3/sl.py b/prolog/problems/denotational_semantics/prog_listswap_3/sl.py new file mode 100644 index 0000000..4d009a7 --- /dev/null +++ b/prolog/problems/denotational_semantics/prog_listswap_3/sl.py @@ -0,0 +1,37 @@ +name = 'prog_listswap/3' +slug = 'jezik za rokovanje s seznami s pomenom' + +description = '''\ +

+Napiši DCG za rokovanje s seznami. Prvi simbol vsake besede je [begin], temu pa sledi +poljubno zaporedje "ukazov" iz nabora {left, right, swap}, +na koncu pa je simbol [end]. Začetni nekončni simbol naj se imenuje prog_listswap. +

+ +

+Pomen besede (programa) v tem jeziku ima obliko In-->Out in pomeni preslikavo +med vhodnim seznamom In in izhodnim seznamom Out. Notranje stanje +gramatike, hranjeno v argumentih nekončnih simbolov, poleg trenutnega stanja seznama vodi tudi +pozicijo kurzorja s katerim rokujemo s seznamom. Ukaza left in right +kurzor premakneta za eno mesto v ustrezno smer, ukaz swap pa zamenja mesti elementov +med katerima stoji kurzor. Če je kurzor na začetku seznama, ukaz swap ne uspe. +

+ +
+?- prog_listswap([1,2,3,4]-->Out, [begin,right,swap,end], []).
+  Out = [2,1,3,4].
+
+ +

+Pomožni predikat (že definiran): +

+ + +

Ta naloga je že rešena. Preveri kako deluje v konzoli.

+''' + +hint = {} -- cgit v1.2.1