timeit

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

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

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

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

Към профила на Денис Михайлов

Резултати

  • 9 точки от тестове
  • 1 бонус точка
  • 10 точки общо
  • 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
from functools import reduce


def convert(argument):
    if isinstance(argument, tuple):
        return Expression(argument)
    elif hasattr(argument, 'evaluate'):
        return argument
    else:
        return Constant(argument)


def to_expression(function):
    def decorator(first, second):
        return Expression((convert(first), function(), convert(second)))
    return decorator


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

    def evaluate(self, **keywords):
        return self.symbol

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

    @property
    def arguments(self):
        return []

    @to_expression
    def __add__():
        return create_operator('+', lambda lhs, rhs: lhs + rhs)

    def __radd__(self, second):
        return Constant.__add__(second, self)

    @to_expression
    def __sub__():
        return create_operator('-', lambda lhs, rhs: lhs - rhs)

    def __rsub__(self, second):
        return Constant.__sub__(second, self)

    @to_expression
    def __mul__():
        return create_operator('*', lambda lhs, rhs: lhs * rhs)

    def __rmul__(self, second):
        return Constant.__mul__(second, self)

    @to_expression
    def __truediv__():
        return create_operator('/', lambda lhs, rhs: lhs / rhs)

    def __rtruediv__(self, second):
        return Constant.__truediv__(second, self)

    __floordiv__ = __truediv__
    __rfloordiv__ = __truediv__


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

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


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

    def evaluate(self, **keywords):
        return keywords[self.symbol]

    @property
    def arguments(self):
        return [str(self)]


class Expression(Constant):
    def __init__(self, expression_structure):
        self.exp = expression_structure

    def evaluate(self, **keywords):
        def evaluate_internal(argument):
            return convert(argument).evaluate(**keywords)
        lhs, rhs = [evaluate_internal(x) for x in [self.exp[0], self.exp[2]]]
        return self.exp[1].evaluate(lhs, rhs)

    @property
    def arguments(self):
        def concat_arguments(variables, item):
            return variables + convert(item).arguments
        return reduce(concat_arguments, [self.exp[0], self.exp[2]], [])

    @property
    def variable_names(self):
        return tuple(self.arguments)

    def __str__(self):
        def formatter(value):
            return str(convert(value))
        return "({} {} {})".format(*(map(formatter, self.exp)))


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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


def create_constant(value):
    return Constant(value)

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

................E..
======================================================================
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: unsupported operand type(s) for ** or pow(): 'Variable' and 'Variable'

----------------------------------------------------------------------
Ran 19 tests in 0.117s

FAILED (errors=1)

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

Денис обнови решението на 18.03.2016 00:00 (преди почти 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
116
117
118
119
120
121
122
123
124
125
126
from functools import reduce


def convert(argument):
    if isinstance(argument, tuple):
        return Expression(argument)
    elif hasattr(argument, 'evaluate'):
        return argument
    else:
        return Constant(argument)


def to_expression(function):
    def decorator(first, second):
        return Expression((convert(first), function(), convert(second)))
    return decorator


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

    def evaluate(self, **keywords):
        return self.symbol

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

    @property
    def arguments(self):
        return []

    @to_expression
    def __add__():
        return create_operator('+', lambda lhs, rhs: lhs + rhs)

    def __radd__(self, second):
        print(self)
        return Constant.__add__(second, self)

    @to_expression
    def __sub__():
        return create_operator('-', lambda lhs, rhs: lhs - rhs)

    def __rsub__(self, second):
        return Constant.__sub__(second, self)

    @to_expression
    def __mul__():
        return create_operator('*', lambda lhs, rhs: lhs * rhs)

    def __rmul__(self, second):
        return Constant.__mul__(second, self)

    @to_expression
    def __truediv__():
        return create_operator('/', lambda lhs, rhs: lhs / rhs)

    def __rtruediv__(self, second):
        return Constant.__truediv__(second, self)

    __floordiv__ = __truediv__
    __rfloordiv__ = __truediv__


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

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


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

    def evaluate(self, **keywords):
        return keywords[self.symbol]

    @property
    def arguments(self):
        return [str(self)]


class Expression(Constant):
    def __init__(self, expression_structure):
        self.exp = expression_structure

    def evaluate(self, **keywords):
        def evaluate_internal(argument):
            return convert(argument).evaluate(**keywords)
        lhs, rhs = [evaluate_internal(x) for x in [self.exp[0], self.exp[2]]]
        return self.exp[1].evaluate(lhs, rhs)

    @property
    def arguments(self):
        def helper(variables, item):
            return variables + convert(item).arguments
        return reduce(helper, [self.exp[0], self.exp[2]], [])

    @property
    def variable_names(self):
        return tuple(self.arguments)

    def __str__(self):
        def formatter(value):
            return str(convert(value))
        return "({} {} {})".format(*(map(formatter, self.exp)))


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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


def create_constant(value):
    return Constant(value)

Денис обнови решението на 20.03.2016 16:13 (преди над 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
from functools import reduce


def convert(argument):
    if isinstance(argument, tuple):
        return Expression(argument)
    elif hasattr(argument, 'evaluate'):
        return argument
    else:
        return Constant(argument)


def to_expression(function):
    def decorator(first, second):
        return Expression((convert(first), function(), convert(second)))
    return decorator


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

    def evaluate(self, **keywords):
        return self.symbol

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

    @property
    def arguments(self):
        return []

    @to_expression
    def __add__():
        return create_operator('+', lambda lhs, rhs: lhs + rhs)

    def __radd__(self, second):
        return Constant.__add__(second, self)

    @to_expression
    def __sub__():
        return create_operator('-', lambda lhs, rhs: lhs - rhs)

    def __rsub__(self, second):
        return Constant.__sub__(second, self)

    @to_expression
    def __mul__():
        return create_operator('*', lambda lhs, rhs: lhs * rhs)

    def __rmul__(self, second):
        return Constant.__mul__(second, self)

    @to_expression
    def __truediv__():
        return create_operator('/', lambda lhs, rhs: lhs / rhs)

    def __rtruediv__(self, second):
        return Constant.__truediv__(second, self)

    __floordiv__ = __truediv__
    __rfloordiv__ = __truediv__


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

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


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

    def evaluate(self, **keywords):
        return keywords[self.symbol]

    @property
    def arguments(self):
        return [str(self)]


class Expression(Constant):
    def __init__(self, expression_structure):
        self.exp = expression_structure

    def evaluate(self, **keywords):
        def evaluate_internal(argument):
            return convert(argument).evaluate(**keywords)
        lhs, rhs = [evaluate_internal(x) for x in [self.exp[0], self.exp[2]]]
        return self.exp[1].evaluate(lhs, rhs)

    @property
    def arguments(self):
        def concat_arguments(variables, item):
            return variables + convert(item).arguments
        return reduce(concat_arguments, [self.exp[0], self.exp[2]], [])

    @property
    def variable_names(self):
        return tuple(self.arguments)

    def __str__(self):
        def formatter(value):
            return str(convert(value))
        return "({} {} {})".format(*(map(formatter, self.exp)))


def create_variable(name):
    return Variable(name)


def create_expression(expression_structure):
    return Expression(expression_structure)


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


def create_constant(value):
    return Constant(value)