timeit

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

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

Решение на Аритметични изрази от Марина Георгиева

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

Към профила на Марина Георгиева

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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
import collections


class Operand:
    def __add__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((self, operator, other))

    def __radd__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((other, operator, self))

    def __sub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((self, operator, other))

    def __rsub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((other, operator, self))

    def __mul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((self, operator, other))

    def __rmul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((other, operator, self))

    def __truediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((self, operator, other))

    def __rtruediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((other, operator, self))

    def __floordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((self, operator, other))

    def __rfloordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((other, operator, self))

    def __mod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((self, operator, other))

    def __rmod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((other, operator, self))

    def __pow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((self, operator, other))

    def __rpow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((other, operator, self))

    def __lshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, operator, other))

    def __rlshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((other, operator, self))

    def __rshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, operator, other))

    def __rrshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((other, operator, self))

    def __and__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((self, operator, other))

    def __rand__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((other, operator, self))

    def __or__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((self, operator, other))

    def __ror__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((other, operator, self))

    def __xor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((self, operator, other))

    def __rxor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((other, operator, self))


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

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

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

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


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

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def evaluate(self, **variables):
        for variable in variables:
            if variable == self.name:
                return variables[variable]


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

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol


class Expression(Operand):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = set()
        self.get_all_variables(self.structure)

    def __str__(self):
        return self.to_string(self.structure)

    def __repr__(self):
        return self.to_string(self.structure)

    def to_string(self, structure):
        result = '('
        for item in structure:
            if isinstance(item, collections.Iterable):
                result += self.to_string(item)
            elif isinstance(item, Operator):
                result += ' ' + str(item) + ' '
            else:
                result += str(item)
        result += ')'
        return result

    def get_all_variables(self, structure):
        for item in structure:
            if isinstance(item, collections.Iterable):
                self.get_all_variables(item)
            elif isinstance(item, Expression):
                self.variable_names = self.variable_names | item.variable_names
            elif isinstance(item, Variable):
                self.variable_names.add(item.name)

    def evaluate(self, **variables):
        return self.evaluate_expression(self.structure, variables)

    def evaluate_expression(self, structure, variables):
        first_operand = structure[0]
        operator = structure[1]
        second_operand = structure[2]

        first_operand = self.evaluate_operand(first_operand, variables)
        second_operand = self.evaluate_operand(second_operand, variables)

        return operator.function(first_operand, second_operand)

    def evaluate_operand(self, operand, variables):
        if isinstance(operand, collections.Iterable):
            operand = self.evaluate_expression(operand, variables)
        elif isinstance(operand, Expression):
            operand = operand.evaluate(**variables)
        elif isinstance(operand, Constant):
            operand = operand.value
        elif isinstance(operand, Variable):
            for variable in variables:
                if operand.name == variable:
                    operand = variables[variable]
                    break

        return operand


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.155s

OK

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

Марина обнови решението на 21.03.2016 13:33 (преди над 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
import collections


class Operand:
    def __add__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((self, operator, other))

    __radd__ = __add__

    def __sub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((self, operator, other))

    def __rsub__(self, other):
        operator = Operator('-', lambda lhs, rhs: rhs - lhs)
        return Expression((self, operator, other))

    def __mul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((self, operator, other))

    __rmul__ = __mul__

    def __truediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((self, operator, other))

    def __rtruediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: rhs / lhs)
        return Expression((self, operator, other))

    def __floordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((self, operator, other))

    def __rfloordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: rhs // lhs)
        return Expression((self, operator, other))

    def __mod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((self, operator, other))

    def __rmod__(self, other):
        operator = Operator('%', lambda lhs, rhs: rhs % lhs)
        return Expression((self, operator, other))

    def __pow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((self, operator, other))

    def __rpow__(self, other):
        operator = Operator('**', lambda lhs, rhs: rhs ** lhs)
        return Expression((self, operator, other))

    def __lshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, operator, other))

    def __rshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, operator, other))

    __rlshift__ = __rshift__

    __rrshift__ = __lshift__

    def __and__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((self, operator, other))

    __rand__ = __and__

    def __or__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((self, operator, other))

    __ror__ = __or__

    def __xor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((self, operator, other))

    __rxor__ = __xor__


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

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

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

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


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

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def evaluate(self, **variables):
        for variable in variables:
            if variable == self.name:
                return variables[variable]


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

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol


class Expression(Operand):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = []
        self.get_all_variables(self.structure)

    def __str__(self):
        return self.to_string(self.structure)

    def to_string(self, structure):
        result = '('
        for item in structure:
            if isinstance(item, collections.Iterable):
                result += self.to_string(item)
            elif isinstance(item, Operator):
                result += ' ' + str(item) + ' '
            else:
                result += str(item)
        result += ')'
        return result

    def get_all_variables(self, structure):
        for item in structure:
            if isinstance(item, collections.Iterable):
                self.get_all_variables(item)
            elif isinstance(item, Variable):
                self.variable_names.append(item.name)

    def evaluate(self, **variables):
        return self.__evaluate(self.structure, variables)

    def __evaluate(self, structure, variables):
        first_operand = structure[0]
        operator = structure[1]
        second_operand = structure[2]

        first_operand = self.evaluate_operand(first_operand, variables)
        # print('__evaluate first {}'.format(first_operand))
        second_operand = self.evaluate_operand(second_operand, variables)
        # print('__evaluate second {}'.format(second_operand))

        return operator.function(first_operand, second_operand)

    def evaluate_operand(self, operand, variables):
        if isinstance(operand, collections.Iterable):
            # print('evaluate_operand Iterable {}'.format(operand))
            operand = self.__evaluate(operand, variables)
        elif isinstance(operand, Constant):
            # print('evaluate_operand Constant {}'.format(operand))
            operand = operand.value
        elif isinstance(operand, Variable):
            for variable in variables:
                if operand.name == variable:
                    # print('evaluate_operand Variable {}'.format(operand))
                    operand = variables[variable]
                    break

        return operand


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)

Марина обнови решението на 21.03.2016 14: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
import collections


class Operand:
    def __add__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((self, operator, other))

    def __radd__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((other, operator, self))

    def __sub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((self, operator, other))

    def __rsub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((other, operator, self))

    def __mul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((self, operator, other))

    def __rmul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((other, operator, self))

    def __truediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((self, operator, other))

    def __rtruediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((other, operator, self))

    def __floordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((self, operator, other))

    def __rfloordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((other, operator, self))

    def __mod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((self, operator, other))

    def __rmod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((other, operator, self))

    def __pow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((self, operator, other))

    def __rpow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((other, operator, self))

    def __lshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, operator, other))

    def __rlshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((other, operator, self))

    def __rshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, operator, other))

    def __rrshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((other, operator, self))

    def __and__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((self, operator, other))

    def __rand__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((other, operator, self))

    def __or__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((self, operator, other))

    def __ror__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((other, operator, self))

    def __xor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((self, operator, other))

    def __rxor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((other, operator, self))


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

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

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

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


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

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def evaluate(self, **variables):
        for variable in variables:
            if variable == self.name:
                return variables[variable]


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

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol


class Expression(Operand):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = []
        self.get_all_variables(self.structure)

    def __str__(self):
        return self.to_string(self.structure)

    def __repr__(self):
        return self.to_string(self.structure)

    def to_string(self, structure):
        result = '('
        for item in structure:
            if isinstance(item, collections.Iterable):
                result += self.to_string(item)
            elif isinstance(item, Operator):
                result += ' ' + str(item) + ' '
            else:
                result += str(item)
        result += ')'
        return result

    def get_all_variables(self, structure):
        for item in structure:
            if isinstance(item, collections.Iterable):
                self.get_all_variables(item)
            elif isinstance(item, Expression):
                self.variable_names.extend(item.variable_names)
            elif isinstance(item, Variable):
                self.variable_names.append(item.name)

    def evaluate(self, **variables):
        return self.evaluate_expression(self.structure, variables)

    def evaluate_expression(self, structure, variables):
        first_operand = structure[0]
        operator = structure[1]
        second_operand = structure[2]

        first_operand = self.evaluate_operand(first_operand, variables)
        second_operand = self.evaluate_operand(second_operand, variables)

        return operator.function(first_operand, second_operand)

    def evaluate_operand(self, operand, variables):
        if isinstance(operand, collections.Iterable):
            operand = self.evaluate_expression(operand, variables)
        elif isinstance(operand, Expression):
            operand = operand.evaluate(**variables)
        elif isinstance(operand, Constant):
            operand = operand.value
        elif isinstance(operand, Variable):
            for variable in variables:
                if operand.name == variable:
                    operand = variables[variable]
                    break

        return operand


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)

Марина обнови решението на 21.03.2016 14:57 (преди над 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
import collections


class Operand:
    def __add__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((self, operator, other))

    def __radd__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((other, operator, self))

    def __sub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((self, operator, other))

    def __rsub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((other, operator, self))

    def __mul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((self, operator, other))

    def __rmul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((other, operator, self))

    def __truediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((self, operator, other))

    def __rtruediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((other, operator, self))

    def __floordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((self, operator, other))

    def __rfloordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((other, operator, self))

    def __mod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((self, operator, other))

    def __rmod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((other, operator, self))

    def __pow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((self, operator, other))

    def __rpow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((other, operator, self))

    def __lshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, operator, other))

    def __rlshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((other, operator, self))

    def __rshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, operator, other))

    def __rrshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((other, operator, self))

    def __and__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((self, operator, other))

    def __rand__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((other, operator, self))

    def __or__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((self, operator, other))

    def __ror__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((other, operator, self))

    def __xor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((self, operator, other))

    def __rxor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((other, operator, self))


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

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

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

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


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

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def evaluate(self, **variables):
        for variable in variables:
            if variable == self.name:
                return variables[variable]


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

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol


class Expression(Operand):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = []
        self.get_all_variables(self.structure)

    def __str__(self):
        return self.to_string(self.structure)

    def __repr__(self):
        return self.to_string(self.structure)

    def to_string(self, structure):
        result = '('
        for item in structure:
            if isinstance(item, collections.Iterable):
                result += self.to_string(item)
            elif isinstance(item, Operator):
                result += ' ' + str(item) + ' '
            else:
                result += str(item)
        result += ')'
        return result

    def get_all_variables(self, structure):
        for item in structure:
            if isinstance(item, collections.Iterable):
                self.get_all_variables(item)
            elif isinstance(item, Expression):
                for variable_name in item.variable_names:
                    if variable_name not in self.variable_names:
                        self.variable_names.append(variable_name)
            elif isinstance(item, Variable):
                if item.name not in self.variable_names:
                    self.variable_names.append(item.name)

    def evaluate(self, **variables):
        return self.evaluate_expression(self.structure, variables)

    def evaluate_expression(self, structure, variables):
        first_operand = structure[0]
        operator = structure[1]
        second_operand = structure[2]

        first_operand = self.evaluate_operand(first_operand, variables)
        second_operand = self.evaluate_operand(second_operand, variables)

        return operator.function(first_operand, second_operand)

    def evaluate_operand(self, operand, variables):
        if isinstance(operand, collections.Iterable):
            operand = self.evaluate_expression(operand, variables)
        elif isinstance(operand, Expression):
            operand = operand.evaluate(**variables)
        elif isinstance(operand, Constant):
            operand = operand.value
        elif isinstance(operand, Variable):
            for variable in variables:
                if operand.name == variable:
                    operand = variables[variable]
                    break

        return operand


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)

Марина обнови решението на 21.03.2016 15: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
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
import collections


class Operand:
    def __add__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((self, operator, other))

    def __radd__(self, other):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        return Expression((other, operator, self))

    def __sub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((self, operator, other))

    def __rsub__(self, other):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        return Expression((other, operator, self))

    def __mul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((self, operator, other))

    def __rmul__(self, other):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        return Expression((other, operator, self))

    def __truediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((self, operator, other))

    def __rtruediv__(self, other):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        return Expression((other, operator, self))

    def __floordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((self, operator, other))

    def __rfloordiv__(self, other):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        return Expression((other, operator, self))

    def __mod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((self, operator, other))

    def __rmod__(self, other):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        return Expression((other, operator, self))

    def __pow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((self, operator, other))

    def __rpow__(self, other):
        operator = Operator('**', lambda lhs, rhs: lhs ** rhs)
        return Expression((other, operator, self))

    def __lshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, operator, other))

    def __rlshift__(self, other):
        operator = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((other, operator, self))

    def __rshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, operator, other))

    def __rrshift__(self, other):
        operator = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((other, operator, self))

    def __and__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((self, operator, other))

    def __rand__(self, other):
        operator = Operator('&', lambda lhs, rhs: lhs & rhs)
        return Expression((other, operator, self))

    def __or__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((self, operator, other))

    def __ror__(self, other):
        operator = Operator('|', lambda lhs, rhs: lhs | rhs)
        return Expression((other, operator, self))

    def __xor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((self, operator, other))

    def __rxor__(self, other):
        operator = Operator('^', lambda lhs, rhs: lhs ^ rhs)
        return Expression((other, operator, self))


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

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

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

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


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

    def __str__(self):
        return self.name

    def __repr__(self):
        return self.name

    def evaluate(self, **variables):
        for variable in variables:
            if variable == self.name:
                return variables[variable]


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

    def __str__(self):
        return self.symbol

    def __repr__(self):
        return self.symbol


class Expression(Operand):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = set()
        self.get_all_variables(self.structure)

    def __str__(self):
        return self.to_string(self.structure)

    def __repr__(self):
        return self.to_string(self.structure)

    def to_string(self, structure):
        result = '('
        for item in structure:
            if isinstance(item, collections.Iterable):
                result += self.to_string(item)
            elif isinstance(item, Operator):
                result += ' ' + str(item) + ' '
            else:
                result += str(item)
        result += ')'
        return result

    def get_all_variables(self, structure):
        for item in structure:
            if isinstance(item, collections.Iterable):
                self.get_all_variables(item)
            elif isinstance(item, Expression):
                self.variable_names = self.variable_names | item.variable_names
            elif isinstance(item, Variable):
                self.variable_names.add(item.name)

    def evaluate(self, **variables):
        return self.evaluate_expression(self.structure, variables)

    def evaluate_expression(self, structure, variables):
        first_operand = structure[0]
        operator = structure[1]
        second_operand = structure[2]

        first_operand = self.evaluate_operand(first_operand, variables)
        second_operand = self.evaluate_operand(second_operand, variables)

        return operator.function(first_operand, second_operand)

    def evaluate_operand(self, operand, variables):
        if isinstance(operand, collections.Iterable):
            operand = self.evaluate_expression(operand, variables)
        elif isinstance(operand, Expression):
            operand = operand.evaluate(**variables)
        elif isinstance(operand, Constant):
            operand = operand.value
        elif isinstance(operand, Variable):
            for variable in variables:
                if operand.name == variable:
                    operand = variables[variable]
                    break

        return operand


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)