From 12378810ee9207536bfa0c264c1bf2a2b0296171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Mo=C5=BEina?= Date: Wed, 24 Aug 2016 15:32:22 +0200 Subject: Added several new exercises for Python (related to dictionaries and sets). --- python/problems/dictionaries/children/common.py | 75 ++++++++++++++++++ python/problems/dictionaries/children/en.py | 13 ++++ python/problems/dictionaries/children/sl.py | 35 +++++++++ python/problems/dictionaries/common.py | 2 + python/problems/dictionaries/en.py | 3 + python/problems/dictionaries/family.png | Bin 0 -> 37655 bytes python/problems/dictionaries/family/common.py | 80 +++++++++++++++++++ python/problems/dictionaries/family/en.py | 14 ++++ python/problems/dictionaries/family/sl.py | 55 +++++++++++++ .../dictionaries/following_words/common.py | 70 +++++++++++++++++ python/problems/dictionaries/following_words/en.py | 13 ++++ python/problems/dictionaries/following_words/sl.py | 36 +++++++++ .../dictionaries/freq_following_word/common.py | 81 +++++++++++++++++++ .../dictionaries/freq_following_word/en.py | 13 ++++ .../dictionaries/freq_following_word/sl.py | 30 +++++++ .../problems/dictionaries/grandchildren/common.py | 84 ++++++++++++++++++++ python/problems/dictionaries/grandchildren/en.py | 13 ++++ python/problems/dictionaries/grandchildren/sl.py | 33 ++++++++ .../problems/dictionaries/max_characters/common.py | 72 +++++++++++++++++ python/problems/dictionaries/max_characters/en.py | 13 ++++ python/problems/dictionaries/max_characters/sl.py | 29 +++++++ .../problems/dictionaries/most_frequent/common.py | 82 ++++++++++++++++++++ python/problems/dictionaries/most_frequent/en.py | 13 ++++ python/problems/dictionaries/most_frequent/sl.py | 30 +++++++ .../problems/dictionaries/show_letters/common.py | 67 ++++++++++++++++ python/problems/dictionaries/show_letters/en.py | 13 ++++ python/problems/dictionaries/show_letters/sl.py | 33 ++++++++ python/problems/dictionaries/sl.py | 3 + python/problems/dictionaries/successors/common.py | 82 ++++++++++++++++++++ python/problems/dictionaries/successors/en.py | 13 ++++ python/problems/dictionaries/successors/sl.py | 35 +++++++++ python/problems/dictionaries/text/common.py | 86 +++++++++++++++++++++ python/problems/dictionaries/text/en.py | 13 ++++ python/problems/dictionaries/text/sl.py | 45 +++++++++++ python/problems/dictionaries/text/tmp.py | 29 +++++++ .../problems/dictionaries/transactions/common.py | 71 +++++++++++++++++ python/problems/dictionaries/transactions/en.py | 13 ++++ python/problems/dictionaries/transactions/sl.py | 41 ++++++++++ python/problems/dictionaries/waiter/common.py | 81 +++++++++++++++++++ python/problems/dictionaries/waiter/en.py | 13 ++++ python/problems/dictionaries/waiter/sl.py | 50 ++++++++++++ 41 files changed, 1577 insertions(+) create mode 100644 python/problems/dictionaries/children/common.py create mode 100644 python/problems/dictionaries/children/en.py create mode 100644 python/problems/dictionaries/children/sl.py create mode 100644 python/problems/dictionaries/common.py create mode 100644 python/problems/dictionaries/en.py create mode 100644 python/problems/dictionaries/family.png create mode 100644 python/problems/dictionaries/family/common.py create mode 100644 python/problems/dictionaries/family/en.py create mode 100644 python/problems/dictionaries/family/sl.py create mode 100644 python/problems/dictionaries/following_words/common.py create mode 100644 python/problems/dictionaries/following_words/en.py create mode 100644 python/problems/dictionaries/following_words/sl.py create mode 100644 python/problems/dictionaries/freq_following_word/common.py create mode 100644 python/problems/dictionaries/freq_following_word/en.py create mode 100644 python/problems/dictionaries/freq_following_word/sl.py create mode 100644 python/problems/dictionaries/grandchildren/common.py create mode 100644 python/problems/dictionaries/grandchildren/en.py create mode 100644 python/problems/dictionaries/grandchildren/sl.py create mode 100644 python/problems/dictionaries/max_characters/common.py create mode 100644 python/problems/dictionaries/max_characters/en.py create mode 100644 python/problems/dictionaries/max_characters/sl.py create mode 100644 python/problems/dictionaries/most_frequent/common.py create mode 100644 python/problems/dictionaries/most_frequent/en.py create mode 100644 python/problems/dictionaries/most_frequent/sl.py create mode 100644 python/problems/dictionaries/show_letters/common.py create mode 100644 python/problems/dictionaries/show_letters/en.py create mode 100644 python/problems/dictionaries/show_letters/sl.py create mode 100644 python/problems/dictionaries/sl.py create mode 100644 python/problems/dictionaries/successors/common.py create mode 100644 python/problems/dictionaries/successors/en.py create mode 100644 python/problems/dictionaries/successors/sl.py create mode 100644 python/problems/dictionaries/text/common.py create mode 100644 python/problems/dictionaries/text/en.py create mode 100644 python/problems/dictionaries/text/sl.py create mode 100644 python/problems/dictionaries/text/tmp.py create mode 100644 python/problems/dictionaries/transactions/common.py create mode 100644 python/problems/dictionaries/transactions/en.py create mode 100644 python/problems/dictionaries/transactions/sl.py create mode 100644 python/problems/dictionaries/waiter/common.py create mode 100644 python/problems/dictionaries/waiter/en.py create mode 100644 python/problems/dictionaries/waiter/sl.py (limited to 'python/problems/dictionaries') diff --git a/python/problems/dictionaries/children/common.py b/python/problems/dictionaries/children/common.py new file mode 100644 index 0000000..437835e --- /dev/null +++ b/python/problems/dictionaries/children/common.py @@ -0,0 +1,75 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20607 +number = 8 +visible = True + +solution = '''\ +def children(tree, name): + if name in tree: + return tree[name] + return [] +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + + +def test(python, code): + func_name = 'children' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + tree = { + 'alice': ['mary', 'tom', 'judy'], + 'bob': ['mary', 'tom', 'judy'], + 'ken': ['suzan'], + 'renee': ['rob', 'bob'], + 'rob': ['jim'], + 'sid': ['rob', 'bob'], + 'tom': ['ken']} + + in_out = [ + ((tree, 'alice'), ['mary', 'tom', 'judy']), + ((tree, 'mary'), []), + ((tree, 'renee'), ['rob', 'bob']), + ((tree, 'rob'), ['jim']), + ((tree, 'suzan'), []), + ] + + test_in = [(func_name+'%s'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = set(ans[0]) == set(to) and len(ans[0]) == len(to) + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/children/en.py b/python/problems/dictionaries/children/en.py new file mode 100644 index 0000000..a3a40c1 --- /dev/null +++ b/python/problems/dictionaries/children/en.py @@ -0,0 +1,13 @@ +id = 20607 +name = 'Children (family tree 2)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/children/sl.py b/python/problems/dictionaries/children/sl.py new file mode 100644 index 0000000..2717a1b --- /dev/null +++ b/python/problems/dictionaries/children/sl.py @@ -0,0 +1,35 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20607 +name = 'Otroci (družinsko drevo 2)' + +description = '''\ +

+Napišite funkcijo children(tree, name), ki v družinskem drevesu tree +vrne seznam vseh otrok osebe. V primeru, da oseba nima otrok, vrnite prazen seznam. +

+ +

+Družinsko drevo tree je slovar, kjer ključi predstavljajo imena +staršev, vrednosti pa so njihovi otroci. Primer iz prejšnje naloge: +

+>>> tree = {'renee': ['rob', 'bob'], 'ken': ['suzan'], 'rob': ['jim'],
+            'sid': ['rob', 'bob'], ... , 'bob': ['mary', 'tom', 'judy']}
+>>> children(tree, 'alice')
+['mary', 'tom', 'judy']
+>>> children(tree, 'mary')
+[]
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/common.py b/python/problems/dictionaries/common.py new file mode 100644 index 0000000..bc5cbc7 --- /dev/null +++ b/python/problems/dictionaries/common.py @@ -0,0 +1,2 @@ +id = 2006 +number = 6 \ No newline at end of file diff --git a/python/problems/dictionaries/en.py b/python/problems/dictionaries/en.py new file mode 100644 index 0000000..bf21767 --- /dev/null +++ b/python/problems/dictionaries/en.py @@ -0,0 +1,3 @@ +name = 'Dictionaries' +description = 'Dictionaries and Sets' + diff --git a/python/problems/dictionaries/family.png b/python/problems/dictionaries/family.png new file mode 100644 index 0000000..a5e1c03 Binary files /dev/null and b/python/problems/dictionaries/family.png differ diff --git a/python/problems/dictionaries/family/common.py b/python/problems/dictionaries/family/common.py new file mode 100644 index 0000000..8d175e1 --- /dev/null +++ b/python/problems/dictionaries/family/common.py @@ -0,0 +1,80 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20601 +number = 7 +visible = True + +solution = '''\ +def family_tree(family): + tree = {} + for parent, child in family: + if parent not in tree: + tree[parent] = [] + tree[parent].append(child) + return tree +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'family_tree' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + + tree = { + 'alice': ['mary', 'tom', 'judy'], + 'bob': ['mary', 'tom', 'judy'], + 'ken': ['suzan'], + 'renee': ['rob', 'bob'], + 'rob': ['jim'], + 'sid': ['rob', 'bob'], + 'tom': ['ken']} + in_out = [ + ([('bob', 'mary')], {'bob':['mary']}), + ([('bob', 'mary'), ('bob', 'tom')], {'bob':['mary','tom']}), + ([], {}), + ([('bob','mary'),('mary','ken'),('alice','mary')], + {'bob':['mary'], 'mary':['ken'], 'alice':['mary']}), + ([('bob','mary'), ('bob', 'tom'), ('bob', 'judy'), ('alice', 'mary'), + ('alice', 'tom'), ('alice', 'judy'),('renee', 'rob'), ('renee', 'bob'), + ('sid', 'rob'), ('sid', 'bob'),('tom', 'ken'), ('ken', 'suzan'), + ('rob', 'jim')],tree) + ] + + test_in = [(func_name+'(%s)'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += dict(ans[0]) == to + if dict(ans[0]) != to: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/family/en.py b/python/problems/dictionaries/family/en.py new file mode 100644 index 0000000..d04f58e --- /dev/null +++ b/python/problems/dictionaries/family/en.py @@ -0,0 +1,14 @@ +id = 20601 +name = 'Family Tree' +slug = 'Family Tree' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/family/sl.py b/python/problems/dictionaries/family/sl.py new file mode 100644 index 0000000..2a572d3 --- /dev/null +++ b/python/problems/dictionaries/family/sl.py @@ -0,0 +1,55 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20601 +name = 'Družinsko drevo' + +description = '''\ +

+V seznamu imamo spravljeno družinsko drevo. Primer: +

+family = [('bob', 'mary'), ('bob', 'tom'),
+          ('bob', 'judy'), ('alice', 'mary'),
+          ('alice', 'tom'), ('alice', 'judy'),
+          ('renee', 'rob'), ('renee', 'bob'),
+          ('sid', 'rob'), ('sid', 'bob'),
+          ('tom', 'ken'), ('ken', 'suzan'),
+          ('rob', 'jim')]
+
+

+

+V vsaki terki sta zapisani dve imeni: ime starša in ime otroka. Terka +('bob', 'mary') nam pove, da je Bob Maryjin oče, terka +('bob', 'tom') pa, da je Bob Tomov oče, itd. +Za lažje razumevanje si relacije predstavimo s sliko: +

+ + + +
+

+

Naloga

+

+Napišite funkcijo family_tree(family), ki sprejeme seznam +v katerem je spravljeno družinsko drevo in vrne slovar v katerem je za vsakega +starša spravljen seznam vseh njegovih otrok.

+
+>>> family_tree(family)
+{'renee': ['rob', 'bob'],
+'ken': ['suzan'],
+'rob': ['jim'],
+'sid': ['rob', 'bob'],
+... ,
+'bob': ['mary', 'tom', 'judy']}
+
+

''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/following_words/common.py b/python/problems/dictionaries/following_words/common.py new file mode 100644 index 0000000..501cfd2 --- /dev/null +++ b/python/problems/dictionaries/following_words/common.py @@ -0,0 +1,70 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20610 +number = 11 +visible = True + +solution = '''\ +import collections + +def following_words(txt): + words = txt.split() + freq = collections.defaultdict(list) + for word, next_word in zip(words, words[1:]): + freq[word].append(next_word) + return freq +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'following_words' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ('in in in in', {'in': ['in', 'in', 'in']}), + ('in to in ono in to smo mi', {'smo': ['mi'], 'to': ['in', 'smo'], + 'ono': ['in'], 'in': ['to', 'ono', 'to']}), + ('danes je lep dan danes sije sonce', + {'lep': ['dan'], 'je': ['lep'], 'dan': ['danes'], + 'danes': ['je', 'sije'], 'sije': ['sonce']}), + ] + + test_in = [(func_name+'("%s")'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = dict(ans[0]) == to + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/following_words/en.py b/python/problems/dictionaries/following_words/en.py new file mode 100644 index 0000000..44de7c2 --- /dev/null +++ b/python/problems/dictionaries/following_words/en.py @@ -0,0 +1,13 @@ +id = 20610 +name = 'Following words' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/following_words/sl.py b/python/problems/dictionaries/following_words/sl.py new file mode 100644 index 0000000..b9d6740 --- /dev/null +++ b/python/problems/dictionaries/following_words/sl.py @@ -0,0 +1,36 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20610 +name = 'Sledeče besede' + +description = '''\ +

+Napišite funkcijo following_words(txt), ki za vsako besedo b +v nizu txt zgradi seznam besed, ki v tekstu nastopijo za +besedo b. Nekaj primerov: +

+>>> following_words('in in in in')
+{'in': ['in', 'in', 'in']}
+
+V besedilu se pojavi samo beseda in; beseda in se pojavi +na treh mestih za in. +
+>>> following_words('in to in ono in to smo mi')
+{'smo': ['mi'], 'to': ['in', 'smo'], 'ono': ['in'], 'in': ['to', 'ono', 'to']}
+
+Za besedo smo se pojavi samo beseda mi, medtem ko se +recimo za besedo to pojavita tako beseda smo kot +tudi beseda in. +

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/freq_following_word/common.py b/python/problems/dictionaries/freq_following_word/common.py new file mode 100644 index 0000000..e00b332 --- /dev/null +++ b/python/problems/dictionaries/freq_following_word/common.py @@ -0,0 +1,81 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20611 +number = 12 +visible = True + +solution = '''\ +import collections + +def following_words(txt): + words = txt.split() + freq = collections.defaultdict(list) + for word, next_word in zip(words, words[1:]): + freq[word].append(next_word) + return freq + +def freq_following_word(txt): + following = following_words(txt) + for f in following: + vals = collections.Counter(following[f]) + s = sorted(vals.most_common(), key = lambda x: (-x[1], x[0])) + following[f] = s[0][0] + return following +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'freq_following_word' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ('in in in in', {'in': 'in'}), + ('in to in ono in to smo mi', {'smo': 'mi', 'to': 'in', + 'ono': 'in', 'in': 'to'}), + ('danes je lep dan danes sije sonce', + {'lep': 'dan', 'je': 'lep', 'dan': 'danes', + 'danes': 'je', 'sije': 'sonce'}), + ('danes je lep dan danes sije sonce danes sije dan ki je sonce', + {'lep': 'dan', 'je': 'lep', 'dan': 'danes', + 'danes': 'sije', 'sije': 'dan', 'ki': 'je', 'sonce':'danes'}), + ] + + test_in = [(func_name+'("%s")'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = dict(ans[0]) == to + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/freq_following_word/en.py b/python/problems/dictionaries/freq_following_word/en.py new file mode 100644 index 0000000..223eaa4 --- /dev/null +++ b/python/problems/dictionaries/freq_following_word/en.py @@ -0,0 +1,13 @@ +id = 20611 +name = 'Most frequent following word' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} \ No newline at end of file diff --git a/python/problems/dictionaries/freq_following_word/sl.py b/python/problems/dictionaries/freq_following_word/sl.py new file mode 100644 index 0000000..eb12456 --- /dev/null +++ b/python/problems/dictionaries/freq_following_word/sl.py @@ -0,0 +1,30 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20611 +name = 'Najpogostejša sledeča beseda' + +description = '''\ +

+Napišite funkcijo freq_following_word(txt), ki za vsako besedo b +v nizu txt zapiše v slovarju najpogostejšo besedo, ki +sledi besedi b. Če je več takih besed, vzami prvo po abecedi. +Nekaj primerov: +

+>>> freq_following_word('in in in in')
+{'in': 'in'}
+>>> freq_following_word('in to in ono in to smo mi')
+{'smo': 'mi', 'to': 'in' , 'ono': 'in', 'in': 'to']}
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/grandchildren/common.py b/python/problems/dictionaries/grandchildren/common.py new file mode 100644 index 0000000..0e71fba --- /dev/null +++ b/python/problems/dictionaries/grandchildren/common.py @@ -0,0 +1,84 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20608 +number = 9 +visible = True + +solution = '''\ +def children(tree, name): + if name in tree: + return tree[name] + return [] + +def grandchildren(tree, name): + names = [] + for child in children(tree, name): + for grandchild in children(tree, child): + names.append(grandchild) + return names +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + + +def test(python, code): + func_name = 'grandchildren' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + tree = { + 'alice': ['mary', 'tom', 'judy'], + 'bob': ['mary', 'tom', 'judy'], + 'ken': ['suzan'], + 'renee': ['rob', 'bob'], + 'rob': ['jim'], + 'sid': ['rob', 'bob'], + 'tom': ['ken']} + + in_out = [ + ((tree, 'alice'), ['ken']), + ((tree, 'bob'), ['ken']), + ((tree, 'ken'), []), + ((tree, 'mary'), []), + ((tree, 'renee'), ['jim', 'mary', 'tom', 'judy']), + ((tree, 'sid'), ['jim', 'mary', 'tom', 'judy']), + ((tree, 'tom'), ['suzan']), +] + + test_in = [(func_name+'%s'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = set(ans[0]) == set(to) and len(ans[0]) == len(to) + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/grandchildren/en.py b/python/problems/dictionaries/grandchildren/en.py new file mode 100644 index 0000000..3f98328 --- /dev/null +++ b/python/problems/dictionaries/grandchildren/en.py @@ -0,0 +1,13 @@ +id = 20608 +name = 'Grandchildren (family tree 3)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/grandchildren/sl.py b/python/problems/dictionaries/grandchildren/sl.py new file mode 100644 index 0000000..b24c315 --- /dev/null +++ b/python/problems/dictionaries/grandchildren/sl.py @@ -0,0 +1,33 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20608 +name = 'Vnuki (družinsko drevo 3)' + +description = '''\ +

+Napišite funkcijo grandchildren(tree, name), ki v družinskem drevesu tree +vrne seznam vseh vnukov in vnukinj osebe. V primeru, da oseba nima vnukov, vrnite prazen seznam. +

+ +

+Družinsko drevo tree je slovar, kjer ključi predstavljajo imena +staršev, vrednosti pa so njihovi otroci. Primer iz prejšnje naloge: +

+>>> tree = {'renee': ['rob', 'bob'], 'ken': ['suzan'], 'rob': ['jim'],
+            'sid': ['rob', 'bob'], ... , 'bob': ['mary', 'tom', 'judy']}
+>>> grandchildren(tree, 'renee')
+['jim', 'mary', 'tom', 'judy']
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/max_characters/common.py b/python/problems/dictionaries/max_characters/common.py new file mode 100644 index 0000000..cb0c4b6 --- /dev/null +++ b/python/problems/dictionaries/max_characters/common.py @@ -0,0 +1,72 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20603 +number = 2 +visible = True + +solution = '''\ +def maxchars(words): + max_chars = 0 + max_word = None + for w in words: + chars = len(set(w.lower())) + if chars > max_chars: + max_chars = chars + max_word = w + return max_word +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'maxchars' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ([], None), + (['RABarbara', 'izpit', 'zmagA'], 'izpit'), + (['RABarbara'], 'RABarbara'), + (['RABarbara', 'Izpit', 'izpit', 'zmagA'], 'Izpit'), + (['RABarbara', 'izpit', 'Izpit', 'zmagA'], 'izpit'), + (['Vesel', 'RABarbara', 'izpit', 'Izpit', 'zmagA'], 'Vesel'), + (['RABarbara', 'izpit', 'Izpit', 'zmagA', 'Vesel'], 'izpit'), + (['RABarbara', 'izpit', 'Izpit', 'zmagA', 'Veselica'], 'Veselica'), + ] + + test_in = [('{0}({1})'.format(func_name, str(l[0])), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += ans[0] == to + if ans[0] != to: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/max_characters/en.py b/python/problems/dictionaries/max_characters/en.py new file mode 100644 index 0000000..e42996e --- /dev/null +++ b/python/problems/dictionaries/max_characters/en.py @@ -0,0 +1,13 @@ +id = 20603 +name = 'Number of characters' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/max_characters/sl.py b/python/problems/dictionaries/max_characters/sl.py new file mode 100644 index 0000000..835c84f --- /dev/null +++ b/python/problems/dictionaries/max_characters/sl.py @@ -0,0 +1,29 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20603 +name = 'Število znakov' + +description = '''\ +

+Napiši funkcijo maxchars(words), ki kot argument prejme seznam nizov +in kot rezultat vrne niz, ki ima največ različnih znakov. Male in velike črke +upoštevaj kot iste znake - beseda "MamA" ima samo dva različna znaka. +Če obstaja več besed z enakim številom različnih znakov, naj vrne prvo med njimi. +Če je seznam prazen, naj funkcija vrne None. +

+
+>>> besede = ["RABarbara", "izpit", "zmagA"]
+>>> maxchars(besede)
+'izpit'
+'''
+
+plan = []
+
+hint = {
+    'final_hint': ['''\
+

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/most_frequent/common.py b/python/problems/dictionaries/most_frequent/common.py new file mode 100644 index 0000000..a945f1d --- /dev/null +++ b/python/problems/dictionaries/most_frequent/common.py @@ -0,0 +1,82 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20604 +number = 3 +visible = True + +solution = '''\ +# Count number of elements in list xs. +def freq(xs): + hist = {} + for x in xs: + if x not in hist: + hist[x] = 0 + hist[x] += 1 + return hist + +# Find element with maximal frequency +def max_freq(f): + max_freq = 0 + max_item = None + for item, item_freq in f.items(): + if item_freq > max_freq: + max_freq = item_freq + max_item = item + return max_item + +def most_frequent(s): + return max_freq(freq(s.split())), max_freq(freq(s)) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'most_frequent' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ('a', ('a', 'a')), + ('aa bb aa', ('aa', 'a')), + ('in to in ono in to smo mi', ('in', ' ')), + ('abc abc abc abacbca', ('abc', 'a')), + ('abc abc abc abacbcb', ('abc', 'b')), + ('abc abc abc abacbcc', ('abc', 'c')), + ] + + test_in = [('{0}("{1}")'.format(func_name, str(l[0])), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += ans[0] == to + if ans[0] != to: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/most_frequent/en.py b/python/problems/dictionaries/most_frequent/en.py new file mode 100644 index 0000000..1ef9394 --- /dev/null +++ b/python/problems/dictionaries/most_frequent/en.py @@ -0,0 +1,13 @@ +id = 20604 +name = 'Most frequent' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/most_frequent/sl.py b/python/problems/dictionaries/most_frequent/sl.py new file mode 100644 index 0000000..c877e20 --- /dev/null +++ b/python/problems/dictionaries/most_frequent/sl.py @@ -0,0 +1,30 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20604 +name = 'Najpogostejše' + +description = '''\ +

+Napišite funkcijo most_frequent(s), ki vrne besedo in znak, +ki se v podanem nizu največkrat pojavita. V nizu +'in to in ono in to smo mi' se največkrat pojavita beseda +'in' in znak ' ' (presledek). +

+>>> most_frequent('aa bb aa')
+('aa', 'a')
+>>> most_frequent('in to in ono in to smo mi')
+('in', ' ')
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/show_letters/common.py b/python/problems/dictionaries/show_letters/common.py new file mode 100644 index 0000000..f69565a --- /dev/null +++ b/python/problems/dictionaries/show_letters/common.py @@ -0,0 +1,67 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20602 +number = 1 +visible = True + +solution = '''\ +def show_letters(word, letters): + bes = "" + for c in word: + bes += c if c in letters else "." + return bes +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'show_letters' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (('PONUDNIK', set(['O', 'N'])), '.ON..N..'), + (('PONUDNIK', set(['O', 'I', 'K'])), '.O....IK'), + (('PONUDNIK', set()), '........'), + (('PONUDNIK', set(['P', 'O', 'N', 'I', 'K', 'U'])), 'PONU.NIK'), + (('PONUDNIK', set(['P', 'O', 'N', 'I', 'K', 'U', 'D', 'Z'])), 'PONUDNIK'), + (('PONUDNIK', set(['A', 'B', 'J', 'L'])), '........'), + (('', set(['A', 'B', 'J', 'L'])), '') + ] + + test_in = [('{0}{1}'.format(func_name, str(l[0])), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += ans[0] == to + if ans[0] != to: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/show_letters/en.py b/python/problems/dictionaries/show_letters/en.py new file mode 100644 index 0000000..bf9cc0e --- /dev/null +++ b/python/problems/dictionaries/show_letters/en.py @@ -0,0 +1,13 @@ +id = 20602 +name = 'Show Letters' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/show_letters/sl.py b/python/problems/dictionaries/show_letters/sl.py new file mode 100644 index 0000000..7bf8a34 --- /dev/null +++ b/python/problems/dictionaries/show_letters/sl.py @@ -0,0 +1,33 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20602 +name = 'Pokaži črke' + +description = '''\ +

+Napiši funkcijo show_letters(word, letter), ki kot argument sprejme +besedo in množico (set) črk. Funkcija mora vrniti besedo, v +kateri so vse črke, ki ne nastopajo v množici crke, spremenjene v pike. Lahko +predpostaviš, da bodo vse besede sestavljene le iz velikih črk.

+
+>>> show_letters("PONUDNIK", set(["O", "N"]))
+'.ON..N..'
+>>> show_letters("PONUDNIK", set(["O", "I", "K"]))
+'.O....IK'
+>>> show_letters("PONUDNIK", set())
+'........'
+>>> show_letters("PONUDNIK", set(["P", "O", "N", "I", "K", "U"]))
+'PONU.NIK'
+
+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/sl.py b/python/problems/dictionaries/sl.py new file mode 100644 index 0000000..416c265 --- /dev/null +++ b/python/problems/dictionaries/sl.py @@ -0,0 +1,3 @@ +name = 'Slovarji' +description = 'Uporaba slovarjev in množic.' + diff --git a/python/problems/dictionaries/successors/common.py b/python/problems/dictionaries/successors/common.py new file mode 100644 index 0000000..5f824f5 --- /dev/null +++ b/python/problems/dictionaries/successors/common.py @@ -0,0 +1,82 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20609 +number = 10 +visible = True + +solution = '''\ +def children(tree, name): + if name in tree: + return tree[name] + return [] + +def successors(tree, name): + names = [] + for child in children(tree, name): + names.append(child) + names.extend(successors(tree, child)) + return names +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'successors' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + tree = { + 'alice': ['mary', 'tom', 'judy'], + 'bob': ['mary', 'tom', 'judy'], + 'ken': ['suzan'], + 'renee': ['rob', 'bob'], + 'rob': ['jim'], + 'sid': ['rob', 'bob'], + 'tom': ['ken']} + + in_out = [ + ((tree, 'tom'), ['ken', 'suzan']), + ((tree, 'sid'), ['rob', 'bob', 'jim', 'mary', + 'tom', 'judy', 'ken', 'suzan']), + ((tree, 'suzan'), []), + ((tree, 'ken'), ['suzan']), + ((tree, 'rob'), ['jim']), +] + + test_in = [(func_name+'%s'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = set(ans[0]) == set(to) and len(ans[0]) == len(to) + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/successors/en.py b/python/problems/dictionaries/successors/en.py new file mode 100644 index 0000000..fb7c662 --- /dev/null +++ b/python/problems/dictionaries/successors/en.py @@ -0,0 +1,13 @@ +id = 20609 +name = 'Successors (family tree 4)' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/successors/sl.py b/python/problems/dictionaries/successors/sl.py new file mode 100644 index 0000000..e92e2d7 --- /dev/null +++ b/python/problems/dictionaries/successors/sl.py @@ -0,0 +1,35 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20609 +name = 'Nasledniki (družinsko drevo 4)' + +description = '''\ +

+Napišite funkcijo successors(tree, name), +ki vrne seznam vseh naslednikov osebe. +

+ +

+Družinsko drevo tree je slovar, kjer ključi predstavljajo imena +staršev, vrednosti pa so njihovi otroci. Primer: +

+>>> tree = {'renee': ['rob', 'bob'], 'ken': ['suzan'], 'rob': ['jim'],
+            'sid': ['rob', 'bob'], ... , 'bob': ['mary', 'tom', 'judy']}
+>>> successors(tree, 'tom')
+['ken', 'suzan']
+>>> successors(tree, 'sid')
+['rob', 'bob', 'jim', 'mary', 'tom', 'judy', 'ken', 'suzan']
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/text/common.py b/python/problems/dictionaries/text/common.py new file mode 100644 index 0000000..134f69b --- /dev/null +++ b/python/problems/dictionaries/text/common.py @@ -0,0 +1,86 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20612 +number = 13 +visible = True + +solution = '''\ +import collections + +def following_words(txt): + words = txt.split() + freq = collections.defaultdict(list) + for word, next_word in zip(words, words[1:]): + freq[word].append(next_word) + return freq + +def freq_following_word(txt): + following = following_words(txt) + for f in following: + vals = collections.Counter(following[f]) + s = sorted(vals.most_common(), key = lambda x: (-x[1], x[0])) + following[f] = s[0][0] + return following + +def text(word, full_text, num): + freq = freq_following_word(full_text) + words = [] + for i in range(num): + words.append(word) + word = freq[word] + return ' '.join(words) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'text' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (('in', 'in in in in', 5), 'in in in in in'), + (('in', 'in to in ono in to smo mi', 5), 'in to in to in'), + (('danes', 'danes je lep dan danes sije sonce', 5), + 'danes je lep dan danes'), + (('danes', 'danes je lep dan danes sije sonce danes sije dan ki je sonce', 5), + 'danes sije dan danes sije'), + ] + + test_in = [(func_name+'%s'%str(l[0]), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + corr = ans[0] == to + n_correct += corr + if not corr: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/text/en.py b/python/problems/dictionaries/text/en.py new file mode 100644 index 0000000..4e5f5b3 --- /dev/null +++ b/python/problems/dictionaries/text/en.py @@ -0,0 +1,13 @@ +id = 20612 +name = 'Generated text' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} \ No newline at end of file diff --git a/python/problems/dictionaries/text/sl.py b/python/problems/dictionaries/text/sl.py new file mode 100644 index 0000000..3afeec4 --- /dev/null +++ b/python/problems/dictionaries/text/sl.py @@ -0,0 +1,45 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20612 +name = 'Generirano besedilo' + +description = '''\ +

+Napisati želimo program, ki bo generiral tipičen stavek. Seveda ni dobro, +da si samo naključno izbiramo besede in jih lepimo skupaj, saj bi tako dobili +nekaj povsem neberljivega. Naloge se bomo lotili malo pametneje. +Recimo, da ima program na voljo nek tekst, npr. 'in to in ono smo mi', +iz katerega se lahko uči. Naš tekst bomo začeli z izbrano besedo. +Nadaljujemo tako, da se vprašamo katera beseda se v učnem tekstu pojavi +najpogosteje za izbrano besedo. Če začnemo z besedo to, potem +bo naslednja beseda in. Postopek nato ponovimo z besedo in. +

+ +

+Napišite funkcijo text(word, full_text, num), ki sprejme začetno +besedo word, celotno besedilo full_text, +ter generira besedilo dolgo num besed. +

+ +

Da bodo generirani stavki bolj zanimivi, lahko program testiraš na +kakšnem romanu, npr. Orwellovi noveli 1984. Vendar pa tega ne boš mogel +izvajati v CodeQ, saj nima dostopa do mreže. Poženi iz kakšnega drugega programa, +npr. iz pyCharma ali kar iz ukazne vrstice. +

+>>> import urllib.request
+>>> txt = urllib.request.urlopen('http://squeeb1134.tripod.com/1984.txt').read().decode('utf8')
+>>> text('Big', txt, 15)
+'Big Brother is not be a few minutes at the Party member of the Party'
+
+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/text/tmp.py b/python/problems/dictionaries/text/tmp.py new file mode 100644 index 0000000..bf56c4f --- /dev/null +++ b/python/problems/dictionaries/text/tmp.py @@ -0,0 +1,29 @@ +import collections + +def following_words(txt): + words = txt.split() + freq = collections.defaultdict(list) + for word, next_word in zip(words, words[1:]): + freq[word].append(next_word) + return freq + +def freq_following_word(txt): + following = following_words(txt) + for f in following: + vals = collections.Counter(following[f]) + s = sorted(vals.most_common(), key = lambda x: (-x[1], x[0])) + following[f] = s[0][0] + return following + +def text(word, freq, num): + words = [] + for i in range(num): + words.append(word) + word = freq[word] + return ' '.join(words) + + +import urllib.request +txt = 'danes je lep dan danes sije sonce danes sije dan ki je sonce' +#urllib.request.urlopen('http://squeeb1134.tripod.com/1984.txt').read().decode('utf8') +print (text('danes', freq_following_word(txt), 5)) diff --git a/python/problems/dictionaries/transactions/common.py b/python/problems/dictionaries/transactions/common.py new file mode 100644 index 0000000..66d6387 --- /dev/null +++ b/python/problems/dictionaries/transactions/common.py @@ -0,0 +1,71 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20605 +number = 4 +visible = True + +solution = '''\ +def transactions(start, trans): + state = dict(start) + for fr, to, amount in trans: + state[fr] -= amount + state[to] += amount + most = None # could use -1, however all can be in debt + for who, amount in state.items(): + if most is None or amount > most: + most, richest = amount, who + return richest +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def test(python, code): + func_name = 'transactions' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + (([("Ana", 0), ("Berta", 1)], [("Berta", "Ana", 1)]), "Ana"), + (([("Ana", 0), ("Berta", 1)], []), "Berta"), + (([("Ana", 3), ("Berta", 2)], [("Berta", "Berta", 3)]), "Ana"), + (([("Ana", 4), ("Berta", 8), ("Cilka", 10)], + [("Cilka", "Ana", 3), ("Cilka", "Ana", 2), ("Ana", "Berta", 2)]), + "Berta"), + ] + + test_in = [('{0}{1}'.format(func_name, str(l[0])), None) for l in in_out] + test_out = [l[1] for l in in_out] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += ans[0] == to + if ans[0] != to: + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/transactions/en.py b/python/problems/dictionaries/transactions/en.py new file mode 100644 index 0000000..f60bc08 --- /dev/null +++ b/python/problems/dictionaries/transactions/en.py @@ -0,0 +1,13 @@ +id = 20605 +name = 'Transactions' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/transactions/sl.py b/python/problems/dictionaries/transactions/sl.py new file mode 100644 index 0000000..b80a688 --- /dev/null +++ b/python/problems/dictionaries/transactions/sl.py @@ -0,0 +1,41 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20605 +name = 'Transakcije' + +description = '''\ +

+V začetku je imela Ana štiri zlatnike, Berta 8 in CIlka 10, torej +[('Ana', 4), ('Berta', 8), ('Cilka', 10)]. Nato je dala Cilka +Ani 3 zlatnike; potem je dala Cilka Ani še 2; na koncu je dala Ana Berti 2, +kar zapišemo [('Cilka', 'Ana', 3), ('Cilka', 'Ana', 2), ('Ana', 'Berta', 2)]. +Kdo ima na koncu največ? +

+ +

+Napišite funkcijo transactions(start, tr), ki dobi gornja seznama in +vrne ime najbogatejše osebe po koncu transakcij. Če je na koncu več enako +bogatih najbogatejših oseb, naj vrne poljubno izmed njih. Namig: delo +si boste poenostavili, če bo funkcija takoj pretvorila sezmam v +primernejšo podatkovno strukturo. +

+

+Primer +

+>>> transakcije([('Ana', 4), ('Berta', 8), ('Cilka', 10)], [('Cilka', 'Ana', 3),
+('Cilka', 'Ana', 2), ('Ana', 'Berta', 2)])
+Berta
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} diff --git a/python/problems/dictionaries/waiter/common.py b/python/problems/dictionaries/waiter/common.py new file mode 100644 index 0000000..bcda759 --- /dev/null +++ b/python/problems/dictionaries/waiter/common.py @@ -0,0 +1,81 @@ +import re +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint + +id = 20606 +number = 5 +visible = True + +solution = '''\ +import collections +def orders(s): + cust = collections.defaultdict(list) + for pers, snack in s: + if snack[0] == "-": + if snack[1:] in cust[pers]: + cust[pers].remove(snack[1:]) + else: + cust[pers].append(snack) + return dict(cust) +''' + +hint_type = { + 'final_hint': Hint('final_hint') +} + +def comp(solution, test): + if not any(test.values()) and dict(solution) == {}: + return True + return dict(solution) == test + +def test(python, code): + func_name = 'orders' + tokens = get_tokens(code) + if not has_token_sequence(tokens, ['def', func_name]): + return False, [{'id' : 'no_func_name', 'args' : {'func_name' : func_name}}] + + in_out = [ + ([("Tone", "pivo")], {"Tone": ["pivo"]}), + ([("Tone", "pizza"), ("Tone", "sok")], {"Tone": ["pizza", "sok"]}), + ([('Ana', 'torta'), ('Berta', 'krof'), ('Cilka', 'kava'), + ('Ana', 'kava'), ('Berta', '-krof'), ('Cilka', '-torta'), + ('Berta', 'torta')], + {'Cilka': ['kava'], 'Berta': ['torta'], 'Ana': ['torta', 'kava']}), + ] + in_out_empty = [ + ([('Ana', 'torta'), ('Ana', '-torta')], {'Ana': []}), + ([('Ana', '-torta')], {'Ana': []}) + ] + + test_in = [('{0}({1})'.format(func_name, str(l[0])), None) + for l in in_out+in_out_empty] + test_out = [l[1] for l in in_out+in_out_empty] + + answers = python(code=code, inputs=test_in, timeout=1.0) + n_correct = 0 + tin, tout = None, None + for i, (ans, to) in enumerate(zip(answers, test_out)): + n_correct += comp(ans[0], to) + if not comp(ans[0], to): + tin = test_in[i][0] + tout = to + + passed = n_correct == len(test_in) + 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)}}) + if passed: + hints.append({'id': 'final_hint'}) + return passed, hints + + +def hint(python, code): + tokens = get_tokens(code) + + # run one test first to see if there are any exceptions + answer = python(code=code, inputs=[(None, None)], timeout=1.0) + exc = get_exception_desc(answer[0][3]) + if exc: return exc + + return None diff --git a/python/problems/dictionaries/waiter/en.py b/python/problems/dictionaries/waiter/en.py new file mode 100644 index 0000000..7f8528b --- /dev/null +++ b/python/problems/dictionaries/waiter/en.py @@ -0,0 +1,13 @@ +id = 20606 +name = 'Waiter' + +description = '''\ +

(translation missing)

''' + +hint = { + 'plan': '''\ +

(translation missing)

''', + + 'no_input_call': '''\ +

(translation missing)

''', +} diff --git a/python/problems/dictionaries/waiter/sl.py b/python/problems/dictionaries/waiter/sl.py new file mode 100644 index 0000000..d2bd50b --- /dev/null +++ b/python/problems/dictionaries/waiter/sl.py @@ -0,0 +1,50 @@ +import server +mod = server.problems.load_language('python', 'sl') + + +id = 20606 +name = 'Natakar' + +description = '''\ +

+Ko je prišel natakar, so naročile: +

+

+Vse skupaj zapišemo takole: [("Ana", "torta"), ("Berta", "krof"), +("Cilka", "kava"), ("Ana", "kava"), ("Berta", "-krof"), ("Cilka", "-torta"), +("Berta", "torta")]. Seznam torej vsebuje pare nizov (oseba, jed), +pri čemer se jed včasih začne z "-", kar pomeni, da stranka prekliče naročilo te +jedi oz. pijače.

+ +

Napiši funkcijo orders(s), ki prejme takšen seznam in vrne +slovar, katerega ključi so imena strank, vrednost pri vsakem ključu pa je +seznam vsega, kar mora stranka na koncu dobiti.

+

Primer +

+>>> orders([('Ana', 'torta'), ('Berta', 'krof'), ('Cilka', 'kava'), ('Ana', 'kava'),
+('Berta', '-krof'), ('Cilka', '-torta'), ('Berta', 'torta')])
+{'Cilka': ['kava'], 'Berta': ['torta'], 'Ana': ['torta', 'kava']}
+>>> orders([('Ana', 'torta'), ('Ana', '-torta')])
+{'Ana': []}
+>>> orders([('Ana', '-torta')])
+{'Ana': []} # Tu sme funkcija vrniti tudi prazen slovar, {}
+
+

+''' + +plan = [] + +hint = { + 'final_hint': ['''\ +

Program je pravilen!
+

+'''], +} -- cgit v1.2.1