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
class Operator:
    def __init__(self, symbol, function):
        self.__symbol = symbol
        self.__function = function

    def apply(self, left, right):
        return self.__function(left, right)

    def __str__(self):
        return self.__symbol


class Operators:
    __or = Operator('|', lambda lhs, rhs: lhs | rhs)
    __and = Operator('&', lambda lhs, rhs: lhs & rhs)
    __xor = Operator('^', lambda lhs, rhs: lhs ^ rhs)
    __add = Operator('+', lambda lhs, rhs: lhs + rhs)
    __sub = Operator('-', lambda lhs, rhs: lhs - rhs)
    __mul = Operator('*', lambda lhs, rhs: lhs * rhs)
    __mod = Operator('%', lambda lhs, rhs: lhs % rhs)
    __pow = Operator('**', lambda lhs, rhs: lhs ** rhs)
    __truediv = Operator('/', lambda lhs, rhs: lhs / rhs)
    __lshift = Operator('<<', lambda lhs, rhs: lhs << rhs)
    __rshift = Operator('>>', lambda lhs, rhs: lhs >> rhs)
    __floordiv = Operator('//', lambda lhs, rhs: lhs // rhs)

    def __helper(self, function, other):
        right = other if isinstance(other, Operators) else Constant(other)
        return Expression((self, function, right))

    def __rhelper(self, function, other):
        right = other if isinstance(other, Operators) else Constant(other)
        return Expression((right, function, self))

    def __add__(self, other):
        return self.__helper(self.__add, other)

    def __radd__(self, other):
        return self.__rhelper(self.__add, other)

    def __sub__(self, other):
        return self.__helper(self.__sub, other)

    def __rsub__(self, other):
        return self.__rhelper(self.__sub, other)

    def __mul__(self, other):
        return self.__helper(self.__mul, other)

    def __rmul__(self, other):
        return self.__rhelper(self.__mul, other)

    def __truediv__(self, other):
        return self.__helper(self.__truediv, other)

    def __rtruediv__(self, other):
        return self.__rhelper(self.__truediv, other)

    def __floordiv__(self, other):
        return self.__helper(self.__floordiv, other)

    def __rfloordiv__(self, other):
        return self.__rhelper(self.__floordiv, other)

    def __mod__(self, other):
        return self.__helper(self.__mod, other)

    def __rmod__(self, other):
        return self.__rhelper(self.__mod, other)

    def __pow__(self, other):
        return self.__helper(self.__pow, other)

    def __rpow__(self, other):
        return self.__rhelper(self.__pow, other)

    def __lshift__(self, other):
        return self.__helper(self.__lshift, other)

    def __rlshift__(self, other):
        return self.__rhelper(self.__lshift, other)

    def __rshift__(self, other):
        return self.__helper(self.__rshift, other)

    def __rrshift__(self, other):
        return self.__rhelper(self.__rshift, other)

    def __and__(self, other):
        return self.__helper(self.__and, other)

    def __rand__(self, other):
        return self.__rhelper(self.__and, other)

    def __xor__(self, other):
        return self.__helper(self.__xor, other)

    def __rxor__(self, other):
        return self.__rhelper(self.__xor, other)

    def __or__(self, other):
        return self.__helper(self.__or, other)

    def __ror__(self, other):
        return self.__rhelper(self.__or, other)


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

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

    @property
    def variable_names(self):
        return []

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


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

    def evaluate(self, **values):
        return values[self.__name]

    @property
    def variable_names(self):
        return [self.__name]

    def __str__(self):
        return self.__name


class Expression(Operators):
    def __init__(self, expression):
        self.__operator = expression[1]
        self.__left_expr = self.__helper(expression[0])
        self.__right_expr = self.__helper(expression[2])

    def __helper(self, object):
        if hasattr(object, '__iter__'):
            return Expression(object)
        else:
            return object

    def evaluate(self, **variables):
        return self.__operator.apply(
            self.__left_expr.evaluate(**variables),
            self.__right_expr.evaluate(**variables))

    @property
    def variable_names(self):
        left_names = self.__left_expr.variable_names
        right_names = self.__right_expr.variable_names
        return left_names + right_names

    def __str__(self):
        left = str(self.__left_expr)
        operator = str(self.__operator)
        right = str(self.__right_expr)
        return '({} {} {})'.format(left, operator, right)


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
AttributeError: 'int' object has no attribute 'evaluate'

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

FAILED (errors=1)

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

Тодор обнови решението на 18.03.2016 20:12 (преди над 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
class Operator:
    def __init__(self, symbol, function):
        self.__symbol = symbol
        self.__function = function

    def apply(self, left, right):
        return self.__function(left, right)

    def __str__(self):
        return self.__symbol


class Operators:
    __add = Operator('+', lambda lhs, rhs: lhs + rhs)
    __sub = Operator('-', lambda lhs, rhs: lhs - rhs)
    __mul = Operator('*', lambda lhs, rhs: lhs * rhs)
    __div = Operator('/', lambda lhs, rhs: lhs / rhs)

    def __add__(self, other):
        if isinstance(other, Operators):
            return Expression((self, self.__add, other))
        else:
            return Expression((self, self.__add, Constant(other)))

    def __radd__(self, other):
        if isinstance(other, Operators):
            return Expression((other, self.__add, self))
        else:
            return Expression((Constant(other), self.__add, self))

    def __sub__(self, other):
        if isinstance(other, Operators):
            return Expression((self, self.__sub, other))
        else:
            return Expression((self, self.__sub, Constant(other)))

    def __rsub__(self, other):
        if isinstance(other, Operators):
            return Expression((other, self.__sub, self))
        else:
            return Expression((Constant(other), self.__sub, self))

    def __mul__(self, other):
        if isinstance(other, Operators):
            return Expression((self, self.__mul, other))
        else:
            return Expression((self, self.__mul, Constant(other)))

    def __rmul__(self, other):
        if isinstance(other, Operators):
            return Expression((other, self.__mul, self))
        else:
            return Expression((Constant(other), self.__mul, self))

    def __truediv__(self, other):
        if isinstance(other, Operators):
            return Expression((self, self.__div, other))
        else:
            return Expression((self, self.__div, Constant(other)))

    def __rtruediv__(self, other):
        if isinstance(other, Operators):
            return Expression((other, self.__div, self))
        else:
            return Expression((Constant(other), self.__div, self))


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

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

    @property
    def variable_names(self):
        return []

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


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

    def evaluate(self, **values):
        return values[self.__name]

    @property
    def variable_names(self):
        return [self.__name]

    def __str__(self):
        return self.__name


class Expression(Operators):
    def __init__(self, expression):
        if hasattr(expression[0], '__iter__'):
            self.__left_expr = Expression(expression[0])
        else:
            self.__left_expr = expression[0]

        if hasattr(expression[2], '__iter__'):
            self.__right_expr = Expression(expression[2])
        else:
            self.__right_expr = expression[2]

        self.__operator = expression[1]

    def evaluate(self, **variables):
        return self.__operator.apply(
            self.__left_expr.evaluate(**variables),
            self.__right_expr.evaluate(**variables))

    @property
    def variable_names(self):
        left_names = self.__left_expr.variable_names
        right_names = self.__right_expr.variable_names
        return left_names + right_names

    def __str__(self):
        left = str(self.__left_expr)
        operator = str(self.__operator)
        right = str(self.__right_expr)
        return '(' + left + ' ' + operator + ' ' + right + ')'


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)

Тодор обнови решението на 19.03.2016 22:56 (преди над 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
class Operator:
    def __init__(self, symbol, function):
        self.__symbol = symbol
        self.__function = function

    def apply(self, left, right):
        return self.__function(left, right)

    def __str__(self):
        return self.__symbol


class Operators:
    __or = Operator('|', lambda lhs, rhs: lhs | rhs)
    __and = Operator('&', lambda lhs, rhs: lhs & rhs)
    __xor = Operator('^', lambda lhs, rhs: lhs ^ rhs)
    __add = Operator('+', lambda lhs, rhs: lhs + rhs)
    __sub = Operator('-', lambda lhs, rhs: lhs - rhs)
    __mul = Operator('*', lambda lhs, rhs: lhs * rhs)
    __mod = Operator('%', lambda lhs, rhs: lhs % rhs)
    __pow = Operator('**', lambda lhs, rhs: lhs ** rhs)
    __truediv = Operator('/', lambda lhs, rhs: lhs / rhs)
    __lshift = Operator('<<', lambda lhs, rhs: lhs << rhs)
    __rshift = Operator('>>', lambda lhs, rhs: lhs >> rhs)
    __floordiv = Operator('//', lambda lhs, rhs: lhs // rhs)

    def __helper(self, function, other):
        right = other if isinstance(other, Operators) else Constant(other)
        return Expression((self, function, right))

    def __rhelper(self, function, other):
        right = other if isinstance(other, Operators) else Constant(other)
        return Expression((right, function, self))

    def __add__(self, other):
        return self.__helper(self.__add, other)

    def __radd__(self, other):
        return self.__rhelper(self.__add, other)

    def __sub__(self, other):
        return self.__helper(self.__sub, other)

    def __rsub__(self, other):
        return self.__rhelper(self.__sub, other)

    def __mul__(self, other):
        return self.__helper(self.__mul, other)

    def __rmul__(self, other):
        return self.__rhelper(self.__mul, other)

    def __truediv__(self, other):
        return self.__helper(self.__truediv, other)

    def __rtruediv__(self, other):
        return self.__rhelper(self.__truediv, other)

    def __floordiv__(self, other):
        return self.__helper(self.__floordiv, other)

    def __rfloordiv__(self, other):
        return self.__rhelper(self.__floordiv, other)

    def __mod__(self, other):
        return self.__helper(self.__mod, other)

    def __rmod__(self, other):
        return self.__rhelper(self.__mod, other)

    def __pow__(self, other):
        return self.__helper(self.__pow, other)

    def __rpow__(self, other):
        return self.__rhelper(self.__pow, other)

    def __lshift__(self, other):
        return self.__helper(self.__lshift, other)

    def __rlshift__(self, other):
        return self.__rhelper(self.__lshift, other)

    def __rshift__(self, other):
        return self.__helper(self.__rshift, other)

    def __rrshift__(self, other):
        return self.__rhelper(self.__rshift, other)

    def __and__(self, other):
        return self.__helper(self.__and, other)

    def __rand__(self, other):
        return self.__rhelper(self.__and, other)

    def __xor__(self, other):
        return self.__helper(self.__xor, other)

    def __rxor__(self, other):
        return self.__rhelper(self.__xor, other)

    def __or__(self, other):
        return self.__helper(self.__or, other)

    def __ror__(self, other):
        return self.__rhelper(self.__or, other)


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

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

    @property
    def variable_names(self):
        return []

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


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

    def evaluate(self, **values):
        return values[self.__name]

    @property
    def variable_names(self):
        return [self.__name]

    def __str__(self):
        return self.__name


class Expression(Operators):
    def __init__(self, expression):
        self.__operator = expression[1]
        self.__left_expr = self.__helper(expression[0])
        self.__right_expr = self.__helper(expression[2])

    def __helper(self, object):
        if hasattr(object, '__iter__'):
            return Expression(object)
        else:
            return object

    def evaluate(self, **variables):
        return self.__operator.apply(
            self.__left_expr.evaluate(**variables),
            self.__right_expr.evaluate(**variables))

    @property
    def variable_names(self):
        left_names = self.__left_expr.variable_names
        right_names = self.__right_expr.variable_names
        return left_names + right_names

    def __str__(self):
        left = str(self.__left_expr)
        operator = str(self.__operator)
        right = str(self.__right_expr)
        return '({} {} {})'.format(left, operator, right)


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)