timeit

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

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

Решение на Аритметични изрази от Емануил Антонов

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

Към профила на Емануил Антонов

Резултати

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

Код

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
class Variable:
    def __setattr__(self, name, value):
        self.__dict__[name] = value

    def evaluate(self, **kwargs):
        if len(kwargs):
            key, value = kwargs.popitem()
            if value:
                self.value = value


        return self.value


class Const:
    class ConstError(TypeError):
        pass

    def __setattr__(self, name, value):
        if name in self.__dict__:
            raise self.ConstError("Can't rebind const(%s)" % name)
        self.__dict__[name] = value

    def __delattr__(self, name):
        if name in self.__dict__:
            raise self.ConstError("Can't unbind const(%s)" % name)
        raise NameError(name)

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


class Operator:
    def __setattr__(self, name, value):
        self.__dict__[name] = value

class Expression(object):
    # def __init__(self, formula):
    #     self.formula = formula

    def evaluate(self, **kwargs):
        # a, avalue = kwargs.popitem()
        # b, bvalue = kwargs.popitem()
        for key, value in kwargs.items():
            if not isinstance(self.first, tuple):
                if self.first.name == key:
                    self.first.evaluate(v=value)
            if not isinstance(self.second, tuple):
                if self.second.name == key:
                    self.second.evaluate(v=value)


        if isinstance(self.first, tuple):
            a = create_variable('a')
            a.evaluate(a=create_expression(self.first).evaluate(**kwargs))

            self.first = a

        if isinstance(self.second, tuple):
            b = create_variable('b')
            b.evaluate(b=create_expression(self.second).evaluate(**kwargs))

            self.second = b
        return self.operator.function(self.first.evaluate(), self.second.evaluate())


def create_expression(args):
    expression = Expression()
    expression.first = args[0]
    expression.second = args[2]
    expression.operator = args[1]

    return expression


def create_constant(value):
    const = Const()
    const.value = value
    return const


def create_variable(name):
    variable = Variable()
    variable.name = name
    variable.value = None
    return variable


def create_operator(name, function):
    operator = Operator()
    operator.name = name
    operator.function = function
    return operator


# plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
# minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
# a = create_variable('a')
# b = create_variable('b')
# c = create_variable('c')
#
# print(create_expression((c, plus, (a, plus,(a, minus, b)))).evaluate(a=2, b=3, c=7))

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

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

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

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

======================================================================
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: 'Const' object has no attribute 'name'

======================================================================
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: 'Const' object has no attribute 'name'

======================================================================
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: 'Const' object has no attribute 'name'

======================================================================
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: 'Const' object has no attribute 'name'

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

======================================================================
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_string_conversion (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
AssertionError: '<solution.Const object at 0x7f7d648a5a90>' != '3'
- <solution.Const object at 0x7f7d648a5a90>
+ 3


======================================================================
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: '<solution.Expression object at 0x7f7d663de9e8>' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
- <solution.Expression object at 0x7f7d663de9e8>
+ (((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))


======================================================================
FAIL: 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
AssertionError: '<solution.Expression object at 0x7f7d663de9e8>' != '(x + 5)'
- <solution.Expression object at 0x7f7d663de9e8>
+ (x + 5)


======================================================================
FAIL: test_string_conversion (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
AssertionError: '<solution.Variable object at 0x7f7d642035f8>' != 'x'
- <solution.Variable object at 0x7f7d642035f8>
+ x


----------------------------------------------------------------------
Ran 19 tests in 0.129s

FAILED (failures=4, errors=15)

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

Емануил обнови решението на 21.03.2016 13:39 (преди над 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
class Variable:
    def __setattr__(self, name, value):
        self.__dict__[name] = value

    def evaluate(self, **kwargs):
        if len(kwargs):
            key, value = kwargs.popitem()
            if value:
                self.value = value


        return self.value


class Const:
    class ConstError(TypeError):
        pass

    def __setattr__(self, name, value):
        if name in self.__dict__:
            raise self.ConstError("Can't rebind const(%s)" % name)
        self.__dict__[name] = value

    def __delattr__(self, name):
        if name in self.__dict__:
            raise self.ConstError("Can't unbind const(%s)" % name)
        raise NameError(name)

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


class Operator:
    def __setattr__(self, name, value):
        self.__dict__[name] = value

class Expression(object):
    # def __init__(self, formula):
    #     self.formula = formula

    def evaluate(self, **kwargs):
        # a, avalue = kwargs.popitem()
        # b, bvalue = kwargs.popitem()
        for key, value in kwargs.items():
            if not isinstance(self.first, tuple):
                if self.first.name == key:
                    self.first.evaluate(v=value)
            if not isinstance(self.second, tuple):
                if self.second.name == key:
                    self.second.evaluate(v=value)


        if isinstance(self.first, tuple):
            a = create_variable('a')
            a.evaluate(a=create_expression(self.first).evaluate(**kwargs))

            self.first = a

        if isinstance(self.second, tuple):
            b = create_variable('b')
            b.evaluate(b=create_expression(self.second).evaluate(**kwargs))

            self.second = b
        return self.operator.function(self.first.evaluate(), self.second.evaluate())


def create_expression(args):
    expression = Expression()
    expression.first = args[0]
    expression.second = args[2]
    expression.operator = args[1]

    return expression


def create_constant(value):
    const = Const()
    const.value = value
    return const


def create_variable(name):
    variable = Variable()
    variable.name = name
    variable.value = None
    return variable


def create_operator(name, function):
    operator = Operator()
    operator.name = name
    operator.function = function
    return operator


# plus = create_operator('+', lambda lhs, rhs: lhs + rhs)
# minus = create_operator('-', lambda lhs, rhs: lhs - rhs)
# a = create_variable('a')
# b = create_variable('b')
# c = create_variable('c')
#
# print(create_expression((c, plus, (a, plus,(a, minus, b)))).evaluate(a=2, b=3, c=7))