diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-04-11 00:10:57 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-04-11 00:10:57 +0200 |
commit | acce0e01cdb2a57cda35e040035dbf1da6b96031 (patch) | |
tree | e5a08e4a45893463e7bacfd82454a1a07fecba69 /prolog/problems | |
parent | 169c995732f3840aae6750479b49a4d0bcd4b198 (diff) |
Prolog: add test cases for the trees group
Diffstat (limited to 'prolog/problems')
27 files changed, 517 insertions, 49 deletions
diff --git a/prolog/problems/trees/deletebt_3/common.py b/prolog/problems/trees/deletebt_3/common.py index 52f9e3f..748fcbf 100644 --- a/prolog/problems/trees/deletebt_3/common.py +++ b/prolog/problems/trees/deletebt_3/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 137 -number = 47 -visible = False +number = 60 +visible = True facts = None solution = '''\ @@ -16,3 +21,44 @@ deleteBT(X, b(L, E, R), b(L1, E, R)) :- deleteBT(X, b(L, E, R), b(L, E, R1)) :- deleteBT(X, R, R1). ''' + +test_cases = [ + ('deleteBT(q, b(nil,q,nil), X), X == nil', + [{}]), + ('''setof(T, deleteBT(2, b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))), T), X), + X == [b(b(nil,4,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))),b(b(b(nil,4,nil),6,nil),1,b(nil,3,b(nil,5,nil)))]''', + [{}]), + ('''setof(T, deleteBT(8, b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), T), X), + X == [b(b(b(nil,7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), + b(b(b(nil,8,nil),7,b(nil,2,nil)),3,b(b(nil,4,nil),8,b(nil,1,nil))), + b(b(b(b(nil,8,nil),7,nil),2,nil),3,b(b(nil,4,nil),8,b(nil,1,nil))), + b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(nil,4,b(nil,1,nil))), + b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),1,nil))]''', + [{}]), + ('''setof(T, deleteBT(2, b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), T), X), + X == [b(b(b(b(nil,8,nil),7,nil),3,nil),8,b(b(nil,4,nil),8,b(nil,1,nil)))]''', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/deletebt_3/en.py b/prolog/problems/trees/deletebt_3/en.py index eedab0a..14f2f1c 100644 --- a/prolog/problems/trees/deletebt_3/en.py +++ b/prolog/problems/trees/deletebt_3/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'deleteBT/3' slug = 'delete an element from a binary tree' diff --git a/prolog/problems/trees/deletebt_3/sl.py b/prolog/problems/trees/deletebt_3/sl.py new file mode 100644 index 0000000..7a1d930 --- /dev/null +++ b/prolog/problems/trees/deletebt_3/sl.py @@ -0,0 +1,13 @@ +name = 'deleteBT/3' +slug = 'izbriši element iz binarnega drevesa' + +description = '''\ +<p><code>deleteBT(X, T, NewT)</code>: binarno drevo <code>NewT</code> dobimo iz <code>T</code> tako, da izbrišemo eno pojavitev elementa <code>X</code>. Če <code>X</code> ni v listu, ga zamenjamo s korenom levega ali desnega poddrevesa. Program naj vrača vse veljavne rešitve.</p> +<pre> +?- deleteBT(1, b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))), T). + T = b(b(nil,4,b(nil,6,nil)),2,b(nil,3,b(nil,5,nil))) ; + T = b(b(b(nil,4,nil),6,nil),2,b(nil,3,b(nil,5,nil))) ; + T = b(b(b(nil,4,nil),2,b(nil,6,nil)),3,b(nil,5,nil)). +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/depthbt_2/common.py b/prolog/problems/trees/depthbt_2/common.py index 4542419..ce04a08 100644 --- a/prolog/problems/trees/depthbt_2/common.py +++ b/prolog/problems/trees/depthbt_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 140 -number = 45 -visible = False +number = 40 +visible = True facts = None solution = '''\ @@ -16,3 +21,37 @@ depthBT(b(L, _, R), D) :- DL < DR, D is DR + 1). ''' + +test_cases = [ + ('depthBT(nil, X), X == 0', + [{}]), + ('depthBT(b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))), X), X == 3', + [{}]), + ('depthBT(b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), X), X == 4', + [{}]), + ('depthBT(b(b(b(b(nil,a,nil),b,nil),c,nil),d,nil), X), X == 4', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/depthbt_2/en.py b/prolog/problems/trees/depthbt_2/en.py index c3f7d4c..ff8d8f3 100644 --- a/prolog/problems/trees/depthbt_2/en.py +++ b/prolog/problems/trees/depthbt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'depthBT/2' slug = 'find the depth of a binary tree' diff --git a/prolog/problems/trees/depthbt_2/sl.py b/prolog/problems/trees/depthbt_2/sl.py new file mode 100644 index 0000000..03589e5 --- /dev/null +++ b/prolog/problems/trees/depthbt_2/sl.py @@ -0,0 +1,11 @@ +name = 'depthBT/2' +slug = 'poišči globino binarnega drevesa' + +description = '''\ +<p><code>depthBT(T, D)</code>: <code>D</code> je globina binarnega drevesa <code>T</code>.</p> +<pre> +?- depthBT(b(b(b(nil,4,nil),2,b(nil,6,nil)),1,nil), D). + D = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/insertbt_3/common.py b/prolog/problems/trees/insertbt_3/common.py index eb1ea94..57c56ee 100644 --- a/prolog/problems/trees/insertbt_3/common.py +++ b/prolog/problems/trees/insertbt_3/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 138 -number = 48 -visible = False +number = 70 +visible = True facts = None solution = '''\ @@ -19,3 +24,56 @@ deleteBT138(X, b(L, E, R), b(L, E, R1)) :- insertBT(X, T, NewT) :- deleteBT138(X, NewT, T). ''' + +test_cases = [ + ('setof(T, insertBT(q,nil,T), X), X == [b(nil,q,nil)]', + [{}]), + ('''setof(T, insertBT(q, b(b(nil,4,nil),2,nil), T), X), + X == [b(b(nil,2,b(nil,4,nil)),q,nil), b(b(nil,4,nil),2,b(nil,q,nil)), + b(b(nil,4,nil),q,b(nil,2,nil)), b(b(nil,4,b(nil,q,nil)),2,nil), + b(b(nil,q,b(nil,4,nil)),2,nil), b(b(b(nil,4,nil),2,nil),q,nil), + b(b(b(nil,4,nil),q,nil),2,nil), b(b(b(nil,q,nil),4,nil),2,nil)]''', + [{}]), + ('''setof(T, insertBT(9, b(b(nil,2,b(nil,6,nil)),1,b(nil,3,nil)), T), X), + X == [b(b(nil,1,b(nil,2,b(nil,6,nil))),9,b(nil,3,nil)), + b(b(nil,1,b(b(nil,6,nil),2,nil)),9,b(nil,3,nil)), + b(b(nil,2,b(nil,6,nil)),1,b(nil,3,b(nil,9,nil))), + b(b(nil,2,b(nil,6,nil)),1,b(nil,9,b(nil,3,nil))), + b(b(nil,2,b(nil,6,nil)),1,b(b(nil,3,nil),9,nil)), + b(b(nil,2,b(nil,6,nil)),1,b(b(nil,9,nil),3,nil)), + b(b(nil,2,b(nil,6,nil)),9,b(nil,1,b(nil,3,nil))), + b(b(nil,2,b(nil,6,nil)),9,b(b(nil,3,nil),1,nil)), + b(b(nil,2,b(nil,6,b(nil,9,nil))),1,b(nil,3,nil)), + b(b(nil,2,b(nil,9,b(nil,6,nil))),1,b(nil,3,nil)), + b(b(nil,2,b(b(nil,6,nil),9,nil)),1,b(nil,3,nil)), + b(b(nil,2,b(b(nil,9,nil),6,nil)),1,b(nil,3,nil)), + b(b(nil,9,b(nil,2,b(nil,6,nil))),1,b(nil,3,nil)), + b(b(nil,9,b(b(nil,6,nil),2,nil)),1,b(nil,3,nil)), + b(b(b(nil,2,nil),1,b(nil,6,nil)),9,b(nil,3,nil)), + b(b(b(nil,2,nil),9,b(nil,6,nil)),1,b(nil,3,nil)), + b(b(b(nil,9,nil),2,b(nil,6,nil)),1,b(nil,3,nil))]''', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/insertbt_3/en.py b/prolog/problems/trees/insertbt_3/en.py index c0fa47c..1205868 100644 --- a/prolog/problems/trees/insertbt_3/en.py +++ b/prolog/problems/trees/insertbt_3/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'insertBT/3' slug = 'insert an element into a binary tree' diff --git a/prolog/problems/trees/insertbt_3/sl.py b/prolog/problems/trees/insertbt_3/sl.py new file mode 100644 index 0000000..f3339fd --- /dev/null +++ b/prolog/problems/trees/insertbt_3/sl.py @@ -0,0 +1,14 @@ +name = 'insertBT/3' +slug = 'vstavi element v binarno drevo' + +description = '''\ +<p><code>insertBT(X, T, NewT)</code>: binarno drevo <code>NewT</code> dobimo tako, da v <code>T</code> vstavimo element <code>X</code> na poljubno mesto. Tvoj program naj vrača vse možne rešitve.</p> +<pre> +?- insertBT(2, b(nil,1,nil), T). + T = b(b(nil,1,nil),2,nil) ; + T = b(nil,2,b(nil,1,nil)) ; + T = b(b(nil,2,nil),1,nil) ; + T = b(nil,1,b(nil,2,nil)). +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/maxt_2/common.py b/prolog/problems/trees/maxt_2/common.py index b1cdb39..5c842d5 100644 --- a/prolog/problems/trees/maxt_2/common.py +++ b/prolog/problems/trees/maxt_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 143 -number = 50 -visible = False +number = 90 +visible = True facts = None solution = '''\ @@ -24,3 +29,35 @@ getMaxElems([SubTree|SubTrees], [MaxElem|MaxElems]) :- maxT(SubTree, MaxElem), getMaxElems(SubTrees, MaxElems). ''' + +test_cases = [ + ('maxT(t(666), X), X == 666', + [{}]), + ('setof(M, maxT(t(3,t(2,t(3),t(6)),t(-1,t(0,t(8)),t(1))), M), X), X == [8]', + [{}]), + ('setof(M, maxT(t(-3,t(-2,t(-3),t(-6)),t(-11,t(-1,t(-8)),t(-10))), M), X), X == [-1]', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/maxt_2/en.py b/prolog/problems/trees/maxt_2/en.py index bb222f8..7a8f59b 100644 --- a/prolog/problems/trees/maxt_2/en.py +++ b/prolog/problems/trees/maxt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'maxT/2' slug = 'find the greatest element in a tree' diff --git a/prolog/problems/trees/maxt_2/sl.py b/prolog/problems/trees/maxt_2/sl.py new file mode 100644 index 0000000..17db5e1 --- /dev/null +++ b/prolog/problems/trees/maxt_2/sl.py @@ -0,0 +1,11 @@ +name = 'maxT/2' +slug = 'poišči največji element v drevesu' + +description = '''\ +<p><code>maxT(Tree, Max)</code>: <code>Max</code> je največji element v drevesu <code>Tree</code>.</p> +<pre> +?- maxT(t(1, t(2), t(3)), Max). + Max = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/memberbt_2/common.py b/prolog/problems/trees/memberbt_2/common.py index 6df89e3..98c1eb2 100644 --- a/prolog/problems/trees/memberbt_2/common.py +++ b/prolog/problems/trees/memberbt_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 135 -number = 42 -visible = False +number = 10 +visible = True facts = None solution = '''\ @@ -12,3 +17,37 @@ memberBT(X, b(L, _, _)) :- memberBT(X, b(_, _, R)) :- memberBT(X, R). ''' + +test_cases = [ + ('memberBT(M, b(nil,r,nil))', + [{'M': 'r'}]), + ('memberBT(M, b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))))', + [{'M': '1'}, {'M': '2'}, {'M': '3'}, {'M': '4'}, {'M': '5'}, + {'M': '6'}]), + ('memberBT(M, b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))))', + [{'M': '1'}, {'M': '2'}, {'M': '3'}, {'M': '4'}, {'M': '7'}, + {'M': '8'}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/memberbt_2/en.py b/prolog/problems/trees/memberbt_2/en.py index 1559151..7c1025b 100644 --- a/prolog/problems/trees/memberbt_2/en.py +++ b/prolog/problems/trees/memberbt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'memberBT/2' slug = 'find elements in a binary tree' diff --git a/prolog/problems/trees/memberbt_2/sl.py b/prolog/problems/trees/memberbt_2/sl.py new file mode 100644 index 0000000..324e6f9 --- /dev/null +++ b/prolog/problems/trees/memberbt_2/sl.py @@ -0,0 +1,13 @@ +name = 'memberBT/2' +slug = 'poišči element v binarnem drevesu' + +description = '''\ +<p><code>memberBT(X, T)</code>: <code>X</code> je element binarnega drevesa <code>T</code>. Vozlišče v binarnem drevesu je predstavljeno s strukturo <code>b(L, E, R)</code>, kjer sta <code>L</code> in <code>R</code> levo in desno poddrevo, <code>E</code> pa je vrednost v tem vozlišču. Prazno drevo predstavlja atom <code>nil</code>.</p> +<pre> +?- memberBT(X, b(b(nil,2,nil),1,b(nil,3,nil))). + X = 1 ; + X = 2 ; + X = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/membert_2/common.py b/prolog/problems/trees/membert_2/common.py index 7d8f207..205487d 100644 --- a/prolog/problems/trees/membert_2/common.py +++ b/prolog/problems/trees/membert_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 142 -number = 49 -visible = False +number = 80 +visible = True facts = None solution = '''\ @@ -17,3 +22,36 @@ memberT(X, Tree) :- memb142(SubTree, SubTrees), memberT(X, SubTree). ''' + +test_cases = [ + ('setof(M, memberT(M, t(r)), X), X == [r]', + [{}]), + ('setof(M, memberT(M, t(3,t(2,t(3),t(6)),t(-1,t(0,t(8)),t(1)))), X), X == [-1,0,1,2,3,6,8]', + [{}]), + ('setof(M, memberT(M, t(r,t(a,t(c),t(d)),t(b,t(e),t(f)))), X), X == [a,b,c,d,e,f,r]', + [{}]), + +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/membert_2/en.py b/prolog/problems/trees/membert_2/en.py index ef48931..11b74c1 100644 --- a/prolog/problems/trees/membert_2/en.py +++ b/prolog/problems/trees/membert_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'memberT/2' slug = 'find elements in a tree' diff --git a/prolog/problems/trees/membert_2/sl.py b/prolog/problems/trees/membert_2/sl.py new file mode 100644 index 0000000..5fee91e --- /dev/null +++ b/prolog/problems/trees/membert_2/sl.py @@ -0,0 +1,13 @@ +name = 'memberT/2' +slug = 'poišči element v drevesu' + +description = '''\ +<p><code>memberT(X, T)</code>: <code>X</code> je element drevesa <code>T</code>. Vozlišče drevesa je predstavljeno s strukturo <code>t(E, T1, T2, …)</code>, kjer je <code>E</code> vrednost v vozlišču, kateri sledi poljubno mnogo poddreves. Prazno drevo predstavlja atom <code>nil</code>.</p> +<pre> +?- memberT(X, t(3, t(1), t(2))). + X = 3 ; + X = 1 ; + X = 2. +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/mirrorbt_2/common.py b/prolog/problems/trees/mirrorbt_2/common.py index 1ee0a9c..ed9162c 100644 --- a/prolog/problems/trees/mirrorbt_2/common.py +++ b/prolog/problems/trees/mirrorbt_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 136 -number = 43 -visible = False +number = 20 +visible = True facts = None solution = '''\ @@ -11,3 +16,41 @@ mirrorBT(b(L, X, R), b(NewR, X, NewL)) :- mirrorBT(L, NewL), mirrorBT(R, NewR). ''' + +test_cases = [ + ('mirrorBT(nil, X)', + [{'X': 'nil'}]), + ('mirrorBT(b(nil,q,nil), X), X == b(nil,q,nil)', + [{}]), + ('mirrorBT(b(b(nil,a,nil),d,b(nil,c,nil)), X), X == b(b(nil,c,nil),d,b(nil,a,nil))', + [{}]), + ('''mirrorBT(b(b(nil,2,nil),1,b(nil,3,b(nil,4,nil))),X), + X == b(b(b(nil,4,nil),3,nil),1,b(nil,2,nil))''', + [{}]), + ('''mirrorBT(b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))),X), + X == b(b(b(nil,5,nil),3,nil),1,b(b(nil,6,nil),2,b(nil,4,nil)))''', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/mirrorbt_2/en.py b/prolog/problems/trees/mirrorbt_2/en.py index 7bc622f..0b87cf2 100644 --- a/prolog/problems/trees/mirrorbt_2/en.py +++ b/prolog/problems/trees/mirrorbt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'mirrorBT/2' slug = 'flip a binary tree horizontally' diff --git a/prolog/problems/trees/mirrorbt_2/sl.py b/prolog/problems/trees/mirrorbt_2/sl.py new file mode 100644 index 0000000..08008ab --- /dev/null +++ b/prolog/problems/trees/mirrorbt_2/sl.py @@ -0,0 +1,11 @@ +name = 'mirrorBT/2' +slug = 'prezrcali binarno drevo' + +description = '''\ +<p><code>mirrorBT(T, NewT)</code>: binarno drevo <code>NewT</code> dobimo tako, da <code>T</code> prezrcalimo čez vertikalo skozi koren.</p> +<pre> +?- mirrorBT(b(b(b(nil,4,nil),2,b(nil,5,nil)),1,b(nil,3,nil)), X). + X = b(b(nil,3,nil), 1, b(b(nil,5,nil), 2, b(nil,4,nil))). +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/numberbt_2/common.py b/prolog/problems/trees/numberbt_2/common.py index e75075e..9061a2d 100644 --- a/prolog/problems/trees/numberbt_2/common.py +++ b/prolog/problems/trees/numberbt_2/common.py @@ -1,8 +1,13 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 139 -number = 44 -visible = False +number = 30 +visible = True facts = None solution = '''\ @@ -12,3 +17,37 @@ numberBT(b(L, _, R), N) :- numberBT(R, RN), N is LN + RN + 1. ''' + +test_cases = [ + ('numberBT(nil, X), X == 0', + [{}]), + ('numberBT(b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))), X), X == 6', + [{}]), + ('numberBT(b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), X), X == 8', + [{}]), + ('numberBT(b(b(b(b(nil,a,nil),b,nil),c,nil),d,nil), X), X == 4', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/numberbt_2/en.py b/prolog/problems/trees/numberbt_2/en.py index 99dc9ca..4cab0f8 100644 --- a/prolog/problems/trees/numberbt_2/en.py +++ b/prolog/problems/trees/numberbt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'numberBT/2' slug = 'find the number of nodes in a binary tree' diff --git a/prolog/problems/trees/numberbt_2/sl.py b/prolog/problems/trees/numberbt_2/sl.py new file mode 100644 index 0000000..d9f0b34 --- /dev/null +++ b/prolog/problems/trees/numberbt_2/sl.py @@ -0,0 +1,11 @@ +name = 'numberBT/2' +slug = 'preštej število vozlišč v binarnem drevesu' + +description = '''\ +<p><code>numberBT(T, N)</code>: <code>N</code> je število vozlišč v binarnem drevesu<code>T</code>.</p> +<pre> +?- numberBT(b(b(nil,2,nil),1,b(nil,3,nil)), N). + N = 3. +</pre>''' + +hint = {} diff --git a/prolog/problems/trees/tolistbt_2/common.py b/prolog/problems/trees/tolistbt_2/common.py index 3c14c4e..290b16b 100644 --- a/prolog/problems/trees/tolistbt_2/common.py +++ b/prolog/problems/trees/tolistbt_2/common.py @@ -1,18 +1,57 @@ -# coding=utf-8 +from operator import itemgetter +import socket + +import prolog.engine +import prolog.util +from server.hints import Hint, HintPopup id = 141 -number = 46 -visible = False +number = 50 +visible = True facts = None solution = '''\ conc141([], L, L). conc141([H|T], L2, [H|L]) :- - conc141(T, L2, L). + conc141(T, L2, L). tolistBT(nil, []). tolistBT(b(L, E, R), List) :- - tolistBT(L, LL), - tolistBT(R, RL), - conc141(LL, [E|RL], List). + tolistBT(L, LL), + tolistBT(R, RL), + conc141(LL, [E|RL], List). ''' + +test_cases = [ + ('tolistBT(nil, X), X == []', + [{}]), + ('tolistBT(b(b(b(nil,4,nil),2,b(nil,6,nil)),1,b(nil,3,b(nil,5,nil))), X), X == [4,2,6,1,3,5]', + [{}]), + ('tolistBT(b(b(b(b(nil,8,nil),7,nil),3,b(nil,2,nil)),8,b(b(nil,4,nil),8,b(nil,1,nil))), X), X == [8,7,3,2,8,4,8,1]', + [{}]), + ('tolistBT(b(b(b(b(nil,a,nil),b,nil),c,nil),d,nil), X), X == [a,b,c,d]', + [{}]), +] + +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 + +def hint(code, aux_code): + # TODO + return [] diff --git a/prolog/problems/trees/tolistbt_2/en.py b/prolog/problems/trees/tolistbt_2/en.py index 94ba3ea..702efbb 100644 --- a/prolog/problems/trees/tolistbt_2/en.py +++ b/prolog/problems/trees/tolistbt_2/en.py @@ -1,5 +1,3 @@ -# coding=utf-8 - name = 'tolistBT/2' slug = 'construct a list with all elements of a binary tree' diff --git a/prolog/problems/trees/tolistbt_2/sl.py b/prolog/problems/trees/tolistbt_2/sl.py new file mode 100644 index 0000000..5545c51 --- /dev/null +++ b/prolog/problems/trees/tolistbt_2/sl.py @@ -0,0 +1,11 @@ +name = 'tolistBT/2' +slug = 'vrni seznam elementov binarnega drevesa' + +description = '''\ +<p><code>tolistBT(T, L)</code>: seznam <code>L</code> vsebuje vse elemente binarnega drevesa <code>T</code> v infiksnem vrstnem redu.</p> +<pre> +?- tolistBT(b(b(nil,2,nil),1,b(nil,3,nil)), L). + L = [2,1,3]. +</pre>''' + +hint = {} |