timeit

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

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

Решение на Аритметични изрази от Тина Накова

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

Към профила на Тина Накова

Резултати

  • 10 точки от тестове
  • 1 отнета точка
  • 9 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

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

    def __radd__(self, other):
        return Value.__add__(other, self)

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

    def __rsub__(self, other):
        return Value.__sub__(other, self)

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

    def __rmul__(self, other):
        return Value.__mul__(other, self)

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

    def __rtruediv__(self, other):
        return Value.__truediv__(other, self)

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

    def __rfloordiv__(self, other):
        return Value.__floordiv__(other, self)

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((self, mod, other))

    def __rmod__(self, other):
        return Value.__mod__(other, self)

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

    def __rpow__(self, other):
        return Value.__pow__(other, self)

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

    def __rlshift__(self, other):
        return Value.__lshift__(other, self)

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

    def __rrshift__(self, other):
        return Value.__rshift__(other, self)

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

    def __rand__(self, other):
        return Value.__and__(other, self)

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

    def __rxor__(self, other):
        return Value.__xor__(other, self)

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

    def __ror__(self, other):
        return Value.__or__(other, self)


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

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

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


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

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

    @property
    def get_name(self):
        return self._name

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


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

    def call(self, *args):
        return self._function(*args)

    @property
    def get_symbol(self):
        return self._symbol

    def __str__(self):
        return self._symbol


class Expression(Value):
    def __init__(self, expression_structure):
        self._expression_structure = expression_structure

    def evaluate(self, **variables):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = create_constant(create_expression(operand1).
                                       evaluate(**variables))

        elif isinstance(operand1, (int, float, complex)):
            operand1 = create_constant(operand1)

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = create_constant(create_expression(operand2).
                                       evaluate(**variables))

        elif isinstance(operand2, (int, float, complex)):
            operand2 = create_constant(operand2)

        return func.call(operand1.evaluate(**variables),
                         operand2.evaluate(**variables))

    def __str__(self):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = str(create_expression(operand1))

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = str(create_expression(operand2))

        return "({} {} {})".format(str(operand1),
                                   func.get_symbol, str(operand2))

    @property
    def variable_names(self):
        names = set()
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = create_expression(operand1)
            names = names.union(operand1.variable_names)
        elif type(operand1) is Expression:
            names = names.union(operand1.variable_names)
        elif type(operand1) is Variable:
            names = names.union(operand1.get_name)

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = create_expression(operand2)
            names = names.union(operand2.variable_names)
        elif type(operand2) is Expression:
            names = names.union(operand2.variable_names)
        elif type(operand2) is Variable:
            names = names.union(operand2.get_name)

        return names


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def create_expression(expression_structure):
    return Expression(expression_structure)

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

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

OK

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

Тина обнови решението на 20.03.2016 15:22 (преди над 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
class Value:
    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, plus, other))

    def __radd__(self, other):
        return Value.__add__(other, self)

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

    def __rsub__(self, other):
        return Value.__sub__(other, self)

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

    def __rmul__(self, other):
        return Value.__mul__(other, self)

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

    def __rtruediv__(self, other):
        return Value.__truediv__(other, self)

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

    def __rfloordiv__(self, other):
        return Value.__floordiv__(other, self)

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((self, mod, other))

    def __rmod__(self, other):
        return Value.__mod__(other, self)

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

    def __rpow__(self, other):
        return Value.__pow__(other, self)

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

    def __rlshift__(self, other):
        return Value.__lshift__(other, self)

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

    def __rrshift__(self, other):
        return Value.__rshift__(other, self)

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

    def __rand__(self, other):
        return Value.__and__(other, self)

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

    def __rxor__(self, other):
        return Value.__xor__(other, self)

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

    def __ror__(self, other):
        return Value.__or__(other, self)


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

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

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


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

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

    def get_name(self):
        return self._name

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


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

    def call(self, *args):
        return self._function(*args)

    def get_symbol(self):
        return self._symbol

    def __str__(self):
        return self._symbol


class Expression(Value):
    def __init__(self, expression_structure):
        self._expression_structure = expression_structure

    def evaluate(self, **variables):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple:
            operand1 = create_constant(create_expression(operand1).
                                       evaluate(**variables))

        if type(operand2) is tuple:
            operand2 = create_constant(create_expression(operand2).
                                       evaluate(**variables))

        if isinstance(operand1, (int, float, complex)):
            operand1 = create_constant(operand1)

        if isinstance(operand2, (int, float, complex)):
            operand2 = create_constant(operand2)

        return func.call(operand1.evaluate(**variables),
                         operand2.evaluate(**variables))

    def __str__(self):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple:
            operand1 = str(create_expression(operand1))

        if type(operand2) is tuple:
            operand2 = str(create_expression(operand2))

        return "({} {} {})".format(str(operand1),
                                   func.get_symbol(), str(operand2))

    @property
    def variable_names(self):
        names = set()
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]

        if type(operand1) is tuple:
            operand1 = create_expression(operand1)
            names = names.union(operand1.variable_names)
        elif type(operand1) is Expression:
            names = names.union(operand1.variable_names)
        elif type(operand1) is Variable:
            names = names.union(operand1.get_name())

        if type(operand2) is tuple:
            operand2 = create_expression(operand2)
            names = names.union(operand2.variable_names)
        elif type(operand2) is Expression:
            names = names.union(operand2.variable_names)
        elif type(operand2) is Variable:
            names = names.union(operand2.get_name())

        return names


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)
  • get_* методите ти биха били далеч по-приятни, ако ползваш property декоратора.
  • evaluate, variable_names и до някъде __str__ биха се получили доста по-добре с повече рекурсия и duck-typing за сметка на всичкото това итеративно обхождане и проверяване на типове.

Тина обнови решението на 21.03.2016 14: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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Value:
    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return create_expression((self, plus, other))

    def __radd__(self, other):
        return Value.__add__(other, self)

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

    def __rsub__(self, other):
        return Value.__sub__(other, self)

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

    def __rmul__(self, other):
        return Value.__mul__(other, self)

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

    def __rtruediv__(self, other):
        return Value.__truediv__(other, self)

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

    def __rfloordiv__(self, other):
        return Value.__floordiv__(other, self)

    def __mod__(self, other):
        mod = create_operator('%', lambda lhs, rhs: lhs % rhs)
        return create_expression((self, mod, other))

    def __rmod__(self, other):
        return Value.__mod__(other, self)

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

    def __rpow__(self, other):
        return Value.__pow__(other, self)

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

    def __rlshift__(self, other):
        return Value.__lshift__(other, self)

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

    def __rrshift__(self, other):
        return Value.__rshift__(other, self)

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

    def __rand__(self, other):
        return Value.__and__(other, self)

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

    def __rxor__(self, other):
        return Value.__xor__(other, self)

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

    def __ror__(self, other):
        return Value.__or__(other, self)


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

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

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


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

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

    @property
    def get_name(self):
        return self._name

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


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

    def call(self, *args):
        return self._function(*args)

    @property
    def get_symbol(self):
        return self._symbol

    def __str__(self):
        return self._symbol


class Expression(Value):
    def __init__(self, expression_structure):
        self._expression_structure = expression_structure

    def evaluate(self, **variables):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = create_constant(create_expression(operand1).
                                       evaluate(**variables))

        elif isinstance(operand1, (int, float, complex)):
            operand1 = create_constant(operand1)

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = create_constant(create_expression(operand2).
                                       evaluate(**variables))

        elif isinstance(operand2, (int, float, complex)):
            operand2 = create_constant(operand2)

        return func.call(operand1.evaluate(**variables),
                         operand2.evaluate(**variables))

    def __str__(self):
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]
        func = self._expression_structure[1]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = str(create_expression(operand1))

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = str(create_expression(operand2))

        return "({} {} {})".format(str(operand1),
                                   func.get_symbol, str(operand2))

    @property
    def variable_names(self):
        names = set()
        operand1 = self._expression_structure[0]
        operand2 = self._expression_structure[2]

        if type(operand1) is tuple or type(operand1) is list:
            operand1 = create_expression(operand1)
            names = names.union(operand1.variable_names)
        elif type(operand1) is Expression:
            names = names.union(operand1.variable_names)
        elif type(operand1) is Variable:
            names = names.union(operand1.get_name)

        if type(operand2) is tuple or type(operand2) is list:
            operand2 = create_expression(operand2)
            names = names.union(operand2.variable_names)
        elif type(operand2) is Expression:
            names = names.union(operand2.variable_names)
        elif type(operand2) is Variable:
            names = names.union(operand2.get_name)

        return names


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)