diff options
author | Aleksander Sadikov <aleksander.sadikov@fri.uni-lj.si> | 2016-05-22 20:37:28 +0200 |
---|---|---|
committer | Aleksander Sadikov <aleksander.sadikov@fri.uni-lj.si> | 2016-05-22 20:37:28 +0200 |
commit | de7b441fe4ebd88fc19abbd6542cb8129f735ced (patch) | |
tree | 9c409cc281e5a095e468f20d9a33b1acad3ed5e7 /prolog/problems/denotational_semantics/prog_listswap_2 | |
parent | 52a65d75cd7adc7983d4e6c45709b7571497ca9a (diff) |
Denotational semantics, part #1: tests and translations added.
Diffstat (limited to 'prolog/problems/denotational_semantics/prog_listswap_2')
3 files changed, 60 insertions, 2 deletions
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 <code>[begin]</code>, followed by any sequence of "instruction" symbols from the set {<code>left</code>, <code>right</code>, <code>swap</code>}, and finally -<code>[end]</code>. The starting symbol should be named +the symbol <code>[end]</code>. The starting symbol should be named <code>prog_listswap</code>. </p> +<p>This exercise has already been solved for you. Check how it works in the console.</p> + <p> -Example words: <code>[begin,right,swap,end]</code>, <code>[begin,right,left,end]</code>. +Example words: <code>[begin,right,swap,end]</code>, <code>[begin,right,right,swap,left,swap,end]</code>. </p> ''' 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 = '''\ +<p> +Napiši DCG za rokovanje s seznami. Prvi simbol vsake besede je <code>[begin]</code>, temu pa sledi +poljubno zaporedje "ukazov" iz nabora {<code>left</code>, <code>right</code>, <code>swap</code>}, +na koncu pa je simbol <code>[end]</code>. Začetni nekončni simbol naj se imenuje <code>prog_listswap</code>. +</p> + +<p>Ta naloga je že rešena. Preveri kako deluje v konzoli.</p> + +<p> +Primeri veljavnih besed: <code>[begin,right,swap,end]</code>, <code>[begin,right,right,swap,left,swap,end]</code>. +</p> +''' + +hint = {} |