summaryrefslogtreecommitdiff
path: root/python/problems/lists_and_for
diff options
context:
space:
mode:
authorMartin <martin@leo.fri1.uni-lj.si>2015-10-09 16:35:23 +0200
committerMartin <martin@leo.fri1.uni-lj.si>2015-10-09 16:35:23 +0200
commit95e107bf9e6a288969e4a83aee1a7062990f3b67 (patch)
treef69b15164a30d464f6f7b340634095716344b1ec /python/problems/lists_and_for
parentfd31d7d74e58796f3a5a7c84b4b19cb4af59ef4c (diff)
Fixed testing for most problems.
Diffstat (limited to 'python/problems/lists_and_for')
-rw-r--r--python/problems/lists_and_for/calculator_polish/common.py33
-rw-r--r--python/problems/lists_and_for/common.py1
-rw-r--r--python/problems/lists_and_for/contains_multiples/common.py40
-rw-r--r--python/problems/lists_and_for/divisors/common.py2
-rw-r--r--python/problems/lists_and_for/divisors_sum/common.py2
-rw-r--r--python/problems/lists_and_for/every_third/common.py34
-rw-r--r--python/problems/lists_and_for/perfect_numbers/common.py2
-rw-r--r--python/problems/lists_and_for/places/common.py38
-rw-r--r--python/problems/lists_and_for/prefix/common.py25
-rw-r--r--python/problems/lists_and_for/split_word/common.py26
-rw-r--r--python/problems/lists_and_for/substrings/common.py24
11 files changed, 202 insertions, 25 deletions
diff --git a/python/problems/lists_and_for/calculator_polish/common.py b/python/problems/lists_and_for/calculator_polish/common.py
index d08f19c..7ea25ce 100644
--- a/python/problems/lists_and_for/calculator_polish/common.py
+++ b/python/problems/lists_and_for/calculator_polish/common.py
@@ -46,12 +46,39 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ (['6 3 +'], [9]),
+ (['6 3 -'], [3]),
+ (['6 3 /'], [2]),
+ (['7 3 /'], [2]),
+ (['6 3 *'], [18]),
+ (['6 3 %'], [0]),
+ (['1 2 +', '2 3 +', '1 2 3 4 5 * * * *'], [3, 5, 120]),
+ (['1 2 + 3 +'], [6]),
+ (['1 2 3 + +'], [6]),
+ (['1 2 + 3 4 + *'], [21]),
+ (['1 2 3 * 4 + +'], [11]),
+ (['3 5 + 2 * 10 2 1 - * -'], [6]),
+ (['11 22 33 * * 7 + 2 / 100 % 1 2 - -'], [97]),
+ ]
+
+ test_in = [(None, '\n'.join(s[0])+'\n') for s in in_out]
+ test_out = [s[1] for s in in_out]
+ # List of outputs: (expression result, stdout, stderr, exception).
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ outputs = [ans[1] for ans in answers]
+
n_correct = 0
+ tin = None
+ for i, (output, correct) in enumerate(zip(outputs, test_out)):
+ if all(string_almost_equal(output, c, prec=2) for c in correct):
+ n_correct += 1
+ else:
+ tin = test_in[i][1]
+ tout = correct
passed = n_correct == len(test_in)
- tin = None
- tout = None
+
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
diff --git a/python/problems/lists_and_for/common.py b/python/problems/lists_and_for/common.py
index b490107..fe6aa94 100644
--- a/python/problems/lists_and_for/common.py
+++ b/python/problems/lists_and_for/common.py
@@ -1 +1,2 @@
id = 16
+number = 3 \ No newline at end of file
diff --git a/python/problems/lists_and_for/contains_multiples/common.py b/python/problems/lists_and_for/contains_multiples/common.py
index 8bdcc36..4712106 100644
--- a/python/problems/lists_and_for/contains_multiples/common.py
+++ b/python/problems/lists_and_for/contains_multiples/common.py
@@ -25,12 +25,46 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ([], False),
+ ([0], True),
+ ([42], True),
+ ([42 * 43], True),
+ ([4, 2], False),
+ ([42, 42], True),
+ ([1, 2, 3, 4, 5, 5, 4, 3, 2, 1], False),
+ ([1, 2, 3, 4, 5, 5, 4, 3, 2, 1, -42], True),
+ ([1, 2, 3, 4, 5, 42 * 3, 5, 4, 3, 2, 1], True),
+ ([1, 2, 42, 4, 5, 42 * 5, 5, 4, 42 * 3, 2, 1], True),
+ ([42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2], True)
+ ]
+
+
+ test_in = [t[0] for t in in_out]
+ test_out = [t[1] for t in in_out]
+
n_correct = 0
+ tin = None
+ for xs_i, xs in enumerate(test_in):
+ # change code to contain new xs instead of the one
+ # given by user
+ tcode = re.sub(r'^xs\s*=\s*\[.*?\]',
+ 'xs = ' + str(xs),
+ code,
+ flags = re.DOTALL | re.MULTILINE)
+ # use python session to call tcode
+ answers = python(code=tcode, inputs=[(None, None)], timeout=1.0)
+ output = answers[0][1]
+
+ if str(test_out[xs_i]) in output and \
+ str((not test_out[xs_i])) not in output:
+ n_correct += 1
+ else:
+ tin = test_in[xs_i]
+ tout = test_out[xs_i]
passed = n_correct == len(test_in)
- tin = None
- tout = None
+
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
diff --git a/python/problems/lists_and_for/divisors/common.py b/python/problems/lists_and_for/divisors/common.py
index f9334be..89092eb 100644
--- a/python/problems/lists_and_for/divisors/common.py
+++ b/python/problems/lists_and_for/divisors/common.py
@@ -8,7 +8,7 @@ from server.hints import Hint
id = 204
group = 'lists_and_for'
number = 20
-visible = False
+visible = True
solution = '''\
n = int(input('Vnesi število: '))
diff --git a/python/problems/lists_and_for/divisors_sum/common.py b/python/problems/lists_and_for/divisors_sum/common.py
index 651f657..c557278 100644
--- a/python/problems/lists_and_for/divisors_sum/common.py
+++ b/python/problems/lists_and_for/divisors_sum/common.py
@@ -8,7 +8,7 @@ from server.hints import Hint
id = 216
group = 'lists_and_for'
number = 21
-visible = False
+visible = True
solution = '''\
n = int(input('Vnesi število: '))
diff --git a/python/problems/lists_and_for/every_third/common.py b/python/problems/lists_and_for/every_third/common.py
index 9c27446..6f42663 100644
--- a/python/problems/lists_and_for/every_third/common.py
+++ b/python/problems/lists_and_for/every_third/common.py
@@ -26,12 +26,40 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ([], []),
+ ([0], []),
+ ([0, 1], []),
+ ([0, 1, 2], [2]),
+ ([1, 2, 3, 4, 5, 5, 4, 3, 2, 1], [3, 5, 2]),
+ ([42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2], [4, 12, 11]),
+ (list(range(15)), [2, 5, 8, 11, 14])
+ ]
+
+ test_in = [t[0] for t in in_out]
+ test_out = [t[1] for t in in_out]
+
n_correct = 0
+ tin = None
+ for xs_i, xs in enumerate(test_in):
+ # change code to contain new xs instead of the one
+ # given by user
+ tcode = re.sub(r'^xs\s*=\s*\[.*?\]',
+ 'xs = ' + str(xs),
+ code,
+ flags = re.DOTALL | re.MULTILINE)
+ # use python session to call tcode
+ answers = python(code=tcode, inputs=[(None, None)], timeout=1.0)
+ output = answers[0][1]
+
+ if str(test_out[xs_i]) in output:
+ n_correct += 1
+ else:
+ tin = test_in[xs_i]
+ tout = test_out[xs_i]
passed = n_correct == len(test_in)
- tin = None
- tout = None
+
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
diff --git a/python/problems/lists_and_for/perfect_numbers/common.py b/python/problems/lists_and_for/perfect_numbers/common.py
index c1c7645..2628fab 100644
--- a/python/problems/lists_and_for/perfect_numbers/common.py
+++ b/python/problems/lists_and_for/perfect_numbers/common.py
@@ -8,7 +8,7 @@ from server.hints import Hint
id = 217
group = 'lists_and_for'
number = 22
-visible = False
+visible = True
solution = '''\
n = int(input('Vnesi število: '))
diff --git a/python/problems/lists_and_for/places/common.py b/python/problems/lists_and_for/places/common.py
index 66db7a3..0a90771 100644
--- a/python/problems/lists_and_for/places/common.py
+++ b/python/problems/lists_and_for/places/common.py
@@ -25,12 +25,44 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ([], []),
+ ([0], []),
+ ([42], [0]),
+ ([4, 2], []),
+ ([42, 42], [0, 1]),
+ ([1, 2, 3, 4, 5, 5, 4, 3, 2, 1], []),
+ ([1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 42], [10]),
+ ([1, 2, 3, 4, 5, 42, 5, 4, 3, 2, 1], [5]),
+ ([1, 2, 42, 4, 5, 42, 5, 4, 42, 2, 1], [2, 5, 8]),
+ ([42, 5, 4, -7, 2, 12, -3, -4, 11, 42, 2], [0, 9]),
+ ([42] * 10, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
+ ]
+
+ test_in = [t[0] for t in in_out]
+ test_out = [t[1] for t in in_out]
+
n_correct = 0
+ tin = None
+ for xs_i, xs in enumerate(test_in):
+ # change code to contain new xs instead of the one
+ # given by user
+ tcode = re.sub(r'^xs\s*=\s*\[.*?\]',
+ 'xs = ' + str(xs),
+ code,
+ flags = re.DOTALL | re.MULTILINE)
+
+ # use python session to call tcode
+ answers = python(code=tcode, inputs=[(None, None)], timeout=1.0)
+ output = answers[0][1]
+
+ if str(test_out[xs_i]) in output:
+ n_correct += 1
+ else:
+ tin = test_in[xs_i]
+ tout = test_out[xs_i]
passed = n_correct == len(test_in)
- tin = None
- tout = None
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
diff --git a/python/problems/lists_and_for/prefix/common.py b/python/problems/lists_and_for/prefix/common.py
index cb8d706..1198e65 100644
--- a/python/problems/lists_and_for/prefix/common.py
+++ b/python/problems/lists_and_for/prefix/common.py
@@ -24,12 +24,29 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ('a', ['', 'a']),
+ ('ab', ['', 'a', 'ab']),
+ ('abc', ['', 'a', 'ab', 'abc']),
+ ('drevo', ['', 'd', 'dr', 'dre', 'drev', 'drevo']),
+ ]
+
+ test_in = [(None, s[0]+'\n') for s in in_out]
+ test_out = [str(s[1]) for s in in_out]
+ # List of outputs: (expression result, stdout, stderr, exception).
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ outputs = [ans[1] for ans in answers]
+
n_correct = 0
+ tin = None
+ for i, (output, correct) in enumerate(zip(outputs, test_out)):
+ if correct in output:
+ n_correct += 1
+ else:
+ tin = test_in[i][1]
+ tout = correct
passed = n_correct == len(test_in)
- tin = None
- tout = None
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
@@ -46,3 +63,5 @@ def hint(python, code):
if exc: return exc
return None
+
+
diff --git a/python/problems/lists_and_for/split_word/common.py b/python/problems/lists_and_for/split_word/common.py
index 0947b09..0f5c5b9 100644
--- a/python/problems/lists_and_for/split_word/common.py
+++ b/python/problems/lists_and_for/split_word/common.py
@@ -16,7 +16,7 @@ s = input('Vpiši besedo: ')
xs = []
for i in range(len(s) + 1):
xs.append((s[:i], s[i:]))
-print(x)
+print(xs)
'''
hint_type = {
@@ -24,12 +24,30 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ('a', [('', 'a'), ('a', '')]),
+ ('ab', [('', 'ab'), ('a', 'b'), ('ab', '')]),
+ ('abc', [('', 'abc'), ('a', 'bc'), ('ab', 'c'), ('abc', '')]),
+ ('gozd', [('', 'gozd'), ('g', 'ozd'), ('go', 'zd'), ('goz', 'd'), ('gozd', '')]),
+ ]
+
+ test_in = [(None, s[0]+'\n') for s in in_out]
+ test_out = [str(s[1]) for s in in_out]
+ # List of outputs: (expression result, stdout, stderr, exception).
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ outputs = [ans[1] for ans in answers]
+
n_correct = 0
+ tin = None
+ for i, (output, correct) in enumerate(zip(outputs, test_out)):
+ if correct in output:
+ n_correct += 1
+ else:
+ tin = test_in[i][1]
+ tout = correct
passed = n_correct == len(test_in)
- tin = None
- tout = None
+
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})
diff --git a/python/problems/lists_and_for/substrings/common.py b/python/problems/lists_and_for/substrings/common.py
index 32a3d4c..bdc356a 100644
--- a/python/problems/lists_and_for/substrings/common.py
+++ b/python/problems/lists_and_for/substrings/common.py
@@ -27,12 +27,30 @@ hint_type = {
}
def test(python, code):
- test_in = [1]
+ in_out = [
+ ('a', ['', 'a']),
+ ('ab', ['', 'a', 'b', 'ab']),
+ ('abc', ['', 'a', 'b', 'c', 'ab', 'bc', 'abc']),
+ ('tema', ['', 't', 'e', 'm', 'a', 'te', 'em', 'ma', 'tem', 'ema', 'tema']),
+ ]
+
+ test_in = [(None, s[0]+'\n') for s in in_out]
+ test_out = [str(s[1]) for s in in_out]
+ # List of outputs: (expression result, stdout, stderr, exception).
+ answers = python(code=code, inputs=test_in, timeout=1.0)
+ outputs = [ans[1] for ans in answers]
+
n_correct = 0
+ tin = None
+ for i, (output, correct) in enumerate(zip(outputs, test_out)):
+ if correct in output:
+ n_correct += 1
+ else:
+ tin = test_in[i][1]
+ tout = correct
passed = n_correct == len(test_in)
- tin = None
- tout = None
+
hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_in)}}]
if tin:
hints.append({'id': 'problematic_test_case', 'args': {'testin': str(tin), 'testout': str(tout)}})