timeit

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

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

Решение на Аритметични изрази от Виктор Драганов

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

Към профила на Виктор Драганов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 15 успешни тест(а)
  • 4 неуспешни тест(а)

Код

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class ExpressionOperand:

    def _check(self, op, other):
        if type(self) is Expression:
            return create_expression(self, op, other)
        if type(othe) is Expression:
            return create_expression(self, op, other)

    def __add__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((self, op, other))

    def __radd__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((other, op, self))

    def __sub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((self, op, other))

    def __rsub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((other, op, self))

    def __mul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((self, op, other))

    def __rmul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((other, op, self))

    def __truediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((self, op, other))

    def __rtruediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((other, op, self))

    def __floordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((self, op, other))

    def __rfloordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((other, op, self))

    def __mod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((self, op, other))

    def __rmod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((other, op, self))

    def __divmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((self, op, other))

    def __rdivmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((other, op, self))

    def __pow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((self, op, other))

    def __rpow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((other, op, self))

    def __lshift__(self, other):
        op = create_operator('<<', lambda l, r: l << r)
        return create_expression((self, op, other))

    def __rshift__(self, other):
        op = create_operator('>>', lambda l, r: l >> r)
        return create_expression((self, op, other))

    def __and__(self, other):
        op = create_operator('&', lambda l, r: l & r)
        return create_expression((self, op, other))

    def __xor__(self, other):
        op = create_operator('^', lambda l, r: l ^ r)
        return create_expression((self, op, other))

    def __or__(self, other):
        op = create_operator('|', lambda l, r: l | r)
        return create_expression((self, op, other))


class Variable(ExpressionOperand):

    def __init__(self, name):
        self._name = name

    def name(self):
        return self._name

    def evaluate(self, **variable):
        return variable[self._name]

    def __str__(self):
        return self._name


class Constant(ExpressionOperand):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def __str__(self):
        return self._symbol


class Expression(ExpressionOperand):

    def __init__(self, expression_structure):
        self.variable_names = set()
        self.l = expression_structure[0]
        self.r = expression_structure[2]
        self._op = expression_structure[1]

        if type(self.l) is Constant:
            self.l = self.l.evaluate()
        elif type(self.l) is Variable:
            self.variable_names.add(self.l.name())
        elif type(self.l) is Expression:
            self.variable_names |= self.l.variable_names

        if type(self.r) is Constant:
            self.r = self.r.evaluate()
        elif type(self.r) is Variable:
            self.variable_names.add(self.r.name())
        elif hasattr(self.r, '__iter__'):
            self.r = create_expression(self.r)
        elif type(self.r) is Expression:
            self.variable_names |= self.r.variable_names

    def evaluate(self, **variables):
        if type(self.l) is Variable:
            l_arg = variables[self.l.name()]
        elif type(self.l) is Expression:
            l_arg = self.l.evaluate(**variables)
        else:
            l_arg = self.l

        if type(self.r) is Variable:
            r_arg = variables[self.r.name()]
        elif type(self.r) is Expression:
            r_arg = self.r.evaluate(**variables)
        else:
            r_arg = self.r

        return self._op._function(l_arg, r_arg)

    def __str__(self):
        return '({0} {1} {2})'.format(self.l, self._op, self.r)


def create_variable(name):
    return Variable(name)


def create_constant(value):
    return Constant(value)


def create_operator(symbol, function):
    return Operator(symbol, function)


def create_expression(expression_structure):
    return Expression(expression_structure)

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

........EEFE.......
======================================================================
ERROR: test_both_nested_operands (test.TestExpressionObjects)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: can only concatenate tuple (not "int") to tuple

======================================================================
ERROR: test_deep_expression (test.TestExpressionObjects)
----------------------------------------------------------------------
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 -: 'tuple' and 'int'

======================================================================
ERROR: test_evaluate_with_extra_kwargs (test.TestExpressionObjects)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: can only concatenate tuple (not "int") to tuple

======================================================================
FAIL: test_deep_string_conversion (test.TestExpressionObjects)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: '(((<solution.Variable object at 0x7f30da0c36[433 chars]3)))' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
- (((<solution.Variable object at 0x7f30da0c36d8>, <solution.Operator object at 0x7f30da0c3860>, <solution.Constant object at 0x7f30da0c3780>), <solution.Operator object at 0x7f30da0c3828>, (<solution.Constant object at 0x7f30da0c37b8>, <solution.Operator object at 0x7f30da0c3860>, <solution.Variable object at 0x7f30da0c3710>)) * ((<solution.Variable object at 0x7f30da0c3710>, <solution.Operator object at 0x7f30da0c3898>, <solution.Constant object at 0x7f30da0c3748>) - (5 + 3)))
+ (((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))


----------------------------------------------------------------------
Ran 19 tests in 0.119s

FAILED (failures=1, errors=3)

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

Виктор обнови решението на 21.03.2016 16:17 (преди над 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class ExpressionOperand:

    def _check(self, op, other):
        if type(self) is Expression:
            return create_expression(self, op, other)
        if type(othe) is Expression:
            return create_expression(self, op, other)

    def __add__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((self, op, other))

    def __radd__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((other, op, self))

    def __sub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((self, op, other))

    def __rsub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((other, op, self))

    def __mul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((self, op, other))

    def __rmul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((other, op, self))

    def __truediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((self, op, other))

    def __rtruediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((other, op, self))

    def __floordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((self, op, other))

    def __rfloordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((other, op, self))

    def __mod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((self, op, other))

    def __rmod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((other, op, self))

    def __divmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((self, op, other))

    def __rdivmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((other, op, self))

    def __pow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((self, op, other))

    def __rpow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((other, op, self))

    def __lshift__(self, other):
        op = create_operator('<<', lambda l, r: l << r)
        return create_expression((self, op, other))

    def __rshift__(self, other):
        op = create_operator('>>', lambda l, r: l >> r)
        return create_expression((self, op, other))

    def __and__(self, other):
        op = create_operator('&', lambda l, r: l & r)
        return create_expression((self, op, other))

    def __xor__(self, other):
        op = create_operator('^', lambda l, r: l ^ r)
        return create_expression((self, op, other))

    def __or__(self, other):
        op = create_operator('|', lambda l, r: l | r)
        return create_expression((self, op, other))


class Variable(ExpressionOperand):

    def __init__(self, name):
        self._name = name

    def name(self):
        return self._name

    def evaluate(self, **variable):
        return variable[self._name]

    def __str__(self):
        return self._name


class Constant(ExpressionOperand):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def __str__(self):
        return self._symbol


class Expression(ExpressionOperand):

    def __init__(self, expression_structure):
        self._variables = {}
        self.variable_names = set()
        self.l = expression_structure[0]
        self.r = expression_structure[2]
        self._op = expression_structure[1]

        if type(self.l) is Constant:
            self.l = self.l.evaluate()
        elif type(self.l) is Variable:
            self._variables[self.l.name()] = None
            self.variable_names.add(self.l.name())
        elif hasattr(self.l, '__iter__'):
            self.l = create_expression(self.l)
        elif type(self.l) is Expression:
            self.variable_names |= self.l.variable_names

        if type(self.r) is Constant:
            self.r = self.r.evaluate()
        elif type(self.r) is Variable:
            self._variables[self.r.name()] = None
            self.variable_names.add(self.r.name())
        elif hasattr(self.r, '__iter__'):
            self.r = create_expression(self.r)
        elif type(self.r) is Expression:
            self.variable_names |= self.r.variable_names

    def evaluate(self, **variables):
        if type(self.l) is Variable:
            l_arg = variables[self.l.name()]
        elif type(self.l) is Expression:
            l_arg = self.l.evaluate(**variables)
        else:
            l_arg = self.l

        if type(self.r) is Variable:
            r_arg = variables[self.r.name()]
        elif type(self.r) is Expression:
            r_arg = self.r.evaluate(**variables)
        else:
            r_arg = self.r

        return self._op._function(l_arg, r_arg)

    def __str__(self):
        return '({0} {1} {2})'.format(self.l, self._op, self.r)


def create_variable(name):
    return Variable(name)


def create_constant(value):
    return Constant(value)


def create_operator(symbol, function):
    return Operator(symbol, function)


def create_expression(expression_structure):
    return Expression(expression_structure)

Виктор обнови решението на 21.03.2016 16:20 (преди над 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class ExpressionOperand:

    def _check(self, op, other):
        if type(self) is Expression:
            return create_expression(self, op, other)
        if type(othe) is Expression:
            return create_expression(self, op, other)

    def __add__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((self, op, other))

    def __radd__(self, other):
        op = create_operator('+', lambda l, r: l + r)
        return create_expression((other, op, self))

    def __sub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((self, op, other))

    def __rsub__(self, other):
        op = create_operator('-', lambda l, r: l - r)
        return create_expression((other, op, self))

    def __mul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((self, op, other))

    def __rmul__(self, other):
        op = create_operator('*', lambda l, r: l * r)
        return create_expression((other, op, self))

    def __truediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((self, op, other))

    def __rtruediv__(self, other):
        op = create_operator('/', lambda l, r: l / r)
        return create_expression((other, op, self))

    def __floordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((self, op, other))

    def __rfloordiv__(self, other):
        op = create_operator('//', lambda l, r: l // r)
        return create_expression((other, op, self))

    def __mod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((self, op, other))

    def __rmod__(self, other):
        op = create_operator('%', lambda l, r: l % r)
        return create_expression((other, op, self))

    def __divmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((self, op, other))

    def __rdivmod__(self, other):
        op = create_operator('divmod', lambda l, r: (l // r, l % r))
        return create_expression((other, op, self))

    def __pow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((self, op, other))

    def __rpow__(self, other):
        op = create_operator('**', lambda l, r: l ** r)
        return create_expression((other, op, self))

    def __lshift__(self, other):
        op = create_operator('<<', lambda l, r: l << r)
        return create_expression((self, op, other))

    def __rshift__(self, other):
        op = create_operator('>>', lambda l, r: l >> r)
        return create_expression((self, op, other))

    def __and__(self, other):
        op = create_operator('&', lambda l, r: l & r)
        return create_expression((self, op, other))

    def __xor__(self, other):
        op = create_operator('^', lambda l, r: l ^ r)
        return create_expression((self, op, other))

    def __or__(self, other):
        op = create_operator('|', lambda l, r: l | r)
        return create_expression((self, op, other))


class Variable(ExpressionOperand):

    def __init__(self, name):
        self._name = name

    def name(self):
        return self._name

    def evaluate(self, **variable):
        return variable[self._name]

    def __str__(self):
        return self._name


class Constant(ExpressionOperand):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def __str__(self):
        return self._symbol


class Expression(ExpressionOperand):

    def __init__(self, expression_structure):
        self.variable_names = set()
        self.l = expression_structure[0]
        self.r = expression_structure[2]
        self._op = expression_structure[1]

        if type(self.l) is Constant:
            self.l = self.l.evaluate()
        elif type(self.l) is Variable:
            self.variable_names.add(self.l.name())
        elif type(self.l) is Expression:
            self.variable_names |= self.l.variable_names

        if type(self.r) is Constant:
            self.r = self.r.evaluate()
        elif type(self.r) is Variable:
            self.variable_names.add(self.r.name())
        elif hasattr(self.r, '__iter__'):
            self.r = create_expression(self.r)
        elif type(self.r) is Expression:
            self.variable_names |= self.r.variable_names

    def evaluate(self, **variables):
        if type(self.l) is Variable:
            l_arg = variables[self.l.name()]
        elif type(self.l) is Expression:
            l_arg = self.l.evaluate(**variables)
        else:
            l_arg = self.l

        if type(self.r) is Variable:
            r_arg = variables[self.r.name()]
        elif type(self.r) is Expression:
            r_arg = self.r.evaluate(**variables)
        else:
            r_arg = self.r

        return self._op._function(l_arg, r_arg)

    def __str__(self):
        return '({0} {1} {2})'.format(self.l, self._op, self.r)


def create_variable(name):
    return Variable(name)


def create_constant(value):
    return Constant(value)


def create_operator(symbol, function):
    return Operator(symbol, function)


def create_expression(expression_structure):
    return Expression(expression_structure)