timeit

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

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

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

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

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

Резултати

  • 10 точки от тестове
  • 1 отнета точка
  • 9 точки общо
  • 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
220
221
222
223
224
225
import collections


class OperatorsOwner:
    def __add__(self, other):
        add = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, add, other))

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

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

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

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

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

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

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

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

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

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        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):
        on_power = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((self, on_power, other))

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

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

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

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

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

    def __and__(self, other):
        and_operator = create_operator('and', lambda lhs, rhs: lhs & rhs)
        return create_expression((self, and_operator, other))

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

    def __or__(self, other):
        or_operator = create_operator('or', lambda lhs, rhs: lhs | rhs)
        return create_expression((self, or_operator, other))

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

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

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

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

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

    def __eq__(self, other):
        eq = create_operator('==', lambda lhs, rhs: lhs == rhs)
        return create_expression((self, eq, other))

    def __ne__(self, other):
        ne = create_operator('!=', lambda lhs, rhs: lhs != rhs)
        return create_expression((self, ne, other))

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

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


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

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

    def __repr__(self):
        return str(self)

    def __int__(self):
        return int(self.__value)

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


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

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

    def __repr__(self):
        return str(self)

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


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

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

    def __repr__(self):
        return str(self)

    def __call__(self, lhs, rhs):
        return self.function(lhs, rhs)


class Expression(OperatorsOwner):
    def __init__(self, expression_structure):
        self.lhs, self.operation, self.rhs = expression_structure
        self.variable_names = []

        if isinstance(self.lhs, Variable):
            self.variable_names.append(self.lhs.name)
        elif isinstance(self.lhs, collections.Iterable):
            self.lhs = create_expression(self.lhs)
            self.variable_names.extend(self.lhs.variable_names)
        elif isinstance(self.lhs, Expression):
            self.variable_names.extend(self.lhs.variable_names)
        else:
            self.lhs = create_constant(self.lhs)

        if isinstance(self.rhs, Variable):
            self.variable_names.append(self.rhs.name)
        elif isinstance(self.rhs, collections.Iterable):
            self.rhs = create_expression(self.rhs)
            self.variable_names.extend(self.rhs.variable_names)
        elif isinstance(self.rhs, Expression):
            self.variable_names.extend(self.rhs.variable_names)
        else:
            self.rhs = create_constant(self.rhs)

    def __str__(self):
        return "({} {} {})".format(
            str(self.lhs),
            str(self.operation),
            str(self.rhs))

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return self.operation(
            self.lhs.evaluate(**kwargs),
            self.rhs.evaluate(**kwargs))


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.151s

OK

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

Виктор обнови решението на 19.03.2016 20:46 (преди над 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
import collections


class OverloadedOperatorsOwner:
    def __add__(self, other):
        add = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, add, other))

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

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

    def __rmul__(self, other):
        return self * other

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

    def __rsub__(self, other):
        return self - other

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

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

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

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

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        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):
        on_power = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((self, on_power, other))

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

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

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

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

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

    def __and__(self, other):
        and_operator = create_operator('and', lambda lhs, rhs: lhs and rhs)
        return create_expression((self, and_operator, other))

    def __rand__(self, other):
        return self and other

    def __or__(self, other):
        or_operator = create_operator('or', lambda lhs, rhs: lhs or rhs)
        return create_expression((self, or_operator, other))

    def __ror__(self, other):
        return self or other

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

    def __rxor__(self, other):
        return self ^ other


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

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

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

    def __int__(self):
        return int(self.__value)

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


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

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

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        for key, value in kwargs.items():
            if key is self.name:
                return value


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

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

    def __repr__(self):
        return str(self)

    def __call__(self, lhs, rhs):
        return self.function(lhs, rhs)


class Expression(OverloadedOperatorsOwner):
    def __init__(self, expression_structure):
        self.variable_names = []
        self.variable_names.extend(
            elem.name for elem
            in expression_structure
            if isinstance(elem, Variable)
            )
        self.lhs, self.operation, self.rhs = expression_structure
        if isinstance(self.lhs, collections.Iterable):
            self.lhs = create_expression(self.lhs)
            self.variable_names.extend(self.lhs.variable_names)
        if isinstance(self.rhs, collections.Iterable):
            self.rhs = create_expression(self.rhs)
            self.variable_names.extend(self.rhs.variable_names)

    def __str__(self):
        return "({} {} {})".format(
            str(self.lhs),
            str(self.operation),
            str(self.rhs)
            )

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        if isinstance(self.lhs, (Constant, Variable, Expression)):
            self.lhs = self.lhs.evaluate(**kwargs)
        if isinstance(self.rhs, (Constant, Variable, Expression)):
            self.rhs = self.rhs.evaluate(**kwargs)
        return self.operation(self.lhs, self.rhs)


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)

Виктор обнови решението на 20.03.2016 01:18 (преди над 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
import collections


class OverloadedOperatorsOwner:
    def __add__(self, other):
        add = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, add, other))

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

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

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

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

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

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

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

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

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

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        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):
        on_power = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((self, on_power, other))

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

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

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

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

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

    def __and__(self, other):
        and_operator = create_operator('and', lambda lhs, rhs: lhs and rhs)
        return create_expression((self, and_operator, other))

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

    def __or__(self, other):
        or_operator = create_operator('or', lambda lhs, rhs: lhs or rhs)
        return create_expression((self, or_operator, other))

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

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

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


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

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

    def __repr__(self):
        return str(self)

    def __int__(self):
        return int(self.__value)

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


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

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

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        for key, value in kwargs.items():
            if key is self.name:
                return value


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

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

    def __repr__(self):
        return str(self)

    def __call__(self, lhs, rhs):
        return self.function(lhs, rhs)


class Expression(OverloadedOperatorsOwner):
    def __init__(self, expression_structure):
        self.lhs, self.operation, self.rhs = expression_structure
        self.variable_names = []
        if isinstance(self.lhs, Variable):
            self.variable_names.append(self.lhs.name)
        elif isinstance(self.lhs, collections.Iterable):
            self.lhs = create_expression(self.lhs)
            self.variable_names.extend(self.lhs.variable_names)
        elif isinstance(self.lhs, Expression):
            self.variable_names.extend(self.lhs.variable_names)
        if isinstance(self.rhs, Variable):
            self.variable_names.append(self.rhs.name)
        elif isinstance(self.rhs, collections.Iterable):
            self.rhs = create_expression(self.rhs)
            self.variable_names.extend(self.rhs.variable_names)
        elif isinstance(self.rhs, Expression):
            self.variable_names.extend(self.rhs.variable_names)

    def __str__(self):
        return "({} {} {})".format(
            str(self.lhs),
            str(self.operation),
            str(self.rhs))

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        lhs, rhs = self.lhs, self.rhs
        if isinstance(self.lhs, (Constant, Variable, Expression)):
            lhs = self.lhs.evaluate(**kwargs)
        if isinstance(self.rhs, (Constant, Variable, Expression)):
            rhs = self.rhs.evaluate(**kwargs)
        return self.operation(lhs, rhs)


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)
  • OverloadedOperatorsOwner е доста кофти име :)
  • evaluate-а на Variable е объркващо сложен за простото нещо, което прави, взимане на стойност от dict.
  • __init__-а на Expression е много труден за четене, може да се случи много по-приятно.
  • Предполагам в следствие на горното в evaluate-а на Expression проверяваш дали lhs и rhs са Variable или Constant, но в крайна сметка не правиш нищо ако не са? А всъщност при правилно създаден Expression обект има ли изобщо как да не са инстанция на някое от двете?
  • Можеш да inline-ваш извикването на create_operator в предефинираните ти оператори
  • В този ред на мисли можеш да предефинираш всичките оператори наведнъж доста по-елегантно.

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


class OperatorsOwner:
    def __add__(self, other):
        add = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, add, other))

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

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

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

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

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

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

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

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

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

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        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):
        on_power = create_operator('**', lambda lhs, rhs: lhs ** rhs)
        return create_expression((self, on_power, other))

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

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

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

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

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

    def __and__(self, other):
        and_operator = create_operator('and', lambda lhs, rhs: lhs & rhs)
        return create_expression((self, and_operator, other))

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

    def __or__(self, other):
        or_operator = create_operator('or', lambda lhs, rhs: lhs | rhs)
        return create_expression((self, or_operator, other))

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

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

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

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

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

    def __eq__(self, other):
        eq = create_operator('==', lambda lhs, rhs: lhs == rhs)
        return create_expression((self, eq, other))

    def __ne__(self, other):
        ne = create_operator('!=', lambda lhs, rhs: lhs != rhs)
        return create_expression((self, ne, other))

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

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


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

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

    def __repr__(self):
        return str(self)

    def __int__(self):
        return int(self.__value)

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


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

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

    def __repr__(self):
        return str(self)

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


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

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

    def __repr__(self):
        return str(self)

    def __call__(self, lhs, rhs):
        return self.function(lhs, rhs)


class Expression(OperatorsOwner):
    def __init__(self, expression_structure):
        self.lhs, self.operation, self.rhs = expression_structure
        self.variable_names = []

        if isinstance(self.lhs, Variable):
            self.variable_names.append(self.lhs.name)
        elif isinstance(self.lhs, collections.Iterable):
            self.lhs = create_expression(self.lhs)
            self.variable_names.extend(self.lhs.variable_names)
        elif isinstance(self.lhs, Expression):
            self.variable_names.extend(self.lhs.variable_names)
        else:
            self.lhs = create_constant(self.lhs)

        if isinstance(self.rhs, Variable):
            self.variable_names.append(self.rhs.name)
        elif isinstance(self.rhs, collections.Iterable):
            self.rhs = create_expression(self.rhs)
            self.variable_names.extend(self.rhs.variable_names)
        elif isinstance(self.rhs, Expression):
            self.variable_names.extend(self.rhs.variable_names)
        else:
            self.rhs = create_constant(self.rhs)

    def __str__(self):
        return "({} {} {})".format(
            str(self.lhs),
            str(self.operation),
            str(self.rhs))

    def __repr__(self):
        return str(self)

    def evaluate(self, **kwargs):
        return self.operation(
            self.lhs.evaluate(**kwargs),
            self.rhs.evaluate(**kwargs))


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)