timeit

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

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

Решение на Аритметични изрази от Николай Желязков

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

Към профила на Николай Желязков

Резултати

  • 10 точки от тестове
  • 1 бонус точка
  • 11 точки общо
  • 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 Operations:
    def __add__(self, other):
        op = create_operator('+', lambda x, y: x + y)
        return Expression(self, op, other)

    def __sub__(self, other):
        op = create_operator('-', lambda x, y: x - y)
        return Expression(self, op, other)

    def __mul__(self, other):
        op = create_operator('*', lambda x, y: x * y)
        return Expression(self, op, other)

    def __truediv__(self, other):
        op = create_operator('/', lambda x, y: x / y)
        return Expression(self, op, other)

    def __floordiv__(self, other):
        op = create_operator('//', lambda x, y: x // y)
        return Expression(self, op, other)

    def __mod__(self, other):
        op = create_operator('%', lambda x, y: x % y)
        return Expression(self, op, other)

    def __divmod__(self, other):
        op = create_operator('divmod', lambda x, y: divmod(x, y))
        return Expression(self, op, other)

    def __pow__(self, other):
        op = create_operator('**', lambda x, y: x ** y)
        return Expression(self, op, other)

    def __lshift__(self, other):
        op = create_operator('<<', lambda x, y: x << y)
        return Expression(self, op, other)

    def __rshift__(self, other):
        op = create_operator('>>', lambda x, y: x >> y)
        return Expression(self, op, other)

    def __and__(self, other):
        op = create_operator('&', lambda x, y: x & y)
        return Expression(self, op, other)

    def __xor__(self, other):
        op = create_operator('^', lambda x, y: x ^ y)
        return Expression(self, op, other)

    def __or__(self, other):
        op = create_operator('|', lambda x, y: x | y)
        return Expression(self, op, other)

    def __radd__(self, other):
        op = create_operator('+', lambda x, y: x + y)
        return Expression(other, op, self)

    def __rsub__(self, other):
        op = create_operator('-', lambda x, y: x - y)
        return Expression(other, op, self)

    def __rmul__(self, other):
        op = create_operator('*', lambda x, y: x * y)
        return Expression(other, op, self)

    def __rtruediv__(self, other):
        op = create_operator('/', lambda x, y: x / y)
        return Expression(other, op, self)

    def __rfloordiv__(self, other):
        op = create_operator('//', lambda x, y: x // y)
        return Expression(other, op, self)

    def __rmod__(self, other):
        op = create_operator('%', lambda x, y: x % y)
        return Expression(other, op, self)

    def __rdivmod__(self, other):
        op = create_operator('divmod', lambda x, y: divmod(x, y))
        return Expression(other, op, self)

    def __rpow__(self, other):
        op = create_operator('**', lambda x, y: x ** y)
        return Expression(other, op, self)

    def __rlshift__(self, other):
        op = create_operator('<<', lambda x, y: x << y)
        return Expression(other, op, self)

    def __rrshift__(self, other):
        op = create_operator('>>', lambda x, y: x >> y)
        return Expression(other, op, self)

    def __rand__(self, other):
        op = create_operator('&', lambda x, y: x & y)
        return Expression(other, op, self)

    def __rxor__(self, other):
        op = create_operator('^', lambda x, y: x ^ y)
        return Expression(other, op, self)

    def __ror__(self, other):
        op = create_operator('|', lambda x, y: x | y)
        return Expression(other, op, self)


class Expression(Operations):
    def __make_parameter(x):
        if isinstance(x, Operations):
            return x
        elif isinstance(x, tuple):
            return Expression(*x)
        else:
            return create_constant(x)

    def __init__(self, left, op, right):
        self.left = Expression.__make_parameter(left)
        self.right = Expression.__make_parameter(right)
        self.op = op
        self.variable_names = set()
        self.variable_names |= getattr(self.right, 'variable_names', set())
        self.variable_names |= getattr(self.left, 'variable_names', set())

    def __str__(self):
        left_half = '(' + str(self.left) + ' '
        right_half = ' ' + str(self.right) + ')'
        return left_half + str(self.op) + right_half

    def evaluate(self, **variables):
        left_value = self.left.evaluate(**variables)
        right_value = self.right.evaluate(**variables)
        return self.op.function(left_value, right_value)


class Operator:
    instances = {}

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

    def __str__(self):
        return self.symbol


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

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

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


class Variable(Operations):
    def __init__(self, name):
        self.name = name
        self.variable_names = {name}

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

    def __str__(self):
        return self.name


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_operator(symbol, function):
    if symbol not in Operator.instances:
        Operator.instances[symbol] = Operator(symbol, function)
    return Operator.instances[symbol]


def create_expression(expression_structure):
    return Expression(*expression_structure)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.118s

OK

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

Николай обнови решението на 17.03.2016 19:40 (преди почти 2 години)

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
class Operations:
    def __add__(self, other):
        return Expression(self, Operator('+', lambda x, y: x + y), other)

    def __sub__(self, other):
        return Expression(self, Operator('-', lambda x, y: x - y), other)

    def __mul__(self, other):
        return Expression(self, Operator('*', lambda x, y: x * y), other)

    def __truediv__(self, other):
        return Expression(self, Operator('/', lambda x, y: x / y), other)

    def __radd__(self, other):
        return Expression(other, Operator('+', lambda x, y: x + y), self)

    def __rsub__(self, other):
        return Expression(other, Operator('-', lambda x, y: x - y), self)

    def __rmul__(self, other):
        return Expression(other, Operator('*', lambda x, y: x * y), self)

    def __rtruediv__(self, other):
        return Expression(other, Operator('/', lambda x, y: x / y), self)


class Expression(Operations):
    def __init__(self, left, op, right):
        if isinstance(left, Operations):
            self.left = left
        elif type(left) is tuple:
            self.left = Expression(*left)
        else:
            self.left = create_constant(left)
        if isinstance(right, Operations):
            self.right = right
        elif type(right) is tuple:
            self.right = Expression(*right)
        else:
            self.right = create_constant(right)
        self.op = op
        self.variable_names = set()
        if type(self.left) is Variable:
            self.variable_names.add(left.name)
        if type(self.right) is Variable:
            self.variable_names.add(right.name)
        if type(self.left) is Expression:
            self.variable_names |= self.left.variable_names
        if type(self.right) is Expression:
            self.variable_names |= self.right.variable_names

    def __str__(self):
        left_half = '(' + str(self.left) + ' '
        right_half = ' ' + str(self.right) + ')'
        return left_half + str(self.op) + right_half

    def evaluate(self, **variables):
        left_value = self.left.evaluate(**variables)
        right_value = self.right.evaluate(**variables)
        return self.op.function(left_value, right_value)


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

        def __str__(self):
            return self.symbol

    instances = {}

    def __new__(cls, symbol, function):
        if symbol not in Operator.instances:
            Operator.instances[symbol] = Operator.__Operator(symbol, function)
        return Operator.instances[symbol]


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

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

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


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

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

    def __str__(self):
        return self.name


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)
  • Expression.__init__ е страшно сложен и правиш сравнения с резултата от type(), вместо да използваш isinstance().
  • Идеята с вложения клас за оператори и заигравките с __new__ са забавни, но нямаш нужда от толкова магия там. Опрости го.

Николай обнови решението на 19.03.2016 22:08 (преди над 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
class Operations:
    def __add__(self, other):
        return Expression(self, Operator('+', lambda x, y: x + y), other)

    def __sub__(self, other):
        return Expression(self, Operator('-', lambda x, y: x - y), other)

    def __mul__(self, other):
        return Expression(self, Operator('*', lambda x, y: x * y), other)

    def __truediv__(self, other):
        return Expression(self, Operator('/', lambda x, y: x / y), other)

    def __radd__(self, other):
        return Expression(other, Operator('+', lambda x, y: x + y), self)

    def __rsub__(self, other):
        return Expression(other, Operator('-', lambda x, y: x - y), self)

    def __rmul__(self, other):
        return Expression(other, Operator('*', lambda x, y: x * y), self)

    def __rtruediv__(self, other):
        return Expression(other, Operator('/', lambda x, y: x / y), self)


class Expression(Operations):
    def __make_parameter(x):
        if isinstance(x, Operations):
            return x
        elif isinstance(x, tuple):
            return Expression(*x)
        else:
            return create_constant(x)

    def __init__(self, left, op, right):
        self.left = Expression.__make_parameter(left)
        self.right = Expression.__make_parameter(right)
        self.op = op
        self.variable_names = set()
        self.variable_names |= getattr(self.right, 'variable_names', set())
        self.variable_names |= getattr(self.left, 'variable_names', set())

    def __str__(self):
        left_half = '(' + str(self.left) + ' '
        right_half = ' ' + str(self.right) + ')'
        return left_half + str(self.op) + right_half

    def evaluate(self, **variables):
        left_value = self.left.evaluate(**variables)
        right_value = self.right.evaluate(**variables)
        return self.op.function(left_value, right_value)


class Operator:
    instances = {}

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

    def __str__(self):
        return self.symbol


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

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

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


class Variable(Operations):
    def __init__(self, name):
        self.name = name
        self.variable_names = {name}

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

    def __str__(self):
        return self.name


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_operator(symbol, function):
    if symbol not in Operator.instances:
        Operator.instances[symbol] = Operator(symbol, function)
    return Operator.instances[symbol]


def create_expression(expression_structure):
    return Expression(*expression_structure)

Николай обнови решението на 20.03.2016 23:31 (преди над 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
class Operations:
    def __add__(self, other):
        return Expression(self, Operator('+', lambda x, y: x + y), other)

    def __sub__(self, other):
        return Expression(self, Operator('-', lambda x, y: x - y), other)

    def __mul__(self, other):
        return Expression(self, Operator('*', lambda x, y: x * y), other)

    def __truediv__(self, other):
        return Expression(self, Operator('/', lambda x, y: x / y), other)

    def __floordiv__(self, other):
        return Expression(self, Operator('//', lambda x, y: x // y), other)

    def __radd__(self, other):
        return Expression(other, Operator('+', lambda x, y: x + y), self)

    def __rsub__(self, other):
        return Expression(other, Operator('-', lambda x, y: x - y), self)

    def __rmul__(self, other):
        return Expression(other, Operator('*', lambda x, y: x * y), self)

    def __rtruediv__(self, other):
        return Expression(other, Operator('/', lambda x, y: x / y), self)

    def __rfloordiv__(self, other):
        return Expression(other, Operator('//', lambda x, y: x // y), self)


class Expression(Operations):
    def __make_parameter(x):
        if isinstance(x, Operations):
            return x
        elif isinstance(x, tuple):
            return Expression(*x)
        else:
            return create_constant(x)

    def __init__(self, left, op, right):
        self.left = Expression.__make_parameter(left)
        self.right = Expression.__make_parameter(right)
        self.op = op
        self.variable_names = set()
        self.variable_names |= getattr(self.right, 'variable_names', set())
        self.variable_names |= getattr(self.left, 'variable_names', set())

    def __str__(self):
        left_half = '(' + str(self.left) + ' '
        right_half = ' ' + str(self.right) + ')'
        return left_half + str(self.op) + right_half

    def evaluate(self, **variables):
        left_value = self.left.evaluate(**variables)
        right_value = self.right.evaluate(**variables)
        return self.op.function(left_value, right_value)


class Operator:
    instances = {}

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

    def __str__(self):
        return self.symbol


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

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

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


class Variable(Operations):
    def __init__(self, name):
        self.name = name
        self.variable_names = {name}

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

    def __str__(self):
        return self.name


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_operator(symbol, function):
    if symbol not in Operator.instances:
        Operator.instances[symbol] = Operator(symbol, function)
    return Operator.instances[symbol]


def create_expression(expression_structure):
    return Expression(*expression_structure)

Николай обнови решението на 21.03.2016 16:33 (преди над 1 година)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
class Operations:
    def __add__(self, other):
        op = create_operator('+', lambda x, y: x + y)
        return Expression(self, op, other)

    def __sub__(self, other):
        op = create_operator('-', lambda x, y: x - y)
        return Expression(self, op, other)

    def __mul__(self, other):
        op = create_operator('*', lambda x, y: x * y)
        return Expression(self, op, other)

    def __truediv__(self, other):
        op = create_operator('/', lambda x, y: x / y)
        return Expression(self, op, other)

    def __floordiv__(self, other):
        op = create_operator('//', lambda x, y: x // y)
        return Expression(self, op, other)

    def __mod__(self, other):
        op = create_operator('%', lambda x, y: x % y)
        return Expression(self, op, other)

    def __divmod__(self, other):
        op = create_operator('divmod', lambda x, y: divmod(x, y))
        return Expression(self, op, other)

    def __pow__(self, other):
        op = create_operator('**', lambda x, y: x ** y)
        return Expression(self, op, other)

    def __lshift__(self, other):
        op = create_operator('<<', lambda x, y: x << y)
        return Expression(self, op, other)

    def __rshift__(self, other):
        op = create_operator('>>', lambda x, y: x >> y)
        return Expression(self, op, other)

    def __and__(self, other):
        op = create_operator('&', lambda x, y: x & y)
        return Expression(self, op, other)

    def __xor__(self, other):
        op = create_operator('^', lambda x, y: x ^ y)
        return Expression(self, op, other)

    def __or__(self, other):
        op = create_operator('|', lambda x, y: x | y)
        return Expression(self, op, other)

    def __radd__(self, other):
        op = create_operator('+', lambda x, y: x + y)
        return Expression(other, op, self)

    def __rsub__(self, other):
        op = create_operator('-', lambda x, y: x - y)
        return Expression(other, op, self)

    def __rmul__(self, other):
        op = create_operator('*', lambda x, y: x * y)
        return Expression(other, op, self)

    def __rtruediv__(self, other):
        op = create_operator('/', lambda x, y: x / y)
        return Expression(other, op, self)

    def __rfloordiv__(self, other):
        op = create_operator('//', lambda x, y: x // y)
        return Expression(other, op, self)

    def __rmod__(self, other):
        op = create_operator('%', lambda x, y: x % y)
        return Expression(other, op, self)

    def __rdivmod__(self, other):
        op = create_operator('divmod', lambda x, y: divmod(x, y))
        return Expression(other, op, self)

    def __rpow__(self, other):
        op = create_operator('**', lambda x, y: x ** y)
        return Expression(other, op, self)

    def __rlshift__(self, other):
        op = create_operator('<<', lambda x, y: x << y)
        return Expression(other, op, self)

    def __rrshift__(self, other):
        op = create_operator('>>', lambda x, y: x >> y)
        return Expression(other, op, self)

    def __rand__(self, other):
        op = create_operator('&', lambda x, y: x & y)
        return Expression(other, op, self)

    def __rxor__(self, other):
        op = create_operator('^', lambda x, y: x ^ y)
        return Expression(other, op, self)

    def __ror__(self, other):
        op = create_operator('|', lambda x, y: x | y)
        return Expression(other, op, self)


class Expression(Operations):
    def __make_parameter(x):
        if isinstance(x, Operations):
            return x
        elif isinstance(x, tuple):
            return Expression(*x)
        else:
            return create_constant(x)

    def __init__(self, left, op, right):
        self.left = Expression.__make_parameter(left)
        self.right = Expression.__make_parameter(right)
        self.op = op
        self.variable_names = set()
        self.variable_names |= getattr(self.right, 'variable_names', set())
        self.variable_names |= getattr(self.left, 'variable_names', set())

    def __str__(self):
        left_half = '(' + str(self.left) + ' '
        right_half = ' ' + str(self.right) + ')'
        return left_half + str(self.op) + right_half

    def evaluate(self, **variables):
        left_value = self.left.evaluate(**variables)
        right_value = self.right.evaluate(**variables)
        return self.op.function(left_value, right_value)


class Operator:
    instances = {}

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

    def __str__(self):
        return self.symbol


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

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

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


class Variable(Operations):
    def __init__(self, name):
        self.name = name
        self.variable_names = {name}

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

    def __str__(self):
        return self.name


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


def create_operator(symbol, function):
    if symbol not in Operator.instances:
        Operator.instances[symbol] = Operator(symbol, function)
    return Operator.instances[symbol]


def create_expression(expression_structure):
    return Expression(*expression_structure)