summaryrefslogtreecommitdiff
path: root/python/problems/dictionaries/most_frequent/common.py
blob: a945f1d58dc4c8109840d76f295deeff027a4fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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