timeit

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

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

Решение на Аритметични изрази от Веселин Иванов

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

Към профила на Веселин Иванов

Резултати

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

Код

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

    def __repr__(self):
        return repr(self.number)

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

    def evaluate(self, **kwargs):
        return self.number


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

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

    def evaluate(self, **kwargs):
        return kwargs[self.name] if self.name in kwargs else None


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

    def __repr__(self):
        return self.symbol

    def __str__(self):
        return self.symbol

    def __call__(self, left_operand, right_operand):
        return self.function(left_operand, right_operand)


class Expression(object):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = []
        extract_variable_names(structure, self.variable_names)

    def __repr__(self):
        return "({} {} {})".format(*self.structure)

    def __str__(self):
        return self.__repr__()

    def evaluate(self, **kwargs):
        left_operand, operator, right_operand = self.structure
        return operator(left_operand.evaluate(**kwargs),
                        right_operand.evaluate(**kwargs))


def extract_variable_names(structure, names):
    for item in structure:
        if isinstance(item, Variable):
            names.append(item.name)
        if hasattr(item, "__iter__"):
            extract_variable_names(item, names)


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)

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

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

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

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

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

======================================================================
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.123s

FAILED (failures=1, errors=14)

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

Веселин обнови решението на 21.03.2016 16:51 (преди над 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
class Constant:
    def __init__(self, number):
        self.number = number

    def __repr__(self):
        return repr(self.number)

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

    def evaluate(self, **kwargs):
        return self.number


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

    def __repr__(self):
        return self.name

    def __str__(self):
        return self.name

    def evaluate(self, **kwargs):
        return kwargs[self.name] if self.name in kwargs else None


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

    def __repr__(self):
        return self.symbol

    def __str__(self):
        return self.symbol

    def __call__(self, left_operand, right_operand):
        return self.function(left_operand, right_operand)


class Expression(object):
    def __init__(self, structure):
        self.structure = structure
        self.variable_names = []
        extract_variable_names(structure, self.variable_names)

    def __repr__(self):
        return "({} {} {})".format(*self.structure)

    def __str__(self):
        return self.__repr__()

    def evaluate(self, **kwargs):
        left_operand, operator, right_operand = self.structure
        return operator(left_operand.evaluate(**kwargs),
                        right_operand.evaluate(**kwargs))


def extract_variable_names(structure, names):
    for item in structure:
        if isinstance(item, Variable):
            names.append(item.name)
        if hasattr(item, "__iter__"):
            extract_variable_names(item, names)


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)