timeit

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

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

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

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

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

Резултати

  • 3 точки от тестове
  • 1 отнета точка
  • 2 точки общо
  • 6 успешни тест(а)
  • 13 неуспешни тест(а)

Код

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class Expression_member:

    def __init__(self):
        pass

    def __add__(self, right):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __sub__(self, right):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mul__(self, right):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __truediv__(self, right):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __floordiv__(self, right):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mod__(self, right):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)


class Constant(Expression_member):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Variable(Expression_member):

    def __init__(self, name):
        self._name = name

    def evaluate(self, **args):
        return args[self._name]

    def __str__(self):
        return self._name


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def evaluate(self, lhs, rhs):
        return self._function(lhs, rhs)

    def __str__(self):
        return self._symbol


class Expression:

    def __init__(self, expression_structure, var_names):
        self._expression_structure = expression_structure
        self.variable_names = var_names

    def __str__(self):
        expression_string = '('
        for item in self._expression_structure:
            if type(item) is Operator:
                expression_string += ' '
                expression_string += str(item) + ' '
            else:
                expression_string += str(item)
        expression_string += ')'
        return expression_string

    def __radd__(self, right):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rsub__(self, right):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rmul__(self, right):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rtruediv__(self, right):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rfloordiv__(self, right):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rmod__(self, right):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def evaluate(self, **variables):
        fst_operand = self._expression_structure[0]
        operator = self._expression_structure[1]
        snd_operand = self._expression_structure[2]
        lhs = rhs = None
        if isinstance(fst_operand, type(self._expression_structure)):
            print('I am here with')
            print(fst_operand)
            temp_expr = create_expression(fst_operand)
            lhs = temp_expr.evaluate(variables)
        if isinstance(snd_operand, type(self._expression_structure)):
            print('I am there with, ')
            print(snd_operand)
            temp_expr = create_expression(snd_operand)
            print(type(temp_expr))
            rhs = temp_expr.evaluate(variables)
        if (type(fst_operand) is Variable):
            lhs = variables[fst_operand._name]
        else:
            lhs = fst_operand._value
        if (type(snd_operand) is Variable):
            rhs = variables[snd_operand._name]
        else:
            rhs = snd_operand._value
        return operator.evaluate(lhs, rhs)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def get_variable_names(expression_structure):
    variables = []
    for item in expression_structure:
        if isinstance(item, type(expression_structure)):
            get_variable_names(item)
        else:
            if item is Variable:
                variables.append(item._name)


def create_expression(expression_structure):
    var_names = get_variable_names(expression_structure)
    return Expression(expression_structure, var_names)

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

EEEEEE..EEFEE..EE..
======================================================================
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 '_value'

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

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

======================================================================
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
TypeError: evaluate() takes 1 positional argument but 2 were given

======================================================================
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: evaluate() takes 1 positional argument but 2 were given

======================================================================
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
TypeError: evaluate() takes 1 positional argument but 2 were given

======================================================================
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: evaluate() takes 1 positional argument but 2 were given

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

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

----------------------------------------------------------------------
Ran 19 tests in 0.131s

FAILED (failures=1, errors=12)

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

Иван обнови решението на 21.03.2016 16:49 (преди над 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Expression_member:

    def __init__(self):
        pass

    def __add__(self, right):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __sub__(self, right):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mul__(self, right):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __truediv__(self, right):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __floordiv__(self, right):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mod__(self, right):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)


class Constant(Expression_member):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Variable(Expression_member):

    def __init__(self, name):
        self._name = name

    def evaluate(self, **args):
        return args[self._name]

    def __str__(self):
        return self._name


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def evaluate(self, lhs, rhs):
        return self._function(lhs, rhs)

    def __str__(self):
        return self._symbol


class Expression:

    def __init__(self, expression_structure, var_names):
        self._expression_structure = expression_structure
        self.variable_names = var_names

    def __str__(self):
        expression_string = '('
        for item in self._expression_structure:
            if type(item) is Operator:
                expression_string += ' '
                expression_string += str(item) + ' '
            else:
                expression_string += str(item)
        expression_string += ')'
        return expression_string

    def evaluate(self, **variables):
        fst_operand = self._expression_structure[0]
        operator = self._expression_structure[1]
        snd_operand = self._expression_structure[2]
        lhs = rhs = None
        if isinstance(fst_operand, type(self._expression_structure)):
            print('I am here with')
            print(fst_operand)
            temp_expr = create_expression(fst_operand)
            lhs = temp_expr.evaluate(variables)
        if isinstance(snd_operand, type(self._expression_structure)):
            print('I am there with, ')
            print(snd_operand)
            temp_expr = create_expression(snd_operand)
            print(type(temp_expr))
            rhs = temp_expr.evaluate(variables)
        if (type(fst_operand) is Variable):
            lhs = variables[fst_operand._name]
        else:
            lhs = fst_operand._value
        if (type(snd_operand) is Variable):
            rhs = variables[snd_operand._name]
        else:
            rhs = snd_operand._value
        return operator.evaluate(lhs, rhs)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def get_variable_names(expression_structure):
    variables = []
    for item in expression_structure:
        if isinstance(item, type(expression_structure)):
            get_variable_names(item)
        else:
            if item is Variable:
                variables.append(item._name)


def create_expression(expression_structure):
    var_names = get_variable_names(expression_structure)
    return Expression(expression_structure, var_names)

Иван обнови решението на 21.03.2016 16:59 (преди над 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class Expression_member:

    def __init__(self):
        pass

    def __add__(self, right):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __sub__(self, right):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mul__(self, right):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __truediv__(self, right):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __floordiv__(self, right):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)

    def __mod__(self, right):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        expression_structure = self, operator, right
        return create_expression(expression_structure)


class Constant(Expression_member):

    def __init__(self, value):
        self._value = value

    def evaluate(self):
        return self._value

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


class Variable(Expression_member):

    def __init__(self, name):
        self._name = name

    def evaluate(self, **args):
        return args[self._name]

    def __str__(self):
        return self._name


class Operator:

    def __init__(self, symbol, function):
        self._symbol = symbol
        self._function = function

    def evaluate(self, lhs, rhs):
        return self._function(lhs, rhs)

    def __str__(self):
        return self._symbol


class Expression:

    def __init__(self, expression_structure, var_names):
        self._expression_structure = expression_structure
        self.variable_names = var_names

    def __str__(self):
        expression_string = '('
        for item in self._expression_structure:
            if type(item) is Operator:
                expression_string += ' '
                expression_string += str(item) + ' '
            else:
                expression_string += str(item)
        expression_string += ')'
        return expression_string

    def __radd__(self, right):
        operator = Operator('+', lambda lhs, rhs: lhs + rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rsub__(self, right):
        operator = Operator('-', lambda lhs, rhs: lhs - rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rmul__(self, right):
        operator = Operator('*', lambda lhs, rhs: lhs * rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rtruediv__(self, right):
        operator = Operator('/', lambda lhs, rhs: lhs / rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rfloordiv__(self, right):
        operator = Operator('//', lambda lhs, rhs: lhs // rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def __rmod__(self, right):
        operator = Operator('%', lambda lhs, rhs: lhs % rhs)
        expression_structure = right, operator, self
        return create_expression(expression_structure)

    def evaluate(self, **variables):
        fst_operand = self._expression_structure[0]
        operator = self._expression_structure[1]
        snd_operand = self._expression_structure[2]
        lhs = rhs = None
        if isinstance(fst_operand, type(self._expression_structure)):
            print('I am here with')
            print(fst_operand)
            temp_expr = create_expression(fst_operand)
            lhs = temp_expr.evaluate(variables)
        if isinstance(snd_operand, type(self._expression_structure)):
            print('I am there with, ')
            print(snd_operand)
            temp_expr = create_expression(snd_operand)
            print(type(temp_expr))
            rhs = temp_expr.evaluate(variables)
        if (type(fst_operand) is Variable):
            lhs = variables[fst_operand._name]
        else:
            lhs = fst_operand._value
        if (type(snd_operand) is Variable):
            rhs = variables[snd_operand._name]
        else:
            rhs = snd_operand._value
        return operator.evaluate(lhs, rhs)


def create_constant(value):
    return Constant(value)


def create_variable(name):
    return Variable(name)


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


def get_variable_names(expression_structure):
    variables = []
    for item in expression_structure:
        if isinstance(item, type(expression_structure)):
            get_variable_names(item)
        else:
            if item is Variable:
                variables.append(item._name)


def create_expression(expression_structure):
    var_names = get_variable_names(expression_structure)
    return Expression(expression_structure, var_names)