summaryrefslogtreecommitdiff
path: root/python/problems/introduction/fahrenheit_to_celsius/common.py
blob: 34f2832be287829184d99d21e016a2c2f6661e8e (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
# coding=utf-8

from python.util import has_token_sequence
from server.hints import Hint, HintSequence

id = 180
group = 'introduction'
number = 1
visible = True

solution = '''\
f = float(input("Temperatura [F]: "))
 c = 5/9 * (f – 32)
print("Temperatura je", c, "C")
'''

hint_type = {
    'plan': Hint('plan'),
    'no_input_call': Hint('no_input_call'),
}

def test(python, code):
    # List of inputs: (expression to eval, stdin).
    test_in = [
        (None, '0\n'),
        (None, '100\n'),
    ]
    test_out = [
        '-17.7',
        '37.7',
    ]

    # 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
    for output, correct in zip(outputs, test_out):
        if correct in output:
            n_correct += 1
    return n_correct, len(test_in)

def hint(python, code):
    if not code:
        return [{'id': 'plan'}]
    if not has_token_sequence(code, ['input']):
        return [{'id': 'no_input_call'}]
    return None