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
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 Expresion(expression_structure[0], expression_structure[1], expression_structure[2])


class Constant:
        def __init__(self, x):
                self.x = x

        def evaluate(self, **variables):
                return self.x

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other): 
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


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

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

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other):
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


class Expresion:
        def __init__(self, first, oper, second):
                self.first = first
                self.oper = oper
                self.second = second

        def evaluate(self, **variables):
                return self.oper.evaluate(self.first.evaluate(**variables), self.second.evaluate(**variables))

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other):
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


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

        def evaluate(self, left, right):
                return self.function(left, right)

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

EEEEEE.FEEFEE.F.EF.
======================================================================
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_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 *: 'int' and 'Variable'

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

======================================================================
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.Constant object at 0x7fbfb29e1390>' != '3'
- <solution.Constant object at 0x7fbfb29e1390>
+ 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.Expresion object at 0x7fbfb4bc2a20>' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
- <solution.Expresion object at 0x7fbfb4bc2a20>
+ (((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.Expresion object at 0x7fbfb4bc2a20>' != '(x + 5)'
- <solution.Expresion object at 0x7fbfb4bc2a20>
+ (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 0x7fbfb3071b38>' != 'x'
- <solution.Variable object at 0x7fbfb3071b38>
+ x


----------------------------------------------------------------------
Ran 19 tests in 0.133s

FAILED (failures=4, errors=11)

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

Антонио обнови решението на 21.03.2016 16:55 (преди над 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
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 Expresion(expression_structure[0], expression_structure[1], expression_structure[2])


class Constant:
        def __init__(self, x):
                self.x = x

        def evaluate(self, **variables):
                return self.x

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other): 
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


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

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

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other):
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


class Expresion:
        def __init__(self, first, oper, second):
                self.first = first
                self.oper = oper
                self.second = second

        def evaluate(self, **variables):
                return self.oper.evaluate(self.first.evaluate(**variables), self.second.evaluate(**variables))

        def __add__(self, other):
                return Expresion(self, create_operator('+', lambda lhs, rhs: lhs + rhs), other)

        def __sub__(self, other):
                return Expresion(self, create_operator('-', lambda lhs, rhs: lhs - rhs), other)

        def __mul__(self, other):
                return Expresion(self, create_operator('*', lambda lhs, rhs: lhs * rhs), other)

        def __truediv__(self, other):
                return Expresion(self, create_operator('/', lambda lhs, rhs: lhs / rhs), other)


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

        def evaluate(self, left, right):
                return self.function(left, right)