timeit

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

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

Решение на Аритметични изрази от Емил Илиев

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

Към профила на Емил Илиев

Резултати

  • 7 точки от тестове
  • 1 отнета точка
  • 6 точки общо
  • 14 успешни тест(а)
  • 5 неуспешни тест(а)

Код

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


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

    __radd__ = __add__

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

    __rmul__ = __mul__

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

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

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

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

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

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

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

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

    def __lshif__(self, other):
        op = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, op, other))

    def __rshift__(self, other):
        op = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, op, other))

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

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

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

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

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


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

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

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

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


class Variable(Base):

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

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

    def __repr__(self):
        return str(self.name)

    def evaluate(self, **value):
        if self.name in value:
            return value[self.name]


class Operator:

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

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

    def __repr__(self):
        return str(self.symbol)


class Expression(Base):

    def __init__(self, expression_structure):
        self.variable_names = {}
        self.init_variable_names(expression_structure)
        self.exp = expression_structure

    def init_variable_names(self, expression):
        for item in expression:
            if (isinstance(item, Variable)):
                self.variable_names.update({item.name: []})
            elif(isinstance(item, collections.Iterable)):
                self.init_variable_names(item)

    def evaluate(self, **variables):
        for key in variables:
            if key in self.variable_names:
                self.variable_names[key] = variables[key]

        return(self.calculate(self.exp))

    def calculate(self, expr):

        operand = self.check_collection(expr[0])
        if(isinstance(operand, Variable) and
                operand.name in self.variable_names):
            operand = self.variable_names[operand.name]
        if isinstance(operand, Constant):
            operand = operand.value
        if isinstance(operand, Expression):
            operand = operand.evaluate((operand.variable_names))
        operator = expr[1]
        operand1 = self.check_collection(expr[2])
        if (isinstance(operand1, Variable) and
                operand1.name in self.variable_names):
            operand1 = self.variable_names[operand1.name]
        if isinstance(operand1, Constant):
            operand1 = operand1.value
        if isinstance(operand1, Expression):
            operand1 = self.calculate(operand1.exp)
        return operator.function(operand, operand1)

    def check_collection(self, operand):
        if isinstance(operand, collections.Iterable):
            return self.calculate(operand)
        return operand

    def show(self):
        exp_string = "("
        exp_string += self.private_show(self.exp)
        exp_string += ")"
        return exp_string

    def private_show(self, expr):
        result = ""
        for item in expr:
            if(isinstance(item, collections.Iterable)):
                result += "("
                result += self.private_show(item)
                result += ")"
            elif (isinstance(item, Variable)):
                result += item.name
            else:
                result += " " + str(item) + " "
        return result

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


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)

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

....F.....F...FEE..
======================================================================
ERROR: test_variables_and_constants_arithmetics (test.TestNativeOperators)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: evaluate() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_variables_and_constants_extra_operators (test.TestNativeOperators)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
TypeError: evaluate() takes 1 positional argument but 2 were given

======================================================================
FAIL: test_string_conversion_with_literals (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
AssertionError: '( (x +  5 )  -  ( (y *  4 )  *  3 ) )' != '((x + 5) - ((4 * y) * 3))'
- ( (x +  5 )  -  ( (y *  4 )  *  3 ) )
+ ((x + 5) - ((4 * y) * 3))


======================================================================
FAIL: test_deep_string_conversion (test.TestExpressionObjects)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: '(((x -  3 ) + ( 5  - y)) * ((y *  2 ) - ( 5  +  3 )))' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
- (((x -  3 ) + ( 5  - y)) * ((y *  2 ) - ( 5  +  3 )))
?        - -     -  -              - -     -  -  - -
+ (((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))


======================================================================
FAIL: test_simple_string_conversion (test.TestExpressionObjects)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: '(x +  5 )' != '(x + 5)'
- (x +  5 )
?      - -
+ (x + 5)


----------------------------------------------------------------------
Ran 19 tests in 0.160s

FAILED (failures=3, errors=2)

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

Емил обнови решението на 21.03.2016 08:29 (преди над 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
import collections


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

    __radd__ = __add__

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

    __rmul__ = __mul__

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

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

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

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

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

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

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

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

    def __lshif__(self, other):
        op = Operator('<<', lambda lhs, rhs: lhs << rhs)
        return Expression((self, op, other))

    def __rshift__(self, other):
        op = Operator('>>', lambda lhs, rhs: lhs >> rhs)
        return Expression((self, op, other))

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

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

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

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

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


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

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

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

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


class Variable(Base):

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

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

    def __repr__(self):
        return str(self.name)

    def evaluate(self, **value):
        if self.name in value:
            return value[self.name]


class Operator:

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

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

    def __repr__(self):
        return str(self.symbol)


class Expression(Base):

    def __init__(self, expression_structure):
        self.variable_names = {}
        self.init_variable_names(expression_structure)
        self.exp = expression_structure

    def init_variable_names(self, expression):
        for item in expression:
            if (isinstance(item, Variable)):
                self.variable_names.update({item.name: []})
            elif(isinstance(item, collections.Iterable)):
                self.init_variable_names(item)

    def evaluate(self, **variables):
        for key in variables:
            if key in self.variable_names:
                self.variable_names[key] = variables[key]

        return(self.calculate(self.exp))

    def calculate(self, expr):

        operand = self.check_collection(expr[0])
        if(isinstance(operand, Variable) and
                operand.name in self.variable_names):
            operand = self.variable_names[operand.name]
        if isinstance(operand, Constant):
            operand = operand.value
        if isinstance(operand, Expression):
            operand = operand.evaluate((operand.variable_names))
        operator = expr[1]
        operand1 = self.check_collection(expr[2])
        if (isinstance(operand1, Variable) and
                operand1.name in self.variable_names):
            operand1 = self.variable_names[operand1.name]
        if isinstance(operand1, Constant):
            operand1 = operand1.value
        if isinstance(operand1, Expression):
            operand1 = self.calculate(operand1.exp)
        return operator.function(operand, operand1)

    def check_collection(self, operand):
        if isinstance(operand, collections.Iterable):
            return self.calculate(operand)
        return operand

    def show(self):
        exp_string = "("
        exp_string += self.private_show(self.exp)
        exp_string += ")"
        return exp_string

    def private_show(self, expr):
        result = ""
        for item in expr:
            if(isinstance(item, collections.Iterable)):
                result += "("
                result += self.private_show(item)
                result += ")"
            elif (isinstance(item, Variable)):
                result += item.name
            else:
                result += " " + str(item) + " "
        return result

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


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)