timeit

Програмиране с Python

Курс във Факултета по Математика и Информатика към СУ

Решение на Аритметични изрази от Евгени Кунев

Обратно към всички решения

Към профила на Евгени Кунев

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 17 успешни тест(а)
  • 2 неуспешни тест(а)

Код

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import operator
from numbers import Number


class Operator:
    def __init__(self, symbol, function):
        self.symbol = symbol
        self.function = function

    def __str__(self):
        return self.symbol


def ensure_node(obj):
    if isinstance(obj, Number):
        obj = Constant(obj)

    return obj


def ensure_node_arg(func):
    def ensured_node_arg(self, other):
        return func(self, ensure_node(other))

    return ensured_node_arg


class Node:
    @ensure_node_arg
    def __add__(self, rhs):
        return Expression(self, addition, rhs)

    @ensure_node_arg
    def __sub__(self, rhs):
        return Expression(self, subtraction, rhs)

    @ensure_node_arg
    def __mul__(self, rhs):
        return Expression(self, multiplication, rhs)

    @ensure_node_arg
    def __div__(self, rhs):
        return Expression(self, division, rhs)

    @ensure_node_arg
    def __radd__(self, lhs):
        return Expression(self, addition, lhs)

    @ensure_node_arg
    def __rsub__(self, lhs):
        return Expression(self, subtraction, lhs)

    @ensure_node_arg
    def __rmul__(self, lhs):
        return Expression(self, multiplication, lhs)

    @ensure_node_arg
    def __rdiv__(self, lhs):
        return Expression(self, division, lhs)


class Variable(Node):
    def __init__(self, name):
        self.name = name

    def evaluate(self, **variables):
        return variables[self.name]

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


class Constant(Node):
    def __init__(self, value):
        self.value = value

    def evaluate(self, **variables):
        return self.value

    def variable_names(self):
        return ()

    def __str__(self):
        return str(self.value)


class Expression(Node):
    def __init__(self, lhs, op, rhs):
        self.lhs = lhs
        self.op = op
        self.rhs = rhs

    def evaluate(self, **variables):
        lhs_value = self.lhs.evaluate(**variables)
        rhs_value = self.rhs.evaluate(**variables)
        return self.op.function(lhs_value, rhs_value)

    @property
    def variable_names(self):
        return self.lhs.variable_names() + self.rhs.variable_names()

    def __str__(self):
        return '({} {} {})'.format(self.lhs, self.op, self.rhs)


def create_expression(expression):
    lhs, op, rhs = expression
    lhs, rhs = ensure_node(lhs), ensure_node(rhs)
    if not isinstance(lhs, Node):
        lhs = create_expression(lhs)
    if not isinstance(rhs, Node):
        rhs = create_expression(rhs)

    return Expression(lhs, op, rhs)


addition = Operator('+', operator.add)
subtraction = Operator('-', operator.sub)
multiplication = Operator('*', operator.mul)
division = Operator('/', operator.truediv)


create_operator = Operator
create_variable = Variable
create_constant = Constant

Лог от изпълнението

....F...........E..
======================================================================
ERROR: test_variables_and_constants_extra_operators (test.TestNativeOperators)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: unsupported operand type(s) for ** or pow(): 'Variable' and 'Variable'

======================================================================
FAIL: test_string_conversion_with_literals (test.TestArithmeticsWithLiterals)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: '((x + 5) - ((y * 4) * 3))' != '((x + 5) - ((4 * y) * 3))'
- ((x + 5) - ((y * 4) * 3))
?                 -----
+ ((x + 5) - ((4 * y) * 3))
?              ++++ +


----------------------------------------------------------------------
Ran 19 tests in 0.134s

FAILED (failures=1, errors=1)

История (2 версии и 2 коментара)

Евгени обнови решението на 17.03.2016 00:01 (преди над 1 година)

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from numbers import Number


class Operator:
    def __init__(self, symbol, function):
        self.symbol = symbol
        self.function = function

    def __str__(self):
        return self.symbol


def ensure_node(obj):
    if isinstance(obj, Number):
        obj = Constant(obj)

    return obj


def ensure_node_arg(func):
    def ensured_node_arg(self, other):
        return func(self, ensure_node(other))

    return ensured_node_arg


class Node:
    @ensure_node_arg
    def __add__(self, rhs):
        return Expression(self, addition, rhs)

    @ensure_node_arg
    def __sub__(self, rhs):
        return Expression(self, subtraction, rhs)

    @ensure_node_arg
    def __mul__(self, rhs):
        return Expression(self, multiplication, rhs)

    @ensure_node_arg
    def __div__(self, rhs):
        return Expression(self, division, rhs)

    @ensure_node_arg
    def __radd__(self, lhs):
        return Expression(self, addition, lhs)

    @ensure_node_arg
    def __rsub__(self, lhs):
        return Expression(self, subtraction, lhs)

    @ensure_node_arg
    def __rmul__(self, lhs):
        return Expression(self, multiplication, lhs)

    @ensure_node_arg
    def __rdiv__(self, lhs):
        return Expression(self, division, lhs)


class Variable(Node):
    def __init__(self, name):
        self.name = name

    def evaluate(self, **variables):
        return variables[self.name]

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


class Constant(Node):
    def __init__(self, value):
        self.value = value

    def evaluate(self, **variables):
        return self.value

    def variable_names(self):
        return ()

    def __str__(self):
        return str(self.value)


class Expression(Node):
    def __init__(self, lhs, op, rhs):
        self.lhs = lhs
        self.op = op
        self.rhs = rhs

    def evaluate(self, **variables):
        lhs_value = self.lhs.evaluate(**variables)
        rhs_value = self.rhs.evaluate(**variables)
        return self.op.function(lhs_value, rhs_value)

    @property
    def variable_names(self):
        return self.lhs.variable_names() + self.rhs.variable_names()

    def __str__(self):
        return '({} {} {})'.format(self.lhs, self.op, self.rhs)


def create_expression(expression):
    lhs, op, rhs = expression
    lhs, rhs = ensure_node(lhs), ensure_node(rhs)
    if not isinstance(lhs, Node):
        lhs = create_expression(lhs)
    if not isinstance(rhs, Node):
        rhs = create_expression(rhs)

    return Expression(lhs, op, rhs)


addition = Operator('+', lambda lhs, rhs: lhs + rhs)
subtraction = Operator('-', lambda lhs, rhs: lhs - rhs)
multiplication = Operator('*', lambda lhs, rhs: lhs * rhs)
division = Operator('/', lambda lhs, rhs: lhs / rhs)


create_operator = Operator
create_variable = Variable
create_constant = Constant

Евгени обнови решението на 17.03.2016 00:08 (преди над 1 година)

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import operator
from numbers import Number


class Operator:
    def __init__(self, symbol, function):
        self.symbol = symbol
        self.function = function

    def __str__(self):
        return self.symbol


def ensure_node(obj):
    if isinstance(obj, Number):
        obj = Constant(obj)

    return obj


def ensure_node_arg(func):
    def ensured_node_arg(self, other):
        return func(self, ensure_node(other))

    return ensured_node_arg


class Node:
    @ensure_node_arg
    def __add__(self, rhs):
        return Expression(self, addition, rhs)

    @ensure_node_arg
    def __sub__(self, rhs):
        return Expression(self, subtraction, rhs)

    @ensure_node_arg
    def __mul__(self, rhs):
        return Expression(self, multiplication, rhs)

    @ensure_node_arg
    def __div__(self, rhs):
        return Expression(self, division, rhs)

    @ensure_node_arg
    def __radd__(self, lhs):
        return Expression(self, addition, lhs)

    @ensure_node_arg
    def __rsub__(self, lhs):
        return Expression(self, subtraction, lhs)

    @ensure_node_arg
    def __rmul__(self, lhs):
        return Expression(self, multiplication, lhs)

    @ensure_node_arg
    def __rdiv__(self, lhs):
        return Expression(self, division, lhs)


class Variable(Node):
    def __init__(self, name):
        self.name = name

    def evaluate(self, **variables):
        return variables[self.name]

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


class Constant(Node):
    def __init__(self, value):
        self.value = value

    def evaluate(self, **variables):
        return self.value

    def variable_names(self):
        return ()

    def __str__(self):
        return str(self.value)


class Expression(Node):
    def __init__(self, lhs, op, rhs):
        self.lhs = lhs
        self.op = op
        self.rhs = rhs

    def evaluate(self, **variables):
        lhs_value = self.lhs.evaluate(**variables)
        rhs_value = self.rhs.evaluate(**variables)
        return self.op.function(lhs_value, rhs_value)

    @property
    def variable_names(self):
        return self.lhs.variable_names() + self.rhs.variable_names()

    def __str__(self):
        return '({} {} {})'.format(self.lhs, self.op, self.rhs)


def create_expression(expression):
    lhs, op, rhs = expression
    lhs, rhs = ensure_node(lhs), ensure_node(rhs)
    if not isinstance(lhs, Node):
        lhs = create_expression(lhs)
    if not isinstance(rhs, Node):
        rhs = create_expression(rhs)

    return Expression(lhs, op, rhs)


addition = Operator('+', operator.add)
subtraction = Operator('-', operator.sub)
multiplication = Operator('*', operator.mul)
division = Operator('/', operator.truediv)


create_operator = Operator
create_variable = Variable
create_constant = Constant