timeit

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

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

Решение на Аритметични изрази от Симеон Ролев

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

Към профила на Симеон Ролев

Резултати

  • 8 точки от тестове
  • 1 отнета точка
  • 7 точки общо
  • 16 успешни тест(а)
  • 3 неуспешни тест(а)

Код

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import collections
import functools


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

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

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

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        if isinstance(other, Expression):
            return create_expression((self, plus, other.exp_str))
        else:
            return create_expression((self, plus, other))

    def __sub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        if isinstance(other, Expression):
            return create_expression((self, minus, other.exp_str))
        else:
            return create_expression((self, minus, other))

    def __mul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        if isinstance(other, Expression):
            return create_expression((self, mult, other.exp_str))
        else:
            return create_expression((self, mult, other))

    def __truediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        if isinstance(other, Expression):
            return create_expression((self, div, other.exp_str))
        else:
            return create_expression((self, div, other))

    def __floordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        if isinstance(other, Expression):
            return create_expression((self, truediv, other.exp_str))
        else:
            return create_expression((self, truediv, other))

    def __radd__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, plus, other))

    def __rsub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        return create_expression((other, minus, self))

    def __rmul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        return create_expression((self, mult, other))

    def __rtruediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        return create_expression((other, div, self))

    def __rfloordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        return create_expression((other, truediv, self))

# ostanali operatori!

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        if isinstance(other, Expression):
            return create_expression((self, mod, other.exp_str))
        else:
            return create_expression((self, mod, other))

    def __rmod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((other, mod, self))

    def __pow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        if isinstance(other, Expression):
            return create_expression((self, pow, other.exp_str))
        else:
            return create_expression((self, pow, other))

    def __rpow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((other, pow, self))

    def __divmod__(self, other):
        mod = create_operator('divmod', lambda lhs, rhs: divmod(lhs, rhs))
        if isinstance(other, Expression):
            return create_expression((self, mod, other.exp_str))
        else:
            return create_expression((self, mod, other))

    def __rdivmod__(self, other):
        rdivmod = create_operator('rdivmod', lambda lhs, rhs: divmod(lhs, rhs))
        return create_expression((other, rdivmod, self))

    def __lshift__(self, other):
        lsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        if isinstance(other, Expression):
            return create_expression((self, lsh, other.exp_str))
        else:
            return create_expression((self, lsh, other))

    def __rlshift__(self, other):
        rlsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        return create_expression((other, rlsh, self))

    def __rshift__(self, other):
        rsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        if isinstance(other, Expression):
            return create_expression((self, rsh, other.exp_str))
        else:
            return create_expression((self, rsh, other))

    def __rrshift__(self, other):
        rrsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        return create_expression((other, rrsh, self))

    def __and__(self, other):
        and_op = create_operator('&', lambda lhs, rhs: lhs & rhs)
        if isinstance(other, Expression):
            return create_expression((self, and_op, other.exp_str))
        else:
            return create_expression((self, and_op, other))

    def __rand__(self, other):
        r_and = create_operator('&', lambda lhs, rhs: lhs & rhs)
        return create_expression((other, r_and, self))

    def __or__(self, other):
        or_op = create_operator('|', lambda lhs, rhs: lhs | rhs)
        if isinstance(other, Expression):
            return create_expression((self, or_op, other.exp_str))
        else:
            return create_expression((self, or_op, other))

    def __ror__(self, other):
        r_or = create_operator('|', lambda lhs, rhs: lhs | rhs)
        return create_expression((other, r_or, self))

    def __xor__(self, other):
        xor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        if isinstance(other, Expression):
            return create_expression((self, xor_op, other.exp_str))
        else:
            return create_expression((self, xor_op, other))

    def __rxor__(self, other):
        rxor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        return create_expression((other, rxor_op, self))


class Variable(Constant):
    def __init__(self, name):
        self.name = name
        self.value = None

    def evaluate(self, **kwargs):
        for var_name in kwargs.keys():
            if var_name == self.name:
                self.value = kwargs[var_name]
        return self.value

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name


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


class Expression:
    def __init__(self, expression_structure):
        self.exp_str = expression_structure
        self.expr1 = expression_structure[0]
        self.oper = expression_structure[1]
        if len(expression_structure[2:]) == 1:
            self.expr2 = expression_structure[2:][0]
        else:
            self.expr2 = expression_structure[2:]
        self.variable_names = self.take_vars()

    def take_vars(self):
        result = []
        for exp in (self.expr1, self.expr2):
            if isinstance(exp, Variable):
                result.append(exp.name)
            elif isinstance(exp, (list, tuple)):
                result.extend(create_expression(exp).take_vars())
        return result

    def all_variables(self):
        result = []
        for exp in (self.expr1, self.expr2):
            if isinstance(exp, Variable):
                result.append(exp)
            elif isinstance(exp, (list, tuple)):
                result.extend(create_expression(exp).all_variables())
        return result

    def evaluate(self, **kwargs):
        for var_name in kwargs.keys():
            if var_name in self.variable_names:
                for var in self.all_variables():
                    if var.name == var_name:
                        var.value = kwargs[var_name]

        if isinstance(self.expr1, (int, float, complex)):
            leftside = self.expr1
        else:
            leftside = create_expression(self.expr1).evaluate(**kwargs)
        if isinstance(self.expr2, (int, float, complex)):
            rightside = self.expr2
        else:
            rightside = create_expression(self.expr2).evaluate(**kwargs)

        return self.oper.function(leftside, rightside)

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, plus, other.exp_str))
        else:
            return create_expression((self.exp_str, plus, other))

    def __radd__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self.exp_str, plus, other))

    def __sub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, minus, other.exp_str))
        else:
            return create_expression((self.exp_str, minus, other))

    def __rsub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        return create_expression((other, minus, self.exp_str))

    def __mul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mult, other.exp_str))
        else:
            return create_expression((self.exp_str, mult, other))

    def __rmul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        return create_expression((self.exp_str, mult, other))

    def __truediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, div, other.exp_str))
        else:
            return create_expression((self.exp_str, div, other))

    def __rtruediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        return create_expression((other, div, self.exp_str))

    def __floordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, truediv, other.exp_str))
        else:
            return create_expression((self.exp_str, truediv, other))

    def __rfloordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        return create_expression((other, truediv, self.exp_str))

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mod, other.exp_str))
        else:
            return create_expression((self.exp_str, mod, other))

    def __rmod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((other, mod, self.exp_str))

    def __pow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, pow, other.exp_str))
        else:
            return create_expression((self.exp_str, pow, other))

    def __rpow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((other, pow, self.exp_str))

    def __divmod__(self, other):
        mod = create_operator('divmod', lambda lhs, rhs: divmod(lhs, rhs))
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mod, other.exp_str))
        else:
            return create_expression((self.exp_str, mod, other))

    def __rdivmod__(self, other):
        rdivmod = create_operator('rdivmod', lambda lhs, rhs: divmod(lhs, rhs))
        return create_expression((other, rdivmod, self.exp_str))

    def __lshift__(self, other):
        lsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, lsh, other.exp_str))
        else:
            return create_expression((self.exp_str, lsh, other))

    def __rlshift__(self, other):
        rlsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        return create_expression((other, rlsh, self.exp_str))

    def __rshift__(self, other):
        rsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, rsh, other.exp_str))
        else:
            return create_expression((self.exp_str, rsh, other))

    def __rrshift__(self, other):
        rrsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        return create_expression((other, rrsh, self.exp_str))

    def __and__(self, other):
        and_op = create_operator('&', lambda lhs, rhs: lhs & rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, and_op, other.exp_str))
        else:
            return create_expression((self.exp_str, and_op, other))

    def __rand__(self, other):
        r_and = create_operator('&', lambda lhs, rhs: lhs & rhs)
        return create_expression((other, r_and, self.exp_str))

    def __or__(self, other):
        or_op = create_operator('|', lambda lhs, rhs: lhs | rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, or_op, other.exp_str))
        else:
            return create_expression((self.exp_str, or_op, other))

    def __ror__(self, other):
        r_or = create_operator('|', lambda lhs, rhs: lhs | rhs)
        return create_expression((other, r_or, self.exp_str))

    def __xor__(self, other):
        xor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, xor_op, other.exp_str))
        else:
            return create_expression((self.exp_str, xor_op, other))

    def __rxor__(self, other):
        rxor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        return create_expression((other, rxor_op, self.exp_str))


def create_constant(value):
    const = Constant(value)
    return const


def create_variable(name):
    var = Variable(name)
    return var


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


def create_expression(exp_structure):
    if isinstance(exp_structure, (Variable, Constant, int, float, complex)):
        return exp_structure
    expr = Expression(exp_structure)
    return expr

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

....F.....F...F....
======================================================================
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: '<solution.Expression object at 0x7fcce7220a58>' != '((x + 5) - ((4 * y) * 3))'
- <solution.Expression object at 0x7fcce7220a58>
+ ((x + 5) - ((4 * y) * 3))


======================================================================
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.Expression object at 0x7fcce92019e8>' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
- <solution.Expression object at 0x7fcce92019e8>
+ (((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))


======================================================================
FAIL: test_simple_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.Expression object at 0x7fcce92019e8>' != '(x + 5)'
- <solution.Expression object at 0x7fcce92019e8>
+ (x + 5)


----------------------------------------------------------------------
Ran 19 tests in 0.121s

FAILED (failures=3)

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

Симеон обнови решението на 20.03.2016 20:43 (преди над 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import collections
import functools


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

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

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

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        if isinstance(other, Expression):
            return create_expression((self, plus, other.exp_str))
        else:
            return create_expression((self, plus, other))

    def __sub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        if isinstance(other, Expression):
            return create_expression((self, minus, other.exp_str))
        else:
            return create_expression((self, minus, other))

    def __mul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        if isinstance(other, Expression):
            return create_expression((self, mult, other.exp_str))
        else:
            return create_expression((self, mult, other))

    def __truediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        if isinstance(other, Expression):
            return create_expression((self, div, other.exp_str))
        else:
            return create_expression((self, div, other))

    def __floordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        if isinstance(other, Expression):
            return create_expression((self, truediv, other.exp_str))
        else:
            return create_expression((self, truediv, other))

    def __radd__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, plus, other))

    def __rsub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        return create_expression((other, minus, self))

    def __rmul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        return create_expression((self, mult, other))

    def __rtruediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        return create_expression((other, div, self))

    def __rfloordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        return create_expression((other, truediv, self))

# ostanali operatori!

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        if isinstance(other, Expression):
            return create_expression((self, mod, other.exp_str))
        else:
            return create_expression((self, mod, other))

    def __rmod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((other, mod, self))

    def __pow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        if isinstance(other, Expression):
            return create_expression((self, pow, other.exp_str))
        else:
            return create_expression((self, pow, other))

    def __rpow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((other, pow, self))

    def __divmod__(self, other):
        mod = create_operator('divmod', lambda lhs, rhs: divmod(lhs, rhs))
        if isinstance(other, Expression):
            return create_expression((self, mod, other.exp_str))
        else:
            return create_expression((self, mod, other))

    def __rdivmod__(self, other):
        rdivmod = create_operator('rdivmod', lambda lhs, rhs: divmod(lhs, rhs))
        return create_expression((other, rdivmod, self))

    def __lshift__(self, other):
        lsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        if isinstance(other, Expression):
            return create_expression((self, lsh, other.exp_str))
        else:
            return create_expression((self, lsh, other))

    def __rlshift__(self, other):
        rlsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        return create_expression((other, rlsh, self))

    def __rshift__(self, other):
        rsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        if isinstance(other, Expression):
            return create_expression((self, rsh, other.exp_str))
        else:
            return create_expression((self, rsh, other))

    def __rrshift__(self, other):
        rrsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        return create_expression((other, rrsh, self))

    def __and__(self, other):
        and_op = create_operator('&', lambda lhs, rhs: lhs & rhs)
        if isinstance(other, Expression):
            return create_expression((self, and_op, other.exp_str))
        else:
            return create_expression((self, and_op, other))

    def __rand__(self, other):
        r_and = create_operator('&', lambda lhs, rhs: lhs & rhs)
        return create_expression((other, r_and, self))

    def __or__(self, other):
        or_op = create_operator('|', lambda lhs, rhs: lhs | rhs)
        if isinstance(other, Expression):
            return create_expression((self, or_op, other.exp_str))
        else:
            return create_expression((self, or_op, other))

    def __ror__(self, other):
        r_or = create_operator('|', lambda lhs, rhs: lhs | rhs)
        return create_expression((other, r_or, self))

    def __xor__(self, other):
        xor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        if isinstance(other, Expression):
            return create_expression((self, xor_op, other.exp_str))
        else:
            return create_expression((self, xor_op, other))

    def __rxor__(self, other):
        rxor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        return create_expression((other, rxor_op, self))


class Variable(Constant):
    def __init__(self, name):
        self.name = name
        self.value = None

    def evaluate(self, **kwargs):
        for var_name in kwargs.keys():
            if var_name == self.name:
                self.value = kwargs[var_name]
        return self.value

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name


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


class Expression:
    def __init__(self, expression_structure):
        self.exp_str = expression_structure
        self.expr1 = expression_structure[0]
        self.oper = expression_structure[1]
        if len(expression_structure[2:]) == 1:
            self.expr2 = expression_structure[2:][0]
        else:
            self.expr2 = expression_structure[2:]
        self.variable_names = self.take_vars()

    def take_vars(self):
        result = []
        for exp in (self.expr1, self.expr2):
            if isinstance(exp, Variable):
                result.append(exp.name)
            elif isinstance(exp, (list, tuple)):
                result.extend(create_expression(exp).take_vars())
        return result

    def all_variables(self):
        result = []
        for exp in (self.expr1, self.expr2):
            if isinstance(exp, Variable):
                result.append(exp)
            elif isinstance(exp, (list, tuple)):
                result.extend(create_expression(exp).all_variables())
        return result

    def evaluate(self, **kwargs):
        for var_name in kwargs.keys():
            if var_name in self.variable_names:
                for var in self.all_variables():
                    if var.name == var_name:
                        var.value = kwargs[var_name]

        if isinstance(self.expr1, (int, float, complex)):
            leftside = self.expr1
        else:
            leftside = create_expression(self.expr1).evaluate(**kwargs)
        if isinstance(self.expr2, (int, float, complex)):
            rightside = self.expr2
        else:
            rightside = create_expression(self.expr2).evaluate(**kwargs)

        return self.oper.function(leftside, rightside)

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, plus, other.exp_str))
        else:
            return create_expression((self.exp_str, plus, other))

    def __radd__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self.exp_str, plus, other))

    def __sub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, minus, other.exp_str))
        else:
            return create_expression((self.exp_str, minus, other))

    def __rsub__(self, other):
        minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
        return create_expression((other, minus, self.exp_str))

    def __mul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mult, other.exp_str))
        else:
            return create_expression((self.exp_str, mult, other))

    def __rmul__(self, other):
        mult = create_operator('*', lambda lhs, rhs: lhs * rhs)
        return create_expression((self.exp_str, mult, other))

    def __truediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, div, other.exp_str))
        else:
            return create_expression((self.exp_str, div, other))

    def __rtruediv__(self, other):
        div = create_operator('/', lambda lhs, rhs: lhs / rhs)
        return create_expression((other, div, self.exp_str))

    def __floordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, truediv, other.exp_str))
        else:
            return create_expression((self.exp_str, truediv, other))

    def __rfloordiv__(self, other):
        truediv = create_operator('//', lambda lhs, rhs: lhs // rhs)
        return create_expression((other, truediv, self.exp_str))

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mod, other.exp_str))
        else:
            return create_expression((self.exp_str, mod, other))

    def __rmod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((other, mod, self.exp_str))

    def __pow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, pow, other.exp_str))
        else:
            return create_expression((self.exp_str, pow, other))

    def __rpow__(self, other):
        pow = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((other, pow, self.exp_str))

    def __divmod__(self, other):
        mod = create_operator('divmod', lambda lhs, rhs: divmod(lhs, rhs))
        if isinstance(other, Expression):
            return create_expression((self.exp_str, mod, other.exp_str))
        else:
            return create_expression((self.exp_str, mod, other))

    def __rdivmod__(self, other):
        rdivmod = create_operator('rdivmod', lambda lhs, rhs: divmod(lhs, rhs))
        return create_expression((other, rdivmod, self.exp_str))

    def __lshift__(self, other):
        lsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, lsh, other.exp_str))
        else:
            return create_expression((self.exp_str, lsh, other))

    def __rlshift__(self, other):
        rlsh = create_operator('<<', lambda lhs, rhs: lhs << rhs)
        return create_expression((other, rlsh, self.exp_str))

    def __rshift__(self, other):
        rsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, rsh, other.exp_str))
        else:
            return create_expression((self.exp_str, rsh, other))

    def __rrshift__(self, other):
        rrsh = create_operator('>>', lambda lhs, rhs: lhs >> rhs)
        return create_expression((other, rrsh, self.exp_str))

    def __and__(self, other):
        and_op = create_operator('&', lambda lhs, rhs: lhs & rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, and_op, other.exp_str))
        else:
            return create_expression((self.exp_str, and_op, other))

    def __rand__(self, other):
        r_and = create_operator('&', lambda lhs, rhs: lhs & rhs)
        return create_expression((other, r_and, self.exp_str))

    def __or__(self, other):
        or_op = create_operator('|', lambda lhs, rhs: lhs | rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, or_op, other.exp_str))
        else:
            return create_expression((self.exp_str, or_op, other))

    def __ror__(self, other):
        r_or = create_operator('|', lambda lhs, rhs: lhs | rhs)
        return create_expression((other, r_or, self.exp_str))

    def __xor__(self, other):
        xor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        if isinstance(other, Expression):
            return create_expression((self.exp_str, xor_op, other.exp_str))
        else:
            return create_expression((self.exp_str, xor_op, other))

    def __rxor__(self, other):
        rxor_op = create_operator('^', lambda lhs, rhs: lhs ^ rhs)
        return create_expression((other, rxor_op, self.exp_str))


def create_constant(value):
    const = Constant(value)
    return const


def create_variable(name):
    var = Variable(name)
    return var


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


def create_expression(exp_structure):
    if isinstance(exp_structure, (Variable, Constant, int, float, complex)):
        return exp_structure
    expr = Expression(exp_structure)
    return expr
  • Правиш един и същи if за почти всички оператори. Измисли как да го изнесеш на едно място, без да копираш код.
  • Едни и същи оператори са копирани в няколко класа. Добра идея е да изнесеш това в един базов клас.
  • Всички числа в езика наследяват от numbers.Number. Можеш да се възползваш от това в isinstance проверките си.
  • Не места дефинираш променлива само за да я върнеш на следващия ред, вместо просто да върнеш израза.
  • Expression.take_vars и Expression.all_variables са ми твърде еднакви.
  • Опитай да опростиш Expression.evaluate.