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
class BaseOperators:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


def eval_element(element, **kwargs):
    if isinstance(element, tuple):
        return Expression(element).evaluate(**kwargs)
    elif isinstance(element, Constant):
        return element.evaluate()
    elif isinstance(element, Variable):
        return element.evaluate(**kwargs)
    elif isinstance(element, Expression):
        return element.evaluate(**kwargs)
    else:
        return element


def find_variable_names(var_set, expr):
    for element in expr:
        if isinstance(element, Variable):
            var_set.add(str(element))
        elif isinstance(element, tuple):
            find_variable_names(var_set, Expression(element).expression)
        elif isinstance(element, Expression):
            find_variable_names(var_set, element.expression)


class Constant(BaseOperators):

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

    def evaluate(self):
        return self.value

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


class Variable(BaseOperators):

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

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

    def __str__(self):
        return self.name


class Expression(BaseOperators):

    def __init__(self, expression_structure):
        self.expression = expression_structure

    def evaluate(self, **kwargs):
        left = eval_element(self.expression[0], **kwargs)
        for position in range(1, len(self.expression), 2):
            right = eval_element(self.expression[position + 1], **kwargs)
            left = self.expression[position].evaluate(left, right)
        return left

    def element_to_string(self, array, expr):
        if isinstance(expr, tuple):
            array.append(str(Expression(expr)))
        elif isinstance(expr, Constant):
            array.append(str(expr))
        elif isinstance(expr, Variable):
            array.append(str(expr))
        elif isinstance(expr, Operator):
            array.append(' ' + str(expr) + ' ')
        else:
            array.append(str(expr))

    def __str__(self):
        output = []
        output.append('(')
        for element in self.expression:
            self.element_to_string(output, element)
        output.append(')')
        return ''.join(output)

    @property
    def variable_names(self):
        output = set()
        find_variable_names(output, self.expression)
        return output


class Operator:

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

    def evaluate(self, left, right):
        return self.function(left, right)

    def __str__(self):
        return self.symbol


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.115s

OK

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

Даниел обнови решението на 20.03.2016 22:59 (преди над 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
class OPERATORS:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


def eval_element(element, **kwargs):
    if type(element) is tuple:
        return Expression(element).evaluate(**kwargs)
    elif type(element) is Constant:
        return element.evaluate()
    elif type(element) is Variable:
        return element.evaluate(**kwargs)
    elif type(element) is Expression:
        return element.evaluate(**kwargs)
    else:
        return element


def find_variable_names(var_set, expr):
    for element in expr:
        if type(element) is Variable:
            var_set.add(str(element))
        elif type(element) is tuple:
            find_variable_names(var_set, Expression(element).expression)


class Constant(OPERATORS):

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

    def evaluate(self):
        return self.value

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


class Variable(OPERATORS):

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

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

    def __str__(self):
        return self.name


class Expression(OPERATORS):

    def __init__(self, expression_structure):
        self.expression = expression_structure

    def evaluate(self, **kwargs):
        left = eval_element(self.expression[0], **kwargs)
        for position in range(1, len(self.expression), 2):
            right = eval_element(self.expression[position + 1], **kwargs)
            left = self.expression[position].evaluate(left, right)
        return left

    def sub_st(self, array, expr):
        if type(expr) is tuple:
            array.append(str(Expression(expr)))
        elif type(expr) is Constant:
            array.append(str(expr))
        elif type(expr) is Variable:
            array.append(str(expr))
        elif type(expr) is Operator:
            array.append(' ' + str(expr) + ' ')
        else:
            array.append(str(expr))

    def __str__(self):
        output = []
        output.append('(')
        for element in self.expression:
            self.sub_st(output, element)
        output.append(')')
        return ''.join(output)

    @property
    def variable_names(self):
        output = set()
        find_variable_names(output, self.expression)
        return output


class Operator:

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

    def evaluate(self, left, right):
        return self.function(left, right)

    def __str__(self):
        return self.symbol


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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

Даниел обнови решението на 21.03.2016 10:40 (преди над 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
class OPERATORS:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


def eval_element(element, **kwargs):
    if type(element) is tuple:
        return Expression(element).evaluate(**kwargs)
    elif type(element) is Constant:
        return element.evaluate()
    elif type(element) is Variable:
        return element.evaluate(**kwargs)
    elif type(element) is Expression:
        return element.evaluate(**kwargs)
    else:
        return element


def find_variable_names(var_set, expr):
    for element in expr:
        if type(element) is Variable:
            var_set.add(str(element))
        elif type(element) is tuple:
            find_variable_names(var_set, Expression(element).expression)
        elif type(element) is Expression:
            find_variable_names(var_set, element.expression)


class Constant(OPERATORS):

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

    def evaluate(self):
        return self.value

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


class Variable(OPERATORS):

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

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

    def __str__(self):
        return self.name


class Expression(OPERATORS):

    def __init__(self, expression_structure):
        self.expression = expression_structure

    def evaluate(self, **kwargs):
        left = eval_element(self.expression[0], **kwargs)
        for position in range(1, len(self.expression), 2):
            right = eval_element(self.expression[position + 1], **kwargs)
            left = self.expression[position].evaluate(left, right)
        return left

    def sub_st(self, array, expr):
        if type(expr) is tuple:
            array.append(str(Expression(expr)))
        elif type(expr) is Constant:
            array.append(str(expr))
        elif type(expr) is Variable:
            array.append(str(expr))
        elif type(expr) is Operator:
            array.append(' ' + str(expr) + ' ')
        else:
            array.append(str(expr))

    def __str__(self):
        output = []
        output.append('(')
        for element in self.expression:
            self.sub_st(output, element)
        output.append(')')
        return ''.join(output)

    @property
    def variable_names(self):
        output = set()
        find_variable_names(output, self.expression)
        return output


class Operator:

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

    def evaluate(self, left, right):
        return self.function(left, right)

    def __str__(self):
        return self.symbol


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


def create_operator(symbol, function):
    return Operator(symbol, function)
  • OPERATORS е лошо име за клас и противоречи с препоръките в PEP8.
  • В дефиницията на операторите дефинираш променлива само за да я върнеш на следващия ред
  • Предпочитай да сравняваш типове с isinstance, вместо с type
  • sub_st е лошо име за метод.

Даниел обнови решението на 21.03.2016 13:59 (преди над 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
class BaseOperators:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


def eval_element(element, **kwargs):
    if isinstance(element, tuple):
        return Expression(element).evaluate(**kwargs)
    elif isinstance(element, Constant):
        return element.evaluate()
    elif isinstance(element, Variable):
        return element.evaluate(**kwargs)
    elif isinstance(element, Expression):
        return element.evaluate(**kwargs)
    else:
        return element


def find_variable_names(var_set, expr):
    for element in expr:
        if isinstance(element, Variable):
            var_set.add(str(element))
        elif isinstance(element, tuple):
            find_variable_names(var_set, Expression(element).expression)
        elif isinstance(element, Expression):
            find_variable_names(var_set, element.expression)


class Constant(BaseOperators):

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

    def evaluate(self):
        return self.value

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


class Variable(BaseOperators):

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

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

    def __str__(self):
        return self.name


class Expression(BaseOperators):

    def __init__(self, expression_structure):
        self.expression = expression_structure

    def evaluate(self, **kwargs):
        left = eval_element(self.expression[0], **kwargs)
        for position in range(1, len(self.expression), 2):
            right = eval_element(self.expression[position + 1], **kwargs)
            left = self.expression[position].evaluate(left, right)
        return left

    def element_to_string(self, array, expr):
        if isinstance(expr, tuple):
            array.append(str(Expression(expr)))
        elif isinstance(expr, Constant):
            array.append(str(expr))
        elif isinstance(expr, Variable):
            array.append(str(expr))
        elif isinstance(expr, Operator):
            array.append(' ' + str(expr) + ' ')
        else:
            array.append(str(expr))

    def __str__(self):
        output = []
        output.append('(')
        for element in self.expression:
            self.element_to_string(output, element)
        output.append(')')
        return ''.join(output)

    @property
    def variable_names(self):
        output = set()
        find_variable_names(output, self.expression)
        return output


class Operator:

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

    def evaluate(self, left, right):
        return self.function(left, right)

    def __str__(self):
        return self.symbol


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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