timeit

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

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

Решение на Аритметични изрази от Кристофър Митов

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

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

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
class Atom:
    pass


class Operand(Atom):
    def __add__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)

        return Expression((self, Operator("+", lambda x, y: x + y),
                           second_operand))

    def __radd__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("+", lambda x, y: x + y),
                           self))

    def __sub__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("-", lambda x, y: x - y),
                           second_operand))

    def __rsub__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("-", lambda x, y: x - y),
                           self))

    def __mul__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("*", lambda x, y: x * y),
                           second_operand))

    def __rmul__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("*", lambda x, y: x * y),
                           self))

    def __floordiv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("//", lambda x, y: x // y),
                           second_operand))

    def __rfloordiv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("//", lambda x, y: x // y),
                           self))

    def __truediv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("/", lambda x, y: x / y),
                           second_operand))

    def __rtruediv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("/", lambda x, y: x / y),
                           self))

    def __mod__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("%", lambda x, y: x % y),
                           second_operand))

    def __rmod__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("%", lambda x, y: x % y),
                           self))

    def __pow__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("**", lambda x, y: x ** y),
                           second_operand))

    def __rpow__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("**", lambda x, y: x ** y),
                           self))

    def __lshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("<<", lambda x, y: x << y),
                           second_operand))

    def __rlshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("<<", lambda x, y: x << y),
                           self))

    def __rshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator(">>", lambda x, y: x >> y),
                           second_operand))

    def __rrshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator(">>", lambda x, y: x >> y),
                           self))

    def __and__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("&", lambda x, y: x & y),
                           second_operand))

    def __rand__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("&", lambda x, y: x & y),
                           self))

    def __or__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("|", lambda x, y: x | y),
                           second_operand))

    def __ror__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("|", lambda x, y: x | y),
                           self))

    def __xor__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("^", lambda x, y: x ^ y),
                           second_operand))

    def __rxor__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("^", lambda x, y: x ^ y),
                           self))


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

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

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


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

    def __str__(self):
        return self.name

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

    def get_name(self):
        return self.name


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

    def __str__(self):
        return self.symbol

    def __call__(self, first_operand, second_operand):
        return self.operation(first_operand, second_operand)


class Expression(Operand):
    def __init__(self, expression_structure):
        self.expression_structure = list(expression_structure)

        for index in range(0, 3, 2):
            operand = self.expression_structure[index]
            if(not isinstance(operand, Atom)):
                self.expression_structure[index] = Expression(operand)

        self.variable_names = self.__get_variable_names(
                                self.expression_structure)

    def __getitem__(self, i):
        return self.expression_structure[i]

    def __str__(self):
        return "({} {} {})".format(str(self[0]), str(self[1]), str(self[2]))

    def __get_variable_names(self, expression_structure):
        variable_names = []
        for expression_part in expression_structure:
            expression_part_type = type(expression_part)
            if expression_part_type is Variable:
                variable_names += (expression_part.get_name(), )
            elif(expression_part_type is Expression):
                variable_names += self.__get_variable_names(expression_part)

        return list(set(variable_names))

    def evaluate(self, **variables):
        operation = self.expression_structure[1]
        first_argument = self.expression_structure[0].evaluate(**variables)
        second_argument = self.expression_structure[2].evaluate(**variables)

        return operation(first_argument, second_argument)


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)

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

.E.................
======================================================================
ERROR: test_create_expression_with_literal (test.TestArithmeticsWithLiterals)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: 'int' object is not iterable

----------------------------------------------------------------------
Ran 19 tests in 0.116s

FAILED (errors=1)

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

Кристофър обнови решението на 20.03.2016 23:07 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
class Atom:
    pass


class Operand(Atom):
    def __add__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)

        return Expression((self, Operator("+", lambda x, y: x + y),
                           second_operand))

    def __radd__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("+", lambda x, y: x + y),
                           self))

    def __sub__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("-", lambda x, y: x - y),
                           second_operand))

    def __rsub__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("-", lambda x, y: x - y),
                           self))

    def __mul__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("*", lambda x, y: x * y),
                           second_operand))

    def __rmul__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("*", lambda x, y: x * y),
                           self))

    def __floordiv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("//", lambda x, y: x // y),
                           second_operand))

    def __rfloordiv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("//", lambda x, y: x // y),
                           self))

    def __truediv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("/", lambda x, y: x / y),
                           second_operand))

    def __rtruediv__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("/", lambda x, y: x / y),
                           self))

    def __mod__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("%", lambda x, y: x % y),
                           second_operand))

    def __rmod__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("%", lambda x, y: x % y),
                           self))

    def __pow__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("**", lambda x, y: x ** y),
                           second_operand))

    def __rpow__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("**", lambda x, y: x ** y),
                           self))

    def __lshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("<<", lambda x, y: x << y),
                           second_operand))

    def __rlshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("<<", lambda x, y: x << y),
                           self))

    def __rshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator(">>", lambda x, y: x >> y),
                           second_operand))

    def __rrshift__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator(">>", lambda x, y: x >> y),
                           self))

    def __and__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("&", lambda x, y: x & y),
                           second_operand))

    def __rand__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("&", lambda x, y: x & y),
                           self))

    def __or__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("|", lambda x, y: x | y),
                           second_operand))

    def __ror__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("|", lambda x, y: x | y),
                           self))

    def __xor__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((self, Operator("^", lambda x, y: x ^ y),
                           second_operand))

    def __rxor__(self, other):
        not_literal = isinstance(other, Operand)
        second_operand = other if not_literal else Constant(other)
        return Expression((second_operand, Operator("^", lambda x, y: x ^ y),
                           self))


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

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

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


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

    def __str__(self):
        return self.name

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

    def get_name(self):
        return self.name


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

    def __str__(self):
        return self.symbol

    def __call__(self, first_operand, second_operand):
        return self.operation(first_operand, second_operand)


class Expression(Operand):
    def __init__(self, expression_structure):
        self.expression_structure = list(expression_structure)

        for index in range(0, 3, 2):
            operand = self.expression_structure[index]
            if(not isinstance(operand, Atom)):
                self.expression_structure[index] = Expression(operand)

        self.variable_names = self.__get_variable_names(
                                self.expression_structure)

    def __getitem__(self, i):
        return self.expression_structure[i]

    def __str__(self):
        return "({} {} {})".format(str(self[0]), str(self[1]), str(self[2]))

    def __get_variable_names(self, expression_structure):
        variable_names = []
        for expression_part in expression_structure:
            expression_part_type = type(expression_part)
            if expression_part_type is Variable:
                variable_names += (expression_part.get_name(), )
            elif(expression_part_type is Expression):
                variable_names += self.__get_variable_names(expression_part)

        return list(set(variable_names))

    def evaluate(self, **variables):
        operation = self.expression_structure[1]
        first_argument = self.expression_structure[0].evaluate(**variables)
        second_argument = self.expression_structure[2].evaluate(**variables)

        return operation(first_argument, second_argument)


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)