timeit

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

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

Решение на Аритметични изрази от Мартин Терзийски

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

Към профила на Мартин Терзийски

Резултати

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

Код

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
class Constant:
    def __init__(self, value):
        self.value = value

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

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return (create_expression((self,
                                  plus,
                                  other)))

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

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

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

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

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


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

    def __repr__(self):
        return self.name

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return (create_expression((self,
                                  plus,
                                  other)))

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

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

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

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

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


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

    def __repr__(self):
        return self.symbol


class Expression:
    def __init__(self, expression_structure):
        self.expression_structure = expression_structure
        self.variable_names = ()
        if type(expression_structure[0]) is Variable:
            self.variable_names += (str(expression_structure[0]),)
        if type(expression_structure[0]) is Expression:
            self.variable_names += expression_structure[0].variable_names
        if type(expression_structure[2]) is Variable:
            self.variable_names += (str(expression_structure[2]),)
        if type(expression_structure[2]) is Expression:
            self.variable_names += expression_structure[2].variable_names

    def __repr__(self):
        return ("(" + str(self.expression_structure[0]) +
                " " + str(self.expression_structure[1]) +
                " " + str(self.expression_structure[2]) + ")")

    def evaluate(self, **variables):
        if (type(self.expression_structure[0]) is int or
                type(self.expression_structure[0]) is float):
            return (self.expression_structure[1].function
                    (self.expression_structure[0],
                     self.expression_structure[2].evaluate(**variables)))
        elif (type(self.expression_structure[2]) is int or
              type(self.expression_structure[2]) is float):
            return (self.expression_structure[1].function
                    (self.expression_structure[0].evaluate(**variables),
                     self.expression_structure[2]))
        else:
            return (self.expression_structure[1].function
                    (self.expression_structure[0].evaluate(**variables),
                     self.expression_structure[2].evaluate(**variables)))


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)

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

..EEE...EEFEE..EE..
======================================================================
ERROR: test_left_hand_side_literal_and_constant (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
TypeError: unsupported operand type(s) for +: 'int' and 'Constant'

======================================================================
ERROR: test_left_hand_side_literal_and_variable (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
TypeError: unsupported operand type(s) for +: 'int' and 'Variable'

======================================================================
ERROR: 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
TypeError: unsupported operand type(s) for *: 'int' and 'Variable'

======================================================================
ERROR: test_both_nested_operands (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
AttributeError: 'tuple' object has no attribute 'evaluate'

======================================================================
ERROR: test_deep_expression (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
AttributeError: 'tuple' object has no attribute 'evaluate'

======================================================================
ERROR: test_evaluate_with_extra_kwargs (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
AttributeError: 'tuple' object has no attribute 'evaluate'

======================================================================
ERROR: test_one_nested_operand (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
AttributeError: 'tuple' object has no attribute 'evaluate'

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

======================================================================
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'

======================================================================
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)))


----------------------------------------------------------------------
Ran 19 tests in 0.119s

FAILED (failures=1, errors=9)

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

Мартин обнови решението на 21.03.2016 11:48 (преди над 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
class Constant:
    def __init__(self, value):
        self.value = value

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

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return (create_expression((self,
                                  plus,
                                  other)))

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

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

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

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

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


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

    def __repr__(self):
        return self.name

    def __add__(self, other):
        plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
        return (create_expression((self,
                                  plus,
                                  other)))

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

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

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

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

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


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

    def __repr__(self):
        return self.symbol


class Expression:
    def __init__(self, expression_structure):
        self.expression_structure = expression_structure
        self.variable_names = ()
        if type(expression_structure[0]) is Variable:
            self.variable_names += (str(expression_structure[0]),)
        if type(expression_structure[0]) is Expression:
            self.variable_names += expression_structure[0].variable_names
        if type(expression_structure[2]) is Variable:
            self.variable_names += (str(expression_structure[2]),)
        if type(expression_structure[2]) is Expression:
            self.variable_names += expression_structure[2].variable_names

    def __repr__(self):
        return ("(" + str(self.expression_structure[0]) +
                " " + str(self.expression_structure[1]) +
                " " + str(self.expression_structure[2]) + ")")

    def evaluate(self, **variables):
        if (type(self.expression_structure[0]) is int or
                type(self.expression_structure[0]) is float):
            return (self.expression_structure[1].function
                    (self.expression_structure[0],
                     self.expression_structure[2].evaluate(**variables)))
        elif (type(self.expression_structure[2]) is int or
              type(self.expression_structure[2]) is float):
            return (self.expression_structure[1].function
                    (self.expression_structure[0].evaluate(**variables),
                     self.expression_structure[2]))
        else:
            return (self.expression_structure[1].function
                    (self.expression_structure[0].evaluate(**variables),
                     self.expression_structure[2].evaluate(**variables)))


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)