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_2/common.py | 38 ++++++++++++++++++++++ .../denotational_semantics/prog_listswap_2/en.py | 6 ++-- .../denotational_semantics/prog_listswap_2/sl.py | 18 ++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 prolog/problems/denotational_semantics/prog_listswap_2/sl.py (limited to 'prolog/problems/denotational_semantics/prog_listswap_2') diff --git a/prolog/problems/denotational_semantics/prog_listswap_2/common.py b/prolog/problems/denotational_semantics/prog_listswap_2/common.py index b3da01f..e4dd975 100644 --- a/prolog/problems/denotational_semantics/prog_listswap_2/common.py +++ b/prolog/problems/denotational_semantics/prog_listswap_2/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 = 175 number = 10 visible = True @@ -25,3 +32,34 @@ instr --> [left]. instr --> [right]. instr --> [swap]. ''' + +test_cases = [ + ('findall(W, (W = [_], prog_listswap(W, [])), Words), Words == []', + [{}]), + ('setof(W, [A,B,C,D]^(W = [A,B,C,D], prog_listswap(W, [])), Words), length(Words, 9)', + [{}]), + ('prog_listswap([begin,right,right,swap,end], [])', + [{}]), + ('prog_listswap([begin,right,right,swap,left,swap,end], [])', + [{}]), +] + +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_2/en.py b/prolog/problems/denotational_semantics/prog_listswap_2/en.py index 89907bd..7db473b 100644 --- a/prolog/problems/denotational_semantics/prog_listswap_2/en.py +++ b/prolog/problems/denotational_semantics/prog_listswap_2/en.py @@ -6,12 +6,14 @@ description = '''\ Write a DCG for manipulating list elements. The first symbol in every word is [begin], followed by any sequence of "instruction" symbols from the set {left, right, swap}, and finally -[end]. The starting symbol should be named +the symbol [end]. The starting symbol should be named prog_listswap.

+

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

+

-Example words: [begin,right,swap,end], [begin,right,left,end]. +Example words: [begin,right,swap,end], [begin,right,right,swap,left,swap,end].

''' diff --git a/prolog/problems/denotational_semantics/prog_listswap_2/sl.py b/prolog/problems/denotational_semantics/prog_listswap_2/sl.py new file mode 100644 index 0000000..0ed8e4d --- /dev/null +++ b/prolog/problems/denotational_semantics/prog_listswap_2/sl.py @@ -0,0 +1,18 @@ +name = 'prog_listswap/2' +slug = 'jezik za rokovanje s seznami' + +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. +

+ +

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

+ +

+Primeri veljavnih besed: [begin,right,swap,end], [begin,right,right,swap,left,swap,end]. +

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