summaryrefslogtreecommitdiff
path: root/prolog/problems/algol
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/algol')
-rw-r--r--prolog/problems/algol/algol_3/common.py36
-rw-r--r--prolog/problems/algol/algol_3/sl.py32
-rw-r--r--prolog/problems/algol/algol_for_3/common.py42
-rw-r--r--prolog/problems/algol/algol_for_3/sl.py18
-rw-r--r--prolog/problems/algol/algol_if_3/common.py42
-rw-r--r--prolog/problems/algol/algol_if_3/sl.py20
6 files changed, 187 insertions, 3 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 = {}
diff --git a/prolog/problems/algol/algol_for_3/common.py b/prolog/problems/algol/algol_for_3/common.py
index b48610b..38d5d45 100644
--- a/prolog/problems/algol/algol_for_3/common.py
+++ b/prolog/problems/algol/algol_for_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 = 178
number = 30
visible = True
@@ -177,5 +184,38 @@ 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_for(_, W, [])), Words), Words == []',
+ [{}]),
+ ('algol_for(F, [begin,for,a,:=,0,&,a,<,9,&,a,:=,a+1,do,begin,print(a),a,:=,a+1,end,end], []), \
+ apply([a=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=10,printout=[0,2,4,6,8]]',
+ [{}]),
+ ('algol_for(F, [begin,for,a,:=,1,&,a,<,32,&,a,:=,a*2,do,begin,print(a),end,end], []), \
+ apply([a=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=32,printout=[1,2,4,8,16]]',
+ [{}]),
+]
+
+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_for_3/sl.py b/prolog/problems/algol/algol_for_3/sl.py
new file mode 100644
index 0000000..17d91fc
--- /dev/null
+++ b/prolog/problems/algol/algol_for_3/sl.py
@@ -0,0 +1,18 @@
+name = 'algol_for/3'
+slug = 'interpreter za mini-algol s for stavkom'
+
+description = '''\
+<p>Razširi podano gramatiko za jezik mini-algol, da bo le-ta podprla uporabo stavka "for". Primer:</p>
+<pre>
+% for a := 0 & a < 5 & a := a + 1 do
+% begin
+% print(a)
+% end
+?- _Program = [begin,for,a,:=,0,&,a,<,5,&,a,:=,a+1,do,begin,print(a),end,end],
+ algol_for(_F, _Program, []),
+ apply([a=2], Output, _F).
+ Output = [a=5,printout=[0,1,2,3,4]].
+</pre>
+'''
+
+hint = {}
diff --git a/prolog/problems/algol/algol_if_3/common.py b/prolog/problems/algol/algol_if_3/common.py
index 9198831..7e27430 100644
--- a/prolog/problems/algol/algol_if_3/common.py
+++ b/prolog/problems/algol/algol_if_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 = 177
number = 20
visible = True
@@ -170,5 +177,38 @@ 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_if(_, W, [])), Words), Words == []',
+ [{}]),
+ ('algol_if(F, [begin,if,a,<,0,then,a,:=,0-a,else,a,:=,a,end,end], []), \
+ apply([a= -2], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a=2,printout=[]]',
+ [{}]),
+ ('algol_if(F, [begin,if,0,<,a,then,print(a),else,b,:=,a+5,print(b),end,end], []), \
+ apply([a= -2,b=0], Out, F), msort(Out, OutSorted), \
+ OutSorted == [a= -2,b=3,printout=[3]]',
+ [{}]),
+]
+
+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_if_3/sl.py b/prolog/problems/algol/algol_if_3/sl.py
new file mode 100644
index 0000000..a75bcef
--- /dev/null
+++ b/prolog/problems/algol/algol_if_3/sl.py
@@ -0,0 +1,20 @@
+name = 'algol_if/3'
+slug = 'interpreter za mini-algol z if stavkom'
+
+description = '''\
+<p>Razširi podano gramatiko za jezik mini-algol, da bo le-ta podprla uporabo stavka "if-then-else".
+Privzemi, da morata obe veji (then in else) biti prisotni v stavku. Primer:</p>
+<pre>
+% if a < b then
+% print(a)
+% else
+% print(b)
+% end
+?- _Program = [begin,if,a,<,b,then,print(a),else,print(b),end,end],
+ algol_if(_F, _Program, []),
+ apply([a=3,b=5], Output, _F).
+ Output = [a=3,b=5,printout=[3]].
+</pre>
+'''
+
+hint = {}