timeit

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

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

Решение на Аритметични изрази от Марио Даскалов

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

Към профила на Марио Даскалов

Резултати

  • 9 точки от тестове
  • 2 бонус точки
  • 11 точки общо
  • 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
import numbers
import collections
import operator


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

    def evaluate(self, lhs, rhs):
        return self.function(lhs, rhs)

    def __str__(self):
        return self.symbol


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


class ExpressionOperators:
    OPERATORS = (
        ("add", create_operator("+", operator.add)),
        ("sub", create_operator("-", operator.sub)),
        ("mul", create_operator("*", operator.mul)),
        ("truediv", create_operator("/", operator.truediv)),
        ("floordiv", create_operator("//", operator.floordiv)),
        ("mod", create_operator("%", operator.mod)),
        ("pow", create_operator("**", operator.pow)),
        ("lshift", create_operator("<<", operator.lshift)),
        ("rshift", create_operator(">>", operator.rshift)),
        ("and", create_operator("&", operator.and_)),
        ("or", create_operator("|", operator.or_)),
        ("xor", create_operator("^", operator.xor)),
    )

    @classmethod
    def create_operator(cls, operator_name, operator):
        def __normal_operator(self, other):
            if isinstance(other, numbers.Number):
                return Expression(self, operator, Constant(other))
            else:
                return Expression(self, operator, other)

        def __reverse_operator(self, other):
            return Expression(Constant(other), operator, self)

        setattr(cls, "__{}__".format(operator_name), __normal_operator)
        setattr(cls, "__r{}__".format(operator_name), __reverse_operator)


for operator_name, operator in ExpressionOperators.OPERATORS:
    ExpressionOperators.create_operator(operator_name, operator)


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

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

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


def create_variable(name):
    return Variable(name)


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

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

    def variable_names(self):
        return tuple()

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


def create_constant(value):
    return Constant(value)


class Expression(ExpressionOperators):
    def __init__(self, left_expression, operator, right_expression):
        self.left_expression = left_expression
        self.operator = operator
        self.right_expression = right_expression

    def evaluate(self, **variables):
        return self.operator.evaluate(
            self.left_expression.evaluate(**variables),
            self.right_expression.evaluate(**variables)
        )

    def variable_names(self):
        return (self.left_expression.variable_names() +
                self.right_expression.variable_names())

    def __str__(self):
        return "({0} {1} {2})".format(
            str(self.left_expression),
            str(self.operator),
            str(self.right_expression)
        )


def create_expression(expression_structure):
    stack = []
    for token in expression_structure:
        if isinstance(token, collections.Iterable):
            stack.append(create_expression(token))
        else:
            stack.append(token)

        if len(stack) == 3:
            expression = Expression(stack[0], stack[1], stack[2])
            stack.clear()
            stack.append(expression)

    return stack[0]

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

.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.159s

FAILED (errors=1)

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

Марио обнови решението на 17.03.2016 18:11 (преди над 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
class Operator:
    def __init__(self, symbol, function):
        self.symbol = symbol
        self.function = function

    def evaluate(self, lhs, rhs):
        return self.function(lhs, rhs)

    def __str__(self):
        return self.symbol


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


class ExpressionOperators:
    PLUS_OPERATOR = create_operator('+', lambda lhs, rhs: lhs + rhs)
    SUBSTITUTION_OPERATOR = create_operator('-', lambda lhs, rhs: lhs - rhs)
    MULTIPLICATION_OPERATOR = create_operator('*', lambda lhs, rhs: lhs * rhs)
    DIVISION_OPERATOR = create_operator('/', lambda lhs, rhs: lhs / rhs)

    def __add__(self, other):
        return Expression(self, self.PLUS_OPERATOR, other)

    def __sub__(self, other):
        return Expression(self, self.SUBSTITUTION_OPERATOR, other)

    def __mul__(self, other):
        return Expression(self, self.MULTIPLICATION_OPERATOR, other)

    def __truediv__(self, other):
        return Expression(self, self.DIVISION_OPERATOR, other)


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

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

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


def create_variable(name):
    return Variable(name)


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

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

    def variable_names(self):
        return tuple()

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


def create_constant(value):
    return Constant(value)


class Expression(ExpressionOperators):
    def __init__(self, left_expression, operator, right_expression):
        self.left_expression = left_expression
        self.operator = operator
        self.right_expression = right_expression

    def evaluate(self, **variables):
        return self.operator.evaluate(
            self.left_expression.evaluate(**variables),
            self.right_expression.evaluate(**variables)
        )

    def variable_names(self):
        return (self.left_expression.variable_names() +
                self.right_expression.variable_names())

    def __str__(self):
        return "({0} {1} {2})".format(
            str(self.left_expression),
            str(self.operator),
            str(self.right_expression)
        )


def create_expression(expression_structure):
    stack = []
    for token in expression_structure:
        if type(token) == tuple:
            stack.append(create_expression(token))
        else:
            stack.append(token)

        if len(stack) == 3:
            expression = Expression(stack[0], stack[1], stack[2])
            stack.clear()
            stack.append(expression)

    return stack[0]

Марио обнови решението на 18.03.2016 12:01 (преди над 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
import numbers


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

    def evaluate(self, lhs, rhs):
        return self.function(lhs, rhs)

    def __str__(self):
        return self.symbol


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


class ExpressionOperators:
    ADD_OPERATOR = create_operator('+', lambda lhs, rhs: lhs + rhs)
    SUB_OPERATOR = create_operator('-', lambda lhs, rhs: lhs - rhs)
    MUL_OPERATOR = create_operator('*', lambda lhs, rhs: lhs * rhs)
    TRUEDIV_OPERATOR = create_operator('/', lambda lhs, rhs: lhs / rhs)

    def __add__(self, other):
        if isinstance(other, numbers.Number):
            return Expression(self, self.ADD_OPERATOR, Constant(other))
        else:
            return Expression(self, self.ADD_OPERATOR, other)

    def __radd__(self, other):
        return self + other

    def __sub__(self, other):
        if isinstance(other, numbers.Number):
            return Expression(self, self.SUB_OPERATOR, Constant(other))
        else:
            return Expression(self, self.SUB_OPERATOR, other)

    def __rsub__(self, other):
        return Expression(Constant(other), self.SUB_OPERATOR, self)

    def __mul__(self, other):
        if isinstance(other, numbers.Number):
            return Expression(self, self.MUL_OPERATOR, Constant(other))
        else:
            return Expression(self, self.MUL_OPERATOR, other)

    def __rmul__(self, other):
        return self * other

    def __truediv__(self, other):
        if isinstance(other, numbers.Number):
            return Expression(self, self.TRUEDIV_OPERATOR, Constant(other))
        else:
            return Expression(self, self.TRUEDIV_OPERATOR, other)

    def __rtruediv__(self, other):
        return Expression(Constant(other), self.TRUEDIV_OPERATOR, self)


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

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

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


def create_variable(name):
    return Variable(name)


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

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

    def variable_names(self):
        return tuple()

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


def create_constant(value):
    return Constant(value)


class Expression(ExpressionOperators):
    def __init__(self, left_expression, operator, right_expression):
        self.left_expression = left_expression
        self.operator = operator
        self.right_expression = right_expression

    def evaluate(self, **variables):
        return self.operator.evaluate(
            self.left_expression.evaluate(**variables),
            self.right_expression.evaluate(**variables)
        )

    def variable_names(self):
        return (self.left_expression.variable_names() +
                self.right_expression.variable_names())

    def __str__(self):
        return "({0} {1} {2})".format(
            str(self.left_expression),
            str(self.operator),
            str(self.right_expression)
        )


def create_expression(expression_structure):
    stack = []
    for token in expression_structure:
        if type(token) == tuple:
            stack.append(create_expression(token))
        else:
            stack.append(token)

        if len(stack) == 3:
            expression = Expression(stack[0], stack[1], stack[2])
            stack.clear()
            stack.append(expression)

    return stack[0]

Марио обнови решението на 21.03.2016 12: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
import numbers
import operator


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

    def evaluate(self, lhs, rhs):
        return self.function(lhs, rhs)

    def __str__(self):
        return self.symbol


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


class ExpressionOperators:
    OPERATORS = (
        ("add", create_operator("+", operator.add)),
        ("sub", create_operator("-", operator.sub)),
        ("mul", create_operator("*", operator.mul)),
        ("truediv", create_operator("/", operator.truediv)),
        ("floordiv", create_operator("//", operator.floordiv)),
        ("mod", create_operator("%", operator.mod)),
        ("pow", create_operator("**", operator.pow)),
        ("lshift", create_operator("<<", operator.lshift)),
        ("rshift", create_operator(">>", operator.rshift)),
        ("and", create_operator("&", operator.and_)),
        ("or", create_operator("|", operator.or_)),
        ("xor", create_operator("^", operator.xor)),
    )

    @classmethod
    def create_operator(cls, operator_name, operator):
        def __normal_operator(self, other):
            if isinstance(other, numbers.Number):
                return Expression(self, operator, Constant(other))
            else:
                return Expression(self, operator, other)

        def __reverse_operator(self, other):
            return Expression(Constant(other), operator, self)

        setattr(cls, "__{}__".format(operator_name), __normal_operator)
        setattr(cls, "__r{}__".format(operator_name), __reverse_operator)


for operator_name, operator in ExpressionOperators.OPERATORS:
    ExpressionOperators.create_operator(operator_name, operator)


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

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

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


def create_variable(name):
    return Variable(name)


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

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

    def variable_names(self):
        return tuple()

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


def create_constant(value):
    return Constant(value)


class Expression(ExpressionOperators):
    def __init__(self, left_expression, operator, right_expression):
        self.left_expression = left_expression
        self.operator = operator
        self.right_expression = right_expression

    def evaluate(self, **variables):
        return self.operator.evaluate(
            self.left_expression.evaluate(**variables),
            self.right_expression.evaluate(**variables)
        )

    def variable_names(self):
        return (self.left_expression.variable_names() +
                self.right_expression.variable_names())

    def __str__(self):
        return "({0} {1} {2})".format(
            str(self.left_expression),
            str(self.operator),
            str(self.right_expression)
        )


def create_expression(expression_structure):
    stack = []
    for token in expression_structure:
        if type(token) == tuple:
            stack.append(create_expression(token))
        else:
            stack.append(token)

        if len(stack) == 3:
            expression = Expression(stack[0], stack[1], stack[2])
            stack.clear()
            stack.append(expression)

    return stack[0]

Марио обнови решението на 21.03.2016 14:37 (преди над 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
import numbers
import collections
import operator


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

    def evaluate(self, lhs, rhs):
        return self.function(lhs, rhs)

    def __str__(self):
        return self.symbol


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


class ExpressionOperators:
    OPERATORS = (
        ("add", create_operator("+", operator.add)),
        ("sub", create_operator("-", operator.sub)),
        ("mul", create_operator("*", operator.mul)),
        ("truediv", create_operator("/", operator.truediv)),
        ("floordiv", create_operator("//", operator.floordiv)),
        ("mod", create_operator("%", operator.mod)),
        ("pow", create_operator("**", operator.pow)),
        ("lshift", create_operator("<<", operator.lshift)),
        ("rshift", create_operator(">>", operator.rshift)),
        ("and", create_operator("&", operator.and_)),
        ("or", create_operator("|", operator.or_)),
        ("xor", create_operator("^", operator.xor)),
    )

    @classmethod
    def create_operator(cls, operator_name, operator):
        def __normal_operator(self, other):
            if isinstance(other, numbers.Number):
                return Expression(self, operator, Constant(other))
            else:
                return Expression(self, operator, other)

        def __reverse_operator(self, other):
            return Expression(Constant(other), operator, self)

        setattr(cls, "__{}__".format(operator_name), __normal_operator)
        setattr(cls, "__r{}__".format(operator_name), __reverse_operator)


for operator_name, operator in ExpressionOperators.OPERATORS:
    ExpressionOperators.create_operator(operator_name, operator)


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

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

    def variable_names(self):
        return (self.name,)

    def __str__(self):
        return self.name


def create_variable(name):
    return Variable(name)


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

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

    def variable_names(self):
        return tuple()

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


def create_constant(value):
    return Constant(value)


class Expression(ExpressionOperators):
    def __init__(self, left_expression, operator, right_expression):
        self.left_expression = left_expression
        self.operator = operator
        self.right_expression = right_expression

    def evaluate(self, **variables):
        return self.operator.evaluate(
            self.left_expression.evaluate(**variables),
            self.right_expression.evaluate(**variables)
        )

    def variable_names(self):
        return (self.left_expression.variable_names() +
                self.right_expression.variable_names())

    def __str__(self):
        return "({0} {1} {2})".format(
            str(self.left_expression),
            str(self.operator),
            str(self.right_expression)
        )


def create_expression(expression_structure):
    stack = []
    for token in expression_structure:
        if isinstance(token, collections.Iterable):
            stack.append(create_expression(token))
        else:
            stack.append(token)

        if len(stack) == 3:
            expression = Expression(stack[0], stack[1], stack[2])
            stack.clear()
            stack.append(expression)

    return stack[0]