timeit

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

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

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

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

Към профила на Александра Матева

Резултати

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

Код

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

    def __add__(self, other):
        return self._value + other._value

    def __sub__(self, other):
        return self._value - other._value

    def __mul__(self, other):
        return self._value * other._value

    def __truediv__(self, other):
        return self._value / other._value

    def __floordiv__(self, other):
        return self._value // other._value

    def __mod__(self, other):
        return self._value % other._value

    def __divmod__(self, other):
        return divmod(self._value, other._value)

    def __pow__(self, other):
        return self._value ** other._value

    def __lshift__(self, other):
        return self._value << other._value

    def __rshift__(self, other):
        return self._value >> other._value

    def __and__(self, other):
        return self._value & other._value

    def __xor__(self, other):
        return self._value ^ other._value

    def __or__(self, other):
        return self._value | other._value


def evaluate(self, **variables):
    if self.__class__ is Variable:
        v = list(variables.keys())
        setattr(self, 'res', variables[v[0]])
        return int(self.res)


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

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

    def evaluate(self):
        return float(self._value)


class Variable(Base):
    def __init__(self, value):
        self._value = value
        self.variable_names = ()

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

    def evaluate(self, **variables):
        v = list(variables.keys())
        setattr(self, 'res', variables[v[0]])
        return int(self.res)


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

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


class Expression(Variable, Constant, Operator):
    def __init__(self, value):
        self.ar1 = value[0]
        self.ar2 = value[2]
        self.variable_names = ()
        if self.ar1.__class__ is Variable:
            self.variable_names += (str(self.ar1),)
        if self.ar2.__class__ is Variable:
            self.variable_names += (str(self.ar2),)
        else:
            create_expression(value[2])
        self.op = value[1]

    def evaluate(self, **variables):
        v = list(variables.keys())
        return self.op.function(variables[v[0]], variables[v[1]])


def create_constant(value):
    return Constant(value)


def create_variable(variable):
    return Variable(variable)


def create_expression(value):
    return Expression(value)


def create_operator(opr, funk):
    return Operator(opr, funk)

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

EEEEEEE.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 '_value'

======================================================================
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
TypeError: 'int' object is not subscriptable

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

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

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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: 'Constant' object does not support indexing

======================================================================
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 -: 'str' and 'str'

======================================================================
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(): 'str' and 'str'

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

----------------------------------------------------------------------
Ran 19 tests in 0.126s

FAILED (errors=17)

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

Александра обнови решението на 21.03.2016 14:06 (преди над 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
class Base:

    def __add__(self, other):
        return self._value + other._value

    def __sub__(self, other):
        return self._value - other._value

    def __mul__(self, other):
        return self._value * other._value

    def __truediv__(self, other):
        return self._value / other._value

    def __floordiv__(self, other):
        return self._value // other._value

    def __mod__(self, other):
        return self._value % other._value

    def __divmod__(self, other):
        return divmod(self._value, other._value)

    def __pow__(self, other):
        return self._value ** other._value

    def __lshift__(self, other):
        return self._value << other._value

    def __rshift__(self, other):
        return self._value >> other._value

    def __and__(self, other):
        return self._value & other._value

    def __xor__(self, other):
        return self._value ^ other._value

    def __or__(self, other):
        return self._value | other._value


def evaluate(self, **variables):
    if self.__class__ is Variable:
        v = list(variables.keys())
        setattr(self, 'res', variables[v[0]])
        return int(self.res)


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

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

    def evaluate(self):
        return float(self._value)


class Variable(Base):
    def __init__(self, value):
        self._value = value
        self.variable_names = ()

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

    def evaluate(self, **variables):
        v = list(variables.keys())
        setattr(self, 'res', variables[v[0]])
        return int(self.res)


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

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


class Expression(Variable, Constant, Operator):
    def __init__(self, value):
        self.ar1 = value[0]
        self.ar2 = value[2]
        self.variable_names = ()
        if self.ar1.__class__ is Variable:
            self.variable_names += (str(self.ar1),)
        if self.ar2.__class__ is Variable:
            self.variable_names += (str(self.ar2),)
        else:
            create_expression(value[2])
        self.op = value[1]

    def evaluate(self, **variables):
        v = list(variables.keys())
        return self.op.function(variables[v[0]], variables[v[1]])


def create_constant(value):
    return Constant(value)


def create_variable(variable):
    return Variable(variable)


def create_expression(value):
    return Expression(value)


def create_operator(opr, funk):
    return Operator(opr, funk)