timeit

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

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

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

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

Към профила на Данаил Димитров

Резултати

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

Код

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
import collections
from functools import reduce

class Operator:
    operators = {}
    def __init__(self, symbol, processor):
        self.symbol = symbol
        self.processor = processor
        Operator.operators[symbol] = self

    @staticmethod
    def get_operator(symbol):
        return Operator.operators[symbol]

class Expression:
    def __init__(self, expression):
        def pre_process(expr):
            expr_out = []
            for item in expr:
                if (isinstance(item, collections.Iterable)):
                    expr_out.append(Expression(item))
                    names.append(expr_out[-1].variable_names)
                else: 
                    expr_out.append(item)

                if (isinstance(item, Variable)):
                    names.append(item._name)
            return (expr_out, tuple(names))

        self.expression, self.variable_names = pre_process(expression)

    def evaluate(self, **variables):
        def eval(expr):
            varibs = []
            for element in expr:
                print(element)
                if (isinstance(element, collections.Iterable)):
                    element = eval(element)

                if (isinstance(element, Variable)):
                    varibs.append(variables[element._name])
                elif (isinstance(element, Constant)):
                    varibs.append(element._value)
                elif (isinstance(element, Operator)):
                    op = element.processor
                else:
                    raise LookupError
            return reduce(op, varibs, 0)
        return eval(self.expression)

    def __str__(self):
        expr = []
        for item in self.expression:
            expr.append(str(item))

        return ' '.join(expr)


    def __add__(self, other):
        op = Operator.get_operator('+')
        return Expression((self, op, other))

    def __sub__(self, other):
        op = Operator.get_operator('-')
        return Expression((self, op, other))

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

    def __str__(self):
        return self._name

class Constant(Summable):
    def __init__(self, value):
        self._value = value

    def __str__(self):
        return setf._value


create_variable = lambda value: Variable(value)
create_constant = lambda name: Constant(name)
create_operator = lambda symbol, processor: Operator(symbol, processor)
create_expression = lambda expr: Expression(expr) 

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

name 'Summable' is not defined
  File "/data/rails/pyfmi-2016/releases/20160307095126/lib/language/python/runner.py", line 114, in main
    loaded_test = imp.load_source('test', test_module)
  File "/usr/local/lib/python3.5/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 693, in _load
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/tmp/d20160322-5045-1j9vys/test.py", line 3, in <module>
    import solution
  File "/tmp/d20160322-5045-1j9vys/solution.py", line 67, in <module>
    class Variable(Summable):

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

Данаил обнови решението на 21.03.2016 14: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
import collections
from functools import reduce

class Operator:
    operators = {}
    def __init__(self, symbol, processor):
        self.symbol = symbol
        self.processor = processor
        Operator.operators[symbol] = self

    @staticmethod
    def get_operator(symbol):
        return Operator.operators[symbol]

class Expression:
    def __init__(self, expression):
        def pre_process(expr):
            expr_out = []
            for item in expr:
                if (isinstance(item, collections.Iterable)):
                    expr_out.append(Expression(item))
                    names.append(expr_out[-1].variable_names)
                else: 
                    expr_out.append(item)

                if (isinstance(item, Variable)):
                    names.append(item._name)
            return (expr_out, tuple(names))

        self.expression, self.variable_names = pre_process(expression)

    def evaluate(self, **variables):
        def eval(expr):
            varibs = []
            for element in expr:
                print(element)
                if (isinstance(element, collections.Iterable)):
                    element = eval(element)

                if (isinstance(element, Variable)):
                    varibs.append(variables[element._name])
                elif (isinstance(element, Constant)):
                    varibs.append(element._value)
                elif (isinstance(element, Operator)):
                    op = element.processor
                else:
                    raise LookupError
            return reduce(op, varibs, 0)
        return eval(self.expression)

    def __str__(self):
        expr = []
        for item in self.expression:
            expr.append(str(item))

        return ' '.join(expr)


    def __add__(self, other):
        op = Operator.get_operator('+')
        return Expression((self, op, other))

    def __sub__(self, other):
        op = Operator.get_operator('-')
        return Expression((self, op, other))

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

    def __str__(self):
        return self._name

class Constant(Summable):
    def __init__(self, value):
        self._value = value

    def __str__(self):
        return setf._value


create_variable = lambda value: Variable(value)
create_constant = lambda name: Constant(name)
create_operator = lambda symbol, processor: Operator(symbol, processor)
create_expression = lambda expr: Expression(expr)