timeit

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

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

Решение на Аритметични изрази от Стилиян Стоянов

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

Към профила на Стилиян Стоянов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 3 успешни тест(а)
  • 16 неуспешни тест(а)

Код

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
class Operator:

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

    def calculate(self, lhs, rhs):
        result = self.function(lhs, rhs)
        return result

    def __repr__(self):
        return self.sign


class Variable:

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

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

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


class Constant:

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

    def evaluate(self):
        return self.value

    def __add__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() + other

    def __sub__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() - other.evaluate()
        else:
            return self.evaluate() - other

    def __truediv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() / other.evaluate()
        else:
            return self.evaluate() / other

    def __floordiv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() // other

    def __mul__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() * other.evaluate()
        else:
            return self.evaluate() * other

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


class Expression:

    def __init__(self, expressions):
        self.expressions = expressions
        self.variables = self.get_variables(self.expressions)

    def variable_names(self):
        return tuple([var for var in self.variables])

    def get_variables(self, iterable=[]):
        vars = []
        for item in iterable:
            if hasattr(item, '__iter__'):
                print(item)
                vars.extend(self.get_variables(item))
            else:
                if isinstance(item, Variable):
                    vars.append(item)
        return vars

    def evaluate(self, **kwargs):
        return self.evaluate_tuple(self.expressions, **kwargs)

    def evaluate_tuple(self, inner_expr,  **kwargs):
        lhs, operator, rhs = inner_expr
        if isinstance(lhs, Variable):
            lhs = kwargs[str(lhs)]
        elif isinstance(lhs, tuple):
            lhs = self.evaluate_tuple(lhs, **kwargs)
        if isinstance(rhs, Variable):
            rhs = kwargs[str(rhs)]
        elif isinstance(rhs, tuple):
            rhs = self.evaluate_tuple(rhs, **kwargs)
        return operator.calculate(lhs, rhs)

    def __repr__(self):
        print(list(self.expressions))


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.EEEEE.EEEEEEEEE.E
======================================================================
ERROR: test_constant_and_right_hand_side_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'

======================================================================
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 +: 'Variable' and 'int'

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

======================================================================
ERROR: test_constants (test.TestConstants)
----------------------------------------------------------------------
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'

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

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

======================================================================
ERROR: 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
TypeError: __str__ returned non-string (type NoneType)

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

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

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

======================================================================
ERROR: 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
TypeError: __str__ returned non-string (type NoneType)

======================================================================
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 +: 'Variable' 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'

======================================================================
ERROR: test_variables (test.TestVariables)
----------------------------------------------------------------------
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 +: 'Variable' and 'Variable'

----------------------------------------------------------------------
Ran 19 tests in 0.123s

FAILED (errors=16)

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

Стилиян обнови решението на 21.03.2016 16:54 (преди над 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
class Operator:

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

    def calculate(self, lhs, rhs):
        result = self.function(lhs, rhs)
        return result

    def __repr__(self):
        return self.sign


class Variable:

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

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

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


class Constant:

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

    def evaluate(self):
        return self.value

    def __add__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() + other

    def __sub__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() - other.evaluate()
        else:
            return self.evaluate() - other

    def __truediv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() / other.evaluate()
        else:
            return self.evaluate() / other

    def __floordiv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() // other

    def __mul__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() * other.evaluate()
        else:
            return self.evaluate() * other

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


class Expression:

    def __init__(self, expressions):
        self.expressions = expressions
        self.variables = self.get_variables()

    def variable_names(self):
        return tuple([var for var in self.variables])

    def get_variables(self, iterable=[]):
        vars = []
        for item in self.expressions:
            if hasattr(item, '__iter__'):
                print(item)
                vars.extend(self.get_variables(item))
            else:
                if isinstance(item, Variable):
                    vars.append(item)
        return vars

    def evaluate(self, **kwargs):
        return self.evaluate_tuple(self.expressions, **kwargs)

    def evaluate_tuple(self, inner_expr,  **kwargs):
        lhs, operator, rhs = inner_expr
        if isinstance(lhs, Variable):
            lhs = kwargs[str(lhs)]
        elif isinstance(lhs, tuple):
            lhs = self.evaluate_tuple(lhs, **kwargs)
        if isinstance(rhs, Variable):
            rhs = kwargs[str(rhs)]
        elif isinstance(rhs, tuple):
            rhs = self.evaluate_tuple(rhs, **kwargs)
        return operator.calculate(lhs, rhs)

    def __repr__(self):
        print(list(self.expressions))


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)

Стилиян обнови решението на 21.03.2016 16:57 (преди над 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
class Operator:

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

    def calculate(self, lhs, rhs):
        result = self.function(lhs, rhs)
        return result

    def __repr__(self):
        return self.sign


class Variable:

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

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

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


class Constant:

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

    def evaluate(self):
        return self.value

    def __add__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() + other

    def __sub__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() - other.evaluate()
        else:
            return self.evaluate() - other

    def __truediv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() / other.evaluate()
        else:
            return self.evaluate() / other

    def __floordiv__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() + other.evaluate()
        else:
            return self.evaluate() // other

    def __mul__(self, other):
        if isinstance(other, Constant):
            return self.evaluate() * other.evaluate()
        else:
            return self.evaluate() * other

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


class Expression:

    def __init__(self, expressions):
        self.expressions = expressions
        self.variables = self.get_variables(self.expressions)

    def variable_names(self):
        return tuple([var for var in self.variables])

    def get_variables(self, iterable=[]):
        vars = []
        for item in iterable:
            if hasattr(item, '__iter__'):
                print(item)
                vars.extend(self.get_variables(item))
            else:
                if isinstance(item, Variable):
                    vars.append(item)
        return vars

    def evaluate(self, **kwargs):
        return self.evaluate_tuple(self.expressions, **kwargs)

    def evaluate_tuple(self, inner_expr,  **kwargs):
        lhs, operator, rhs = inner_expr
        if isinstance(lhs, Variable):
            lhs = kwargs[str(lhs)]
        elif isinstance(lhs, tuple):
            lhs = self.evaluate_tuple(lhs, **kwargs)
        if isinstance(rhs, Variable):
            rhs = kwargs[str(rhs)]
        elif isinstance(rhs, tuple):
            rhs = self.evaluate_tuple(rhs, **kwargs)
        return operator.calculate(lhs, rhs)

    def __repr__(self):
        print(list(self.expressions))


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)