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
def create_constant(value):
    return Expression(value, None)


def create_variable(name):
    return Expression(name, None)


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


def create_expression(expression_structure):
    return Expression(expression_structure, None)


class Expression:

    def __init__(self, argument, function):

        self.variable_names = []
        self.variable = None
        self.const_number = None
        self.operator = None
        self.function = function
        self.operand1 = None
        self.operand2 = None

        if type(argument) == str and function is None:
            self.variable = argument
            self.variable_names.append(argument)
        elif type(argument) == int:
            self.const_number = argument
        elif type(argument) == float:
            self.const_number = argument
        elif type(argument) == complex:
            self.const_number = argument
        elif type(argument) == str and function is not None:
            self.operator = argument
        else:
            self.operand1 = argument[0]
            self.operand2 = argument[2]
            self.variable_names.append(self.operand1.variable)
            self.variable_names.append(self.operand2.variable)
            self.operator = argument[1].operator
            self.function = argument[1].function

    def __repr__(self):
        str1 = '(' + str(self.operand1)
        str2 = str(self.operator)
        str3 = str(self.operand2) + ')'
        return str1 + ' ' + str2 + ' ' + str3

    def __str__(self):

        if self.const_number is not None:
            return str(self.const_number)
        elif self.variable is not None:
            return self.variable
        elif self.operator is not None and self.operand1 is None:
            return self.operator
        else:
            str1 = '(' + str(self.operand1)
            str2 = str(self.operator)
            str3 = str(self.operand2) + ')'
            return str1 + ' ' + str2 + ' ' + str3

    def evaluate(self, *args, **variables):
        if self.const_number is not None:
            return self.const_number
        elif self.variable is not None:
            return variables[self.variable]
        else:
            num1 = self.operand1.evaluate(**variables)
            num2 = self.operand2.evaluate(**variables)
            return self.function(num1, num2)

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

EEEEEEE.EEEEE..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 +: 'Expression' 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 'variable'

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

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

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

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

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

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

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

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

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

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

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

----------------------------------------------------------------------
Ran 19 tests in 0.128s

FAILED (errors=15)

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

Пламен обнови решението на 21.03.2016 01:11 (преди над 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
def create_constant(value):
    return Expression(value, None)


def create_variable(name):
    return Expression(name, None)


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


def create_expression(expression_structure):
    return Expression(expression_structure, None)


class Expression:

    def __init__(self, argument, function):

        self.variable_names = []
        self.variable = None
        self.const_number = None
        self.operator = None
        self.function = function
        self.operand1 = None
        self.operand2 = None

        if type(argument) == str and function is None:
            self.variable = argument
            self.variable_names.append(argument)
        elif type(argument) == int:
            self.const_number = argument
        elif type(argument) == float:
            self.const_number = argument
        elif type(argument) == complex:
            self.const_number = argument
        elif type(argument) == str and function is not None:
            self.operator = argument
        else:
            self.operand1 = argument[0]
            self.operand2 = argument[2]
            self.variable_names.append(self.operand1.variable)
            self.variable_names.append(self.operand2.variable)
            self.operator = argument[1].operator
            self.function = argument[1].function

    def __repr__(self):
        str1 = '(' + str(self.operand1)
        str2 = str(self.operator)
        str3 = str(self.operand2) + ')'
        return str1 + ' ' + str2 + ' ' + str3

    def __str__(self):

        if self.const_number is not None:
            return str(self.const_number)
        elif self.variable is not None:
            return self.variable
        elif self.operator is not None and self.operand1 is None:
            return self.operator
        else:
            str1 = '(' + str(self.operand1)
            str2 = str(self.operator)
            str3 = str(self.operand2) + ')'
            return str1 + ' ' + str2 + ' ' + str3

    def evaluate(self, *args, **variables):
        if self.const_number is not None:
            return self.const_number
        elif self.variable is not None:
            return variables[self.variable]
        else:
            num1 = self.operand1.evaluate(**variables)
            num2 = self.operand2.evaluate(**variables)
            return self.function(num1, num2)