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


class Expression_Element:

    def __add__(self, other):
        return Expression((self, Operator('+', lambda l, r: l + r), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', lambda l, r: l + r), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', lambda l, r: l - r), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', lambda l, r: l - r), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', lambda l, r: l * r), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', lambda l, r: l * r), self))

    def __truediv__(self, other):
        return Expression((self, Operator('/', lambda l, r: l / r), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('/', lambda l, r: l / r), self))

    def __floordiv__(self, other):
        return Expression((self, Operator('//', lambda l, r: l // r), other))

    def __rfloordiv__(self, other):
        return Expression((other, Operator('//', lambda l, r: l // r), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', lambda l, r: l % r), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', lambda l, r: l % r), self))

    def __lshift__(self, other):
        return Expression((self, Operator('<<', lambda l, r: l << r), other))

    def __rlshift__(self, other):
        return Expression((other, Operator('<<', lambda l, r: l << r), self))

    def __rshift__(self, other):
        return Expression((self, Operator('>>', lambda l, r: l >> r), other))

    def __rrshift__(self, other):
        return Expression((other, Operator('>>', lambda l, r: l >> r), self))

    def __and__(self, other):
        return Expression((self, Operator('&', lambda l, r: l & r), other))

    def __rand__(self, other):
        return Expression((other, Operator('&', lambda l, r: l & r), self))

    def __xor__(self, other):
        return Expression((self, Operator('^', lambda l, r: l ^ r), other))

    def __rxor__(self, other):
        return Expression((other, Operator('^', lambda l, r: l ^ r), self))

    def __or__(self, other):
        return Expression((self, Operator('|', lambda l, r: l | r), other))

    def __ror__(self, other):
        return Expression((other, Operator('|', lambda l, r: l | r), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', lambda l, r: l**r), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', lambda l, r: l**r), self))


class Constant(Expression_Element):

    def __init__(self, value):
        object.__setattr__(self, 'value', value)

    def __setattr__(self, name, value):
        raise TypeError("A Constant's value cannot be altered!")

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

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


class Variable(Expression_Element):

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

    def evaluate(self, **kwargs):
        if self.name in kwargs:
            result = kwargs[self.name]
            return result
        else:
            raise NameError("Variable to evaluate: not found")

    def __str__(self):
        return self.name


class Operator:

    def __init__(self, symbol, function):
        self.symbol = symbol
        self.operation = function

    def __call__(self, arg1, arg2):
        return self.operation(arg1, arg2)

    def __str__(self):
        return self.symbol


class Expression(Expression_Element):

    def __init__(self, expression_structure):

        self.expression_structure = []
        self.variable_names = []
        self.__arguments = []
        self.__operators = []

        for item in expression_structure:

            if isinstance(item, collections.Iterable):
                item = Expression(item)

            if isinstance(item, Constant):
                self.__arguments.append(item.value)
            elif isinstance(item, Variable):
                self.__arguments.append(item)
                self.variable_names.append(item.name)
            elif isinstance(item, Operator):
                self.__operators.append(item)
            else:
                self.__arguments.append(item)
                if isinstance(item, Expression):
                    self.variable_names.extend(item.variable_names)

            self.expression_structure.append(item)

    def evaluate(self, **variables):

        arguments = [item.evaluate(
                     **variables) if isinstance(item, Expression_Element)
                     else item for item in self.__arguments]
        arguments = arguments[::-1]

        for operator in self.__operators:
            if len(arguments) == 1:
                break
            result = operator(arguments.pop(), arguments.pop())
            arguments.append(result)

        return result

    def __str__(self):
        str_rpr = [str(item) for item in self.expression_structure]
        str_rpr = "(" + ' '.join(str_rpr) + ")"
        return str_rpr


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


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.158s

OK

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

Кристина обнови решението на 21.03.2016 14:39 (преди над 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 Expression_Element:

    def __add__(self, other):
        return Expression((self, Operator('+', lambda l, r: l + r), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', lambda l, r: l + r), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', lambda l, r: l - r), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', lambda l, r: l - r), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', lambda l, r: l * r), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', lambda l, r: l * r), self))

    def __truediv__(self, other):
        return Expression((self, Operator('/', lambda l, r: l / r), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('/', lambda l, r: l / r), self))

    def __floordiv__(self, other):
        return Expression((self, Operator('//', lambda l, r: l // r), other))

    def __rfloordiv__(self, other):
        return Expression((other, Operator('//', lambda l, r: l // r), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', lambda l, r: l % r), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', lambda l, r: l % r), self))

    def __lshift__(self, other):
        return Expression((self, Operator('<<', lambda l, r: l << r), other))

    def __rlshift__(self, other):
        return Expression((other, Operator('<<', lambda l, r: l << r), self))

    def __rshift__(self, other):
        return Expression((self, Operator('>>', lambda l, r: l >> r), other))

    def __rrshift__(self, other):
        return Expression((other, Operator('>>', lambda l, r: l >> r), self))

    def __and__(self, other):
        return Expression((self, Operator('&', lambda l, r: l & r), other))

    def __rand__(self, other):
        return Expression((other, Operator('&', lambda l, r: l & r), self))

    def __xor__(self, other):
        return Expression((self, Operator('^', lambda l, r: l ^ r), other))

    def __rxor__(self, other):
        return Expression((other, Operator('^', lambda l, r: l ^ r), self))

    def __or__(self, other):
        return Expression((self, Operator('|', lambda l, r: l | r), other))

    def __ror__(self, other):
        return Expression((other, Operator('|', lambda l, r: l | r), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', lambda l, r: l**r), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', lambda l, r: l**r), self))


class Constant(Expression_Element):

    def __init__(self, value):
        object.__setattr__(self, 'value', value)

    def __setattr__(self, name, value):
        raise TypeError("A Constant's value cannot be altered!")

    def evaluate(self):
        return self.value

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


class Variable(Expression_Element):

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

    def evaluate(self, **kwargs):
        if self.name in kwargs:
            result = kwargs[self.name]
        return result

    def __str__(self):
        return self.name


class Operator:

    def __init__(self, symbol, function):
        self.symbol = symbol
        self.operation = function

    def __call__(self, arg1, arg2):
        return self.operation(arg1, arg2)

    def __str__(self):
        return self.symbol


class Expression(Expression_Element):

    def __init__(self, expression_structure):

        self.expression_structure = []
        self.variable_names = []
        self.__arguments = []
        self.__operators = []

        for item in expression_structure:

            if isinstance(item, collections.Iterable):
                item = Expression(item)

            if isinstance(item, Constant):
                self.__arguments.append(item.value)
            elif isinstance(item, Variable):
                self.__arguments.append(item)
                self.variable_names.append(item.name)
            elif isinstance(item, Operator):
                self.__operators.append(item)
            else:
                self.__arguments.append(item)
                if isinstance(item, Expression):
                    self.variable_names.extend(item.variable_names)

            self.expression_structure.append(item)

        self.variable_names = tuple(self.variable_names)

    def evaluate(self, **variables):

        arguments = []

        for arg in self.__arguments:
            if isinstance(arg, Variable) or isinstance(arg, Expression):
                arguments.append(arg.evaluate(**variables))
            else:
                arguments.append(arg)

        arguments = arguments[::-1]

        for operator in self.__operators:
            if len(arguments) == 1:
                break
            a = arguments.pop()
            b = arguments.pop()
            result = operator(a, b)
            arguments.append(result)

        return result

    def __str__(self):
        str_rpr = [str(item) for item in self.expression_structure]
        str_rpr = "(" + ' '.join(str_rpr) + ")"
        return str_rpr


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


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

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


class Expression_Element:

    def __add__(self, other):
        return Expression((self, Operator('+', lambda l, r: l + r), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', lambda l, r: l + r), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', lambda l, r: l - r), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', lambda l, r: l - r), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', lambda l, r: l * r), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', lambda l, r: l * r), self))

    def __truediv__(self, other):
        return Expression((self, Operator('/', lambda l, r: l / r), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('/', lambda l, r: l / r), self))

    def __floordiv__(self, other):
        return Expression((self, Operator('//', lambda l, r: l // r), other))

    def __rfloordiv__(self, other):
        return Expression((other, Operator('//', lambda l, r: l // r), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', lambda l, r: l % r), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', lambda l, r: l % r), self))

    def __lshift__(self, other):
        return Expression((self, Operator('<<', lambda l, r: l << r), other))

    def __rlshift__(self, other):
        return Expression((other, Operator('<<', lambda l, r: l << r), self))

    def __rshift__(self, other):
        return Expression((self, Operator('>>', lambda l, r: l >> r), other))

    def __rrshift__(self, other):
        return Expression((other, Operator('>>', lambda l, r: l >> r), self))

    def __and__(self, other):
        return Expression((self, Operator('&', lambda l, r: l & r), other))

    def __rand__(self, other):
        return Expression((other, Operator('&', lambda l, r: l & r), self))

    def __xor__(self, other):
        return Expression((self, Operator('^', lambda l, r: l ^ r), other))

    def __rxor__(self, other):
        return Expression((other, Operator('^', lambda l, r: l ^ r), self))

    def __or__(self, other):
        return Expression((self, Operator('|', lambda l, r: l | r), other))

    def __ror__(self, other):
        return Expression((other, Operator('|', lambda l, r: l | r), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', lambda l, r: l**r), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', lambda l, r: l**r), self))


class Constant(Expression_Element):

    def __init__(self, value):
        object.__setattr__(self, 'value', value)

    def __setattr__(self, name, value):
        raise TypeError("A Constant's value cannot be altered!")

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

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


class Variable(Expression_Element):

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

    def evaluate(self, **kwargs):
        if self.name in kwargs:
            result = kwargs[self.name]
            return result
        else:
            raise NameError("Variable to evaluate: not found")

    def __str__(self):
        return self.name


class Operator:

    def __init__(self, symbol, function):
        self.symbol = symbol
        self.operation = function

    def __call__(self, arg1, arg2):
        return self.operation(arg1, arg2)

    def __str__(self):
        return self.symbol


class Expression(Expression_Element):

    def __init__(self, expression_structure):

        self.expression_structure = []
        self.variable_names = []
        self.__arguments = []
        self.__operators = []

        for item in expression_structure:

            if isinstance(item, collections.Iterable):
                item = Expression(item)

            if isinstance(item, Constant):
                self.__arguments.append(item.value)
            elif isinstance(item, Variable):
                self.__arguments.append(item)
                self.variable_names.append(item.name)
            elif isinstance(item, Operator):
                self.__operators.append(item)
            else:
                self.__arguments.append(item)
                if isinstance(item, Expression):
                    self.variable_names.extend(item.variable_names)

            self.expression_structure.append(item)

    def evaluate(self, **variables):

        arguments = []

        for arg in self.__arguments:
            if isinstance(arg, Expression_Element):
                arguments.append(arg.evaluate(**variables))
            else:
                arguments.append(arg)

        arguments = arguments[::-1]

        for operator in self.__operators:
            if len(arguments) == 1:
                break
            result = operator(arguments.pop(), arguments.pop())
            arguments.append(result)

        return result

    def __str__(self):
        str_rpr = [str(item) for item in self.expression_structure]
        str_rpr = "(" + ' '.join(str_rpr) + ")"
        return str_rpr


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


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

Кристина обнови решението на 21.03.2016 16:23 (преди над 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
import collections


class Expression_Element:

    def __add__(self, other):
        return Expression((self, Operator('+', lambda l, r: l + r), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', lambda l, r: l + r), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', lambda l, r: l - r), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', lambda l, r: l - r), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', lambda l, r: l * r), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', lambda l, r: l * r), self))

    def __truediv__(self, other):
        return Expression((self, Operator('/', lambda l, r: l / r), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('/', lambda l, r: l / r), self))

    def __floordiv__(self, other):
        return Expression((self, Operator('//', lambda l, r: l // r), other))

    def __rfloordiv__(self, other):
        return Expression((other, Operator('//', lambda l, r: l // r), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', lambda l, r: l % r), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', lambda l, r: l % r), self))

    def __lshift__(self, other):
        return Expression((self, Operator('<<', lambda l, r: l << r), other))

    def __rlshift__(self, other):
        return Expression((other, Operator('<<', lambda l, r: l << r), self))

    def __rshift__(self, other):
        return Expression((self, Operator('>>', lambda l, r: l >> r), other))

    def __rrshift__(self, other):
        return Expression((other, Operator('>>', lambda l, r: l >> r), self))

    def __and__(self, other):
        return Expression((self, Operator('&', lambda l, r: l & r), other))

    def __rand__(self, other):
        return Expression((other, Operator('&', lambda l, r: l & r), self))

    def __xor__(self, other):
        return Expression((self, Operator('^', lambda l, r: l ^ r), other))

    def __rxor__(self, other):
        return Expression((other, Operator('^', lambda l, r: l ^ r), self))

    def __or__(self, other):
        return Expression((self, Operator('|', lambda l, r: l | r), other))

    def __ror__(self, other):
        return Expression((other, Operator('|', lambda l, r: l | r), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', lambda l, r: l**r), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', lambda l, r: l**r), self))


class Constant(Expression_Element):

    def __init__(self, value):
        object.__setattr__(self, 'value', value)

    def __setattr__(self, name, value):
        raise TypeError("A Constant's value cannot be altered!")

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

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


class Variable(Expression_Element):

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

    def evaluate(self, **kwargs):
        if self.name in kwargs:
            result = kwargs[self.name]
            return result
        else:
            raise NameError("Variable to evaluate: not found")

    def __str__(self):
        return self.name


class Operator:

    def __init__(self, symbol, function):
        self.symbol = symbol
        self.operation = function

    def __call__(self, arg1, arg2):
        return self.operation(arg1, arg2)

    def __str__(self):
        return self.symbol


class Expression(Expression_Element):

    def __init__(self, expression_structure):

        self.expression_structure = []
        self.variable_names = []
        self.__arguments = []
        self.__operators = []

        for item in expression_structure:

            if isinstance(item, collections.Iterable):
                item = Expression(item)

            if isinstance(item, Constant):
                self.__arguments.append(item.value)
            elif isinstance(item, Variable):
                self.__arguments.append(item)
                self.variable_names.append(item.name)
            elif isinstance(item, Operator):
                self.__operators.append(item)
            else:
                self.__arguments.append(item)
                if isinstance(item, Expression):
                    self.variable_names.extend(item.variable_names)

            self.expression_structure.append(item)

    def evaluate(self, **variables):

        arguments = [item.evaluate(
                     **variables) if isinstance(item,
                                                Expression_Element) else item for item in self.__arguments]
        arguments = arguments[::-1]

        for operator in self.__operators:
            if len(arguments) == 1:
                break
            result = operator(arguments.pop(), arguments.pop())
            arguments.append(result)

        return result

    def __str__(self):
        str_rpr = [str(item) for item in self.expression_structure]
        str_rpr = "(" + ' '.join(str_rpr) + ")"
        return str_rpr


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


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)

Кристина обнови решението на 21.03.2016 16:32 (преди над 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
import collections


class Expression_Element:

    def __add__(self, other):
        return Expression((self, Operator('+', lambda l, r: l + r), other))

    def __radd__(self, other):
        return Expression((other, Operator('+', lambda l, r: l + r), self))

    def __sub__(self, other):
        return Expression((self, Operator('-', lambda l, r: l - r), other))

    def __rsub__(self, other):
        return Expression((other, Operator('-', lambda l, r: l - r), self))

    def __mul__(self, other):
        return Expression((self, Operator('*', lambda l, r: l * r), other))

    def __rmul__(self, other):
        return Expression((other, Operator('*', lambda l, r: l * r), self))

    def __truediv__(self, other):
        return Expression((self, Operator('/', lambda l, r: l / r), other))

    def __rtruediv__(self, other):
        return Expression((other, Operator('/', lambda l, r: l / r), self))

    def __floordiv__(self, other):
        return Expression((self, Operator('//', lambda l, r: l // r), other))

    def __rfloordiv__(self, other):
        return Expression((other, Operator('//', lambda l, r: l // r), self))

    def __mod__(self, other):
        return Expression((self, Operator('%', lambda l, r: l % r), other))

    def __rmod__(self, other):
        return Expression((other, Operator('%', lambda l, r: l % r), self))

    def __lshift__(self, other):
        return Expression((self, Operator('<<', lambda l, r: l << r), other))

    def __rlshift__(self, other):
        return Expression((other, Operator('<<', lambda l, r: l << r), self))

    def __rshift__(self, other):
        return Expression((self, Operator('>>', lambda l, r: l >> r), other))

    def __rrshift__(self, other):
        return Expression((other, Operator('>>', lambda l, r: l >> r), self))

    def __and__(self, other):
        return Expression((self, Operator('&', lambda l, r: l & r), other))

    def __rand__(self, other):
        return Expression((other, Operator('&', lambda l, r: l & r), self))

    def __xor__(self, other):
        return Expression((self, Operator('^', lambda l, r: l ^ r), other))

    def __rxor__(self, other):
        return Expression((other, Operator('^', lambda l, r: l ^ r), self))

    def __or__(self, other):
        return Expression((self, Operator('|', lambda l, r: l | r), other))

    def __ror__(self, other):
        return Expression((other, Operator('|', lambda l, r: l | r), self))

    def __pow__(self, other):
        return Expression((self, Operator('**', lambda l, r: l**r), other))

    def __rpow__(self, other):
        return Expression((other, Operator('**', lambda l, r: l**r), self))


class Constant(Expression_Element):

    def __init__(self, value):
        object.__setattr__(self, 'value', value)

    def __setattr__(self, name, value):
        raise TypeError("A Constant's value cannot be altered!")

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

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


class Variable(Expression_Element):

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

    def evaluate(self, **kwargs):
        if self.name in kwargs:
            result = kwargs[self.name]
            return result
        else:
            raise NameError("Variable to evaluate: not found")

    def __str__(self):
        return self.name


class Operator:

    def __init__(self, symbol, function):
        self.symbol = symbol
        self.operation = function

    def __call__(self, arg1, arg2):
        return self.operation(arg1, arg2)

    def __str__(self):
        return self.symbol


class Expression(Expression_Element):

    def __init__(self, expression_structure):

        self.expression_structure = []
        self.variable_names = []
        self.__arguments = []
        self.__operators = []

        for item in expression_structure:

            if isinstance(item, collections.Iterable):
                item = Expression(item)

            if isinstance(item, Constant):
                self.__arguments.append(item.value)
            elif isinstance(item, Variable):
                self.__arguments.append(item)
                self.variable_names.append(item.name)
            elif isinstance(item, Operator):
                self.__operators.append(item)
            else:
                self.__arguments.append(item)
                if isinstance(item, Expression):
                    self.variable_names.extend(item.variable_names)

            self.expression_structure.append(item)

    def evaluate(self, **variables):

        arguments = [item.evaluate(
                     **variables) if isinstance(item, Expression_Element)
                     else item for item in self.__arguments]
        arguments = arguments[::-1]

        for operator in self.__operators:
            if len(arguments) == 1:
                break
            result = operator(arguments.pop(), arguments.pop())
            arguments.append(result)

        return result

    def __str__(self):
        str_rpr = [str(item) for item in self.expression_structure]
        str_rpr = "(" + ' '.join(str_rpr) + ")"
        return str_rpr


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


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)