diff options
author | Martin Možina <martin.mozina@fri.uni-lj.si> | 2015-12-28 10:23:46 +0100 |
---|---|---|
committer | Martin Možina <martin.mozina@fri.uni-lj.si> | 2015-12-28 10:23:46 +0100 |
commit | 165c7165bb2184c9c9e0576a074ba4f29052bf8f (patch) | |
tree | 9b86dbbd1d4c39b52bfd7f54cfb13c2354727a53 /prolog/problems/clp_fd | |
parent | 4cdea335b0951e3c12dfb7b906f958d6b5be25c6 (diff) | |
parent | c1bb0d56b2c0482c766d094c65fdf0fd9d1aa0ba (diff) |
Merge branch 'master' of 192.168.15.97:codeq-problems
Conflicts:
python/problems/functions/palindrome/sl.py
python/problems/functions/palindromic_numbers/sl.py
Diffstat (limited to 'prolog/problems/clp_fd')
-rw-r--r-- | prolog/problems/clp_fd/common.py | 2 | ||||
-rw-r--r-- | prolog/problems/clp_fd/gcd_3/common.py | 41 | ||||
-rw-r--r-- | prolog/problems/clp_fd/gcd_3/sl.py | 14 | ||||
-rw-r--r-- | prolog/problems/clp_fd/magic_1/common.py | 38 | ||||
-rw-r--r-- | prolog/problems/clp_fd/magic_1/sl.py | 14 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_abc_3/common.py | 33 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_abc_3/sl.py | 10 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_beth_1/common.py | 33 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_beth_1/sl.py | 11 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_momson_2/common.py | 35 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_momson_2/sl.py | 10 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_ratio_2/common.py | 33 | ||||
-rw-r--r-- | prolog/problems/clp_fd/puzzle_ratio_2/sl.py | 10 | ||||
-rw-r--r-- | prolog/problems/clp_fd/tobase_3/common.py | 43 | ||||
-rw-r--r-- | prolog/problems/clp_fd/tobase_3/sl.py | 17 |
15 files changed, 344 insertions, 0 deletions
diff --git a/prolog/problems/clp_fd/common.py b/prolog/problems/clp_fd/common.py index 3b1f9cf..845ed8b 100644 --- a/prolog/problems/clp_fd/common.py +++ b/prolog/problems/clp_fd/common.py @@ -1,2 +1,4 @@ id = 8 number = 7 + +allowed_groups = ['lists'] diff --git a/prolog/problems/clp_fd/gcd_3/common.py b/prolog/problems/clp_fd/gcd_3/common.py index 7cd6623..f24e0bb 100644 --- a/prolog/problems/clp_fd/gcd_3/common.py +++ b/prolog/problems/clp_fd/gcd_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 149 number = 61 visible = True @@ -15,3 +19,40 @@ gcd(X, Y, GCD):- cd(X, Y, GCD), \+ ( cd(X, Y, CD), CD > GCD ). ''' + +test_cases = [ + ('gcd(1, 1, X)', + [{'X': '1'}]), + ('gcd(12, 8, X)', + [{'X': '4'}]), + ('gcd(18, 36, X)', + [{'X': '18'}]), + ('gcd(21, 14, X)', + [{'X': '7'}]), + ('gcd(10, 22, X)', + [{'X': '2'}]), + ('gcd(66, 60, X)', + [{'X': '6'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/gcd_3/sl.py b/prolog/problems/clp_fd/gcd_3/sl.py new file mode 100644 index 0000000..9ef1a87 --- /dev/null +++ b/prolog/problems/clp_fd/gcd_3/sl.py @@ -0,0 +1,14 @@ +# coding=utf-8 + +name = 'gcd/3' +slug = 'Največji skupni delitelj' + +description = '''\ +<p><code>gcd(X, Y, GCD)</code>: <code>GCD</code> je največji skupni delitelj števil <code>X</code> in <code>Y</code>. Implementiraj ta predikat z uporabo omejitev.</p> +<p>Namig: poskusi najprej napisati predikat, ki poišče <em>vse</em> skupne delitelje dveh števil.</p> +<pre> + ?- gcd(36, 84, GCD). + GCD = 12. +</pre>''' + +hint = {} diff --git a/prolog/problems/clp_fd/magic_1/common.py b/prolog/problems/clp_fd/magic_1/common.py index 17a9199..3ffe72f 100644 --- a/prolog/problems/clp_fd/magic_1/common.py +++ b/prolog/problems/clp_fd/magic_1/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 151 number = 60 visible = True @@ -16,3 +20,37 @@ magic(L):- A1+B1+C1 #= A3+B3+C3, A1+B2+C3 #= A3+B2+C1, labeling([], L).''' + +test_cases = [ + ('magic(X)', + [{'X': '[2, 7, 6, 9, 5, 1, 4, 3, 8]'}]), + ('magic(X)', + [{'X': '[2, 9, 4, 7, 5, 3, 6, 1, 8]'}]), + ('setof(S, magic(S), X)', + [{'X': '[[2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], \ + [4, 3, 8, 9, 5, 1, 2, 7, 6], [4, 9, 2, 3, 5, 7, 8, 1, 6], \ + [6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 7, 2, 1, 5, 9, 8, 3, 4], \ + [8, 1, 6, 3, 5, 7, 4, 9, 2], [8, 3, 4, 1, 5, 9, 6, 7, 2]]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/magic_1/sl.py b/prolog/problems/clp_fd/magic_1/sl.py new file mode 100644 index 0000000..8cf040e --- /dev/null +++ b/prolog/problems/clp_fd/magic_1/sl.py @@ -0,0 +1,14 @@ +# coding=utf-8 + +name = 'magic/1' +slug = '3x3 magični kvadrat' + +description = '''\ +<p><code>magic(S)</code>: seznam <code>S</code> predstavlja 3×3 magični kvadrat (<code>S</code> je permutacija števil 1 do 9 -- tri števila tvorijo eno vrstico). Vsote števil v vsaki vrstici, stolpcu in glavni diagonali magičnega kvadrata so enake. Implementiraj ta predikat z uporabo omejitev. Predikat naj vrne vse rešitve, eno po eno.</p> +<pre> + ?- magic(S). + S = [2, 7, 6, 9, 5, 1, 4, 3, 8] ; + … +</pre>''' + +hint = {} diff --git a/prolog/problems/clp_fd/puzzle_abc_3/common.py b/prolog/problems/clp_fd/puzzle_abc_3/common.py index 1563e1f..ffce416 100644 --- a/prolog/problems/clp_fd/puzzle_abc_3/common.py +++ b/prolog/problems/clp_fd/puzzle_abc_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 153 number = 57 visible = True @@ -13,3 +17,32 @@ puzzle_abc(A, B, C) :- [A,B,C] ins 0..inf, labeling([], [A,B,C]). ''' + +test_cases = [ + ('puzzle_abc(A, B, C)', + [{'A': '12', 'B': '10', 'C': '5'}]), + ('setof(A/B/C, puzzle_abc(A, B, C), X)', + [{'X': '[12/10/5]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/puzzle_abc_3/sl.py b/prolog/problems/clp_fd/puzzle_abc_3/sl.py new file mode 100644 index 0000000..118481a --- /dev/null +++ b/prolog/problems/clp_fd/puzzle_abc_3/sl.py @@ -0,0 +1,10 @@ +# coding=utf-8 + +name = 'puzzle_abc/3' +slug = 'Uganka z leti: ABC' + +description = '''\ +<p>Oseba <code>A</code> je dve leti starejša od osebe <code>B</code>, ki je dvakrat starejša od osebe <code>C</code>. Skupaj so osebe A, B in C stare 27 let.</p> +<p>Napiši predikat <code>puzzle_abc(A, B, C)</code>, ki izračuna koliko so stare osebe A, B in C.</p>''' + +hint = {} diff --git a/prolog/problems/clp_fd/puzzle_beth_1/common.py b/prolog/problems/clp_fd/puzzle_beth_1/common.py index bf81396..fb00516 100644 --- a/prolog/problems/clp_fd/puzzle_beth_1/common.py +++ b/prolog/problems/clp_fd/puzzle_beth_1/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 155 number = 56 visible = True @@ -8,3 +12,32 @@ facts = None solution = '''\ puzzle_beth(X) :- X + 2 #= 2*(X-5).''' + +test_cases = [ + ('puzzle_beth(X)', + [{'X': '12'}]), + ('setof(A, puzzle_beth(A), X)', + [{'X': '[12]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/puzzle_beth_1/sl.py b/prolog/problems/clp_fd/puzzle_beth_1/sl.py new file mode 100644 index 0000000..261ec74 --- /dev/null +++ b/prolog/problems/clp_fd/puzzle_beth_1/sl.py @@ -0,0 +1,11 @@ +# coding=utf-8 + +name = 'puzzle_beth/1' +slug = 'Uganka z leti: Beti' + +description = '''\ +<p>Ko so Beti vprašali koliko je stara, je odgovorila takole "Čez dve leti bom dvakrat starejša kot sem bila pet let nazaj".</p> +<p>Napiši predikat <code>puzzle_beth(X)</code>, ki izračuna njeno trenutno starost <code>X</code>.</p> +''' + +hint = {} diff --git a/prolog/problems/clp_fd/puzzle_momson_2/common.py b/prolog/problems/clp_fd/puzzle_momson_2/common.py index 9cef246..906bd15 100644 --- a/prolog/problems/clp_fd/puzzle_momson_2/common.py +++ b/prolog/problems/clp_fd/puzzle_momson_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 152 number = 58 visible = True @@ -13,3 +17,34 @@ puzzle_momson(M, S) :- M + S #= 66, M #> S, labeling([], [M, S]).''' + +test_cases = [ + ('puzzle_momson(M, S)', + [{'M': '42', 'S': '24'}]), + ('puzzle_momson(M, S)', + [{'M': '51', 'S': '15'}]), + ('setof(M/S, puzzle_momson(M, S), X)', + [{'X': '[42/24, 51/15]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/puzzle_momson_2/sl.py b/prolog/problems/clp_fd/puzzle_momson_2/sl.py new file mode 100644 index 0000000..593f153 --- /dev/null +++ b/prolog/problems/clp_fd/puzzle_momson_2/sl.py @@ -0,0 +1,10 @@ +# coding=utf-8 + +name = 'puzzle_momson/2' +slug = 'Uganka z leti: mama in sin' + +description = '''\ +<p>Mama in sin sta skupaj stara 66 let. Mamina leta so ravno sinova obrnjena leta (obrnjene so cifre). Koliko sta stara?</p> +<p>Napiši predikat <code>puzzle_momson(M, S)</code>, ki izračuna starost mame <code>M</code> in sina <code>S</code>.</p>''' + +hint = {} diff --git a/prolog/problems/clp_fd/puzzle_ratio_2/common.py b/prolog/problems/clp_fd/puzzle_ratio_2/common.py index 73b9234..736cd2e 100644 --- a/prolog/problems/clp_fd/puzzle_ratio_2/common.py +++ b/prolog/problems/clp_fd/puzzle_ratio_2/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 154 number = 59 visible = True @@ -16,3 +20,32 @@ puzzle_ratio(A, B) :- A #< 200, B #< 200, labeling([], [A, B]).''' + +test_cases = [ + ('puzzle_ratio(A, B)', + [{'A': '30', 'B': '24'}]), + ('setof(A/B, puzzle_ratio(A, B), X)', + [{'X': '[30/24]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/puzzle_ratio_2/sl.py b/prolog/problems/clp_fd/puzzle_ratio_2/sl.py new file mode 100644 index 0000000..826e66f --- /dev/null +++ b/prolog/problems/clp_fd/puzzle_ratio_2/sl.py @@ -0,0 +1,10 @@ +# coding=utf-8 + +name = 'puzzle_ratio/2' +slug = 'Uganka z leti: razmerje' + +description = '''\ +<p>Trenutni starosti osebe <code>A</code> in osebe <code>B</code> sta v razmerju 5:4. Čez tri leta bo razmerje njunih let postalo 11:9.</p> +<p>Napiši predikat <code>puzzle_ratio(A, B)</code>, ki izračuna starost osebe <code>A</code> in osebe <code>B</code>.</p>''' + +hint = {} diff --git a/prolog/problems/clp_fd/tobase_3/common.py b/prolog/problems/clp_fd/tobase_3/common.py index 5d160d7..3783396 100644 --- a/prolog/problems/clp_fd/tobase_3/common.py +++ b/prolog/problems/clp_fd/tobase_3/common.py @@ -1,5 +1,9 @@ # coding=utf-8 +from operator import itemgetter +import prolog.engine +import server.problems + id = 150 number = 62 visible = True @@ -14,3 +18,42 @@ tobase(N, B, Nb) :- Rem #>= 0, Rem #< B, Nb #= Nb1 * 10 + Rem, tobase(N1, B, Nb1).''' + +test_cases = [ + ('tobase(214, 2, X)', + [{'X': '11010110'}]), + ('tobase(321, 7, X)', + [{'X': '636'}]), + ('tobase(X, 8, 777)', + [{'X': '511'}]), + ('tobase(X, 8, 132)', + [{'X': '90'}]), + ('tobase(653, X, 10103)', + [{'X': '5'}]), + ('tobase(123, X, 146)', + [{'X': '9'}]), + ('setof(B/N, tobase(123, B, N), X)', + [{'X': '[2/1111011, 3/11120, 4/1323, 5/443, 6/323, 7/234, 8/173, 9/146, 10/123]'}]), +] + +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 + finally: + if engine_id: + prolog.engine.destroy(engine_id) + + passed = n_correct == len(test_cases) + hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}] + return passed, hints + +def hint(program, solved_problems): + # TODO + return [] diff --git a/prolog/problems/clp_fd/tobase_3/sl.py b/prolog/problems/clp_fd/tobase_3/sl.py new file mode 100644 index 0000000..a775808 --- /dev/null +++ b/prolog/problems/clp_fd/tobase_3/sl.py @@ -0,0 +1,17 @@ +# coding=utf-8 + +name = 'tobase/3' +slug = 'Pretvori števila v/iz desetiškega sistema' + +description = '''\ +<p><code>tobase(Number, B, X)</code>: število <code>Number</code> je v desetiškem sistemu. <code>X</code> predstavlja to število v sistemu z bazo <code>B</code>. Implementiraj predikat z uporabo omejitev. Omeji vrednost <code>B</code> na interval [2..10].</p> +<pre> + ?- tobase(42, 2, X). + X = 101010. + ?- tobase(N, 2, 101010). + N = 42. + ?- tobase(42, B, 101010). + B = 2. +</pre>''' + +hint = {} |