summaryrefslogtreecommitdiff
path: root/prolog/problems/trees/mirrorbt_2
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/trees/mirrorbt_2')
-rw-r--r--prolog/problems/trees/mirrorbt_2/common.py49
-rw-r--r--prolog/problems/trees/mirrorbt_2/en.py2
-rw-r--r--prolog/problems/trees/mirrorbt_2/sl.py11
3 files changed, 57 insertions, 5 deletions
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 = {}