timeit

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

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

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

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

Към профила на Илиан Стаменов

Резултати

  • 7 точки от тестове
  • 2 отнети точки
  • 5 точки общо
  • 14 успешни тест(а)
  • 5 неуспешни тест(а)

Код

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
class base:
    def __str__(self):
        return str(self.value)


for fun, op in [("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"),
                ("floordiv", "//"), ("mod", "%"), ("divmod", "divmod?"),
                ("pow", "**"), ("lshift", "<<"), ("rshift", ">>"),
                ("and", "&"), ("xor", "^"), ("or", "|")]:
    exec("setattr(base, \'__{0}__\', lambda self, other: expr((self, \
          op(\'{1}\', lambda l, r: l.__{0}__(r)), other)))".format(fun, op))
    exec("setattr(base, \'__r{0}__\', lambda self, other: base.__{0}__(other, \
          self))".format(fun))


class constant(base):
    def __init__(self, value):
        self.value = value
        self.variable_names = []

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


def create_constant(value):
    return constant(value)


class variable(base):
    def __init__(self, value):
        self.value = value
        self.variable_names = [value]

    def evaluate(self, **vars):
        return vars[self.value]


def create_variable(value):
    return variable(value)


class op:
    def __init__(self, value, action):
        self.value = value
        self.action = action

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

    def __call__(self, left, right):
        return self.action(left, right)


def create_operator(sign, action):
    return op(sign, action)


class expr(base):
    def __init__(self, expression):
        try:
            self.right = expr.createExpr(expression[2])
            self.op = expression[1]
            self.left = expr.createExpr(expression[0])
            self.variable_names = self.left.variable_names + \
                self.right.variable_names
        except TypeError:
            self = expr.createExpr(expression)

    def __str__(self):
        return "(" + str(self.left) + " " + str(self.op) + " " +\
               str(self.right) + ")"

    @staticmethod
    def createExpr(e):
        if type(e) is expr or type(e) is variable or type(e) is constant:
            return e
        elif type(e) is str:
            return variable(e)
        else:
            return constant(e)

    def evaluate(self, **vars):
        left = self.left.evaluate(**vars)
        right = self.right.evaluate(**vars)
        return self.op(left, right)


def create_expression(expression_structure):
    return expr(expression_structure)

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

........FEFFE......
======================================================================
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: can't multiply sequence by non-int of type 'tuple'

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

======================================================================
FAIL: 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
AssertionError: (<solution.variable object at 0x7fcc4eccc[218 chars]4a8>) != 11

======================================================================
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.variable object at 0x7fcc4ecccb[573 chars]>)))' != '(((x - 3) + (5 - y)) * ((y * 2) - (5 + 3)))'
Diff is 671 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: 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
AssertionError: (<solution.variable object at 0x7fcc4ecd5[218 chars]0f0>) != 11

----------------------------------------------------------------------
Ran 19 tests in 0.119s

FAILED (failures=3, errors=2)

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

Илиан обнови решението на 21.03.2016 16:57 (преди над 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
class base:
    def __str__(self):
        return str(self.value)


for fun, op in [("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"),
                ("floordiv", "//"), ("mod", "%"), ("divmod", "divmod?"),
                ("pow", "**"), ("lshift", "<<"), ("rshift", ">>"),
                ("and", "&"), ("xor", "^"), ("or", "|")]:
    exec("setattr(base, \'__{0}__\', lambda self, other: expr((self, \
          op(\'{1}\', lambda l, r: l.__{0}__(r)), other)))".format(fun, op))
    exec("setattr(base, \'__r{0}__\', lambda self, other: base.__{0}__(other, \
          self))".format(fun))


class constant(base):
    def __init__(self, value):
        self.value = value
        self.variable_names = []

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


def create_constant(value):
    return constant(value)


class variable(base):
    def __init__(self, value):
        self.value = value
        self.variable_names = [value]

    def evaluate(self, **vars):
        return vars[self.value]


def create_variable(value):
    return variable(value)


class op:
    def __init__(self, value, action):
        self.value = value
        self.action = action

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

    def __call__(self, left, right):
        return self.action(left, right)


def create_operator(sign, action):
    return op(sign, action)


class expr(base):
    def __init__(self, expression):
        try:
            self.right = expr.createExpr(expression[2])
            self.op = expression[1]
            self.left = expr.createExpr(expression[0])
            self.variable_names = self.left.variable_names + \
                self.right.variable_names
        except TypeError:
            self = expr.createExpr(expression)

    def __str__(self):
        return "(" + str(self.left) + " " + str(self.op) + " " +\
               str(self.right) + ")"

    @staticmethod
    def createExpr(e):
        if type(e) is expr or type(e) is variable or type(e) is constant:
            return e
        elif type(e) is str:
            return variable(e)
        else:
            return constant(e)

    def evaluate(self, **vars):
        left = self.left.evaluate(**vars)
        right = self.right.evaluate(**vars)
        return self.op(left, right)


def create_expression(expression_structure):
    return expr(expression_structure)