|
1 | 1 | from math import floor
|
2 |
| -from collections import deque |
3 | 2 |
|
4 |
| -''' |
5 |
| - Note: |
6 |
| - We need only the result at the end of an operation. |
7 |
| - In the returns you may see not just the result, but the formula also. |
8 |
| - This is made just for debugging. In case something's wrong, |
9 |
| - we can easily see what the current function does. |
10 |
| -''' |
11 | 3 |
|
| 4 | +def multiply(): |
| 5 | + global sentence |
12 | 6 |
|
13 |
| -def multiply(numbers: deque): |
14 |
| - global result |
15 |
| - result = 1 # The result is set to one, because any number multiplied by 0 will equal 0 |
| 7 | + manual_check = " * ".join([str(num) for num in sentence]) |
| 8 | + result = 1 |
16 | 9 |
|
17 |
| - for num in numbers: |
18 |
| - result *= num |
| 10 | + for item in sentence: |
| 11 | + result *= int(item) |
19 | 12 |
|
20 |
| - return f"{' * '.join([str(num) for num in numbers])} = {result}" |
| 13 | + manual_check += f" = {result}" |
| 14 | + return manual_check, result |
21 | 15 |
|
22 | 16 |
|
23 |
| -def divide(numbers: deque): |
24 |
| - global result |
| 17 | +def add(): |
| 18 | + global sentence |
25 | 19 |
|
26 |
| - formula = ' / '.join([str(num) for num in numbers]) # This is made here, because after the for loop below the numbers will be different |
| 20 | + manual_check = " + ".join([str(num) for num in sentence]) |
| 21 | + result = sum([int(num) for num in sentence]) |
27 | 22 |
|
28 |
| - for index in range(len(numbers) - 1): |
29 |
| - numbers[index + 1] = numbers[index] / numbers[index + 1] # Here, we make the second index(divisor) become the result of the division |
| 23 | + manual_check += f" = {result}" |
| 24 | + return manual_check, result |
30 | 25 |
|
31 |
| - result = floor(numbers[-1]) # After the for loop, we made the last number result of the whole division, so the result is just the last number. |
32 | 26 |
|
33 |
| - return f"{formula} = {result}" |
| 27 | +def divide(): |
| 28 | + global sentence |
34 | 29 |
|
| 30 | + manual_check = " / ".join([str(num) for num in sentence]) |
| 31 | + result = int(sentence[0]) |
35 | 32 |
|
36 |
| -def add(numbers: deque): |
37 |
| - global result |
38 |
| - result = sum(numbers) |
| 33 | + try: |
39 | 34 |
|
40 |
| - return f"{' + '.join([str(num) for num in numbers])} = {result}" |
| 35 | + for index in range(len(sentence)): |
| 36 | + result /= int(sentence[index + 1]) |
41 | 37 |
|
| 38 | + except IndexError: |
| 39 | + manual_check += f" = {result}" |
| 40 | + return manual_check, result |
42 | 41 |
|
43 |
| -def subtract(numbers: deque): |
44 |
| - global result |
45 | 42 |
|
46 |
| - formula = ' - '.join([str(num) for num in numbers]) # This is made here, because after the for loop below the numbers will be different |
| 43 | +def subtract(): |
| 44 | + global sentence |
47 | 45 |
|
48 |
| - for index in range(len(numbers) - 1): |
49 |
| - numbers[index + 1] = numbers[index] - numbers[index + 1] # Here, we make the second index(divisor) become the result of the subtraction |
| 46 | + manual_check = " - ".join([str(num) for num in sentence]) |
| 47 | + result = int(sentence[0]) |
50 | 48 |
|
51 |
| - result = numbers[-1] # After the for loop, we made the last number result of the whole subtraction, so the result is just the last number. |
| 49 | + try: |
52 | 50 |
|
53 |
| - return f"{formula} = {result}" |
| 51 | + for index in range(len(sentence)): |
| 52 | + result -= int(sentence[index + 1]) |
54 | 53 |
|
| 54 | + except IndexError: |
| 55 | + manual_check += f" = {result}" |
| 56 | + return manual_check, result |
55 | 57 |
|
56 |
| -valid_operations = ["*", "+", "-", "/"] |
57 | 58 |
|
58 |
| -expression = input().split() |
59 |
| -expression_copy = expression.copy() # We made copy, so I can do whatever I want with it, without affecting the for loop or changing the expression. |
60 |
| -operation_numbers = deque() |
61 |
| - |
62 |
| -last_operator_index = 0 # This is used to see, what the last operation result was, so we work directly with the new number. |
63 |
| -last_operator = 0 # This is used to print the last result of the operations. |
64 |
| - |
65 |
| -for index, char in enumerate(expression): |
66 |
| - |
67 |
| - if char in valid_operations: |
68 |
| - |
69 |
| - [operation_numbers.append(int(num)) for num in expression_copy[last_operator_index:index]] # We get all numbers, from the last result to the operation. |
70 |
| - |
71 |
| - if char == "*": |
72 |
| - |
73 |
| - operation = multiply(operation_numbers) |
74 |
| - |
75 |
| - expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
76 |
| - last_operator_index = index |
77 |
| - |
78 |
| - last_operator = operation.split()[-1] # We need to print only the result at the end. |
79 |
| - |
80 |
| - elif char == "/": |
81 |
| - |
82 |
| - operation = divide(operation_numbers) |
| 59 | +sentence = [] |
83 | 60 |
|
84 |
| - expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
85 |
| - last_operator_index = index |
| 61 | +operations = { |
| 62 | + "*": multiply, |
| 63 | + "+": add, |
| 64 | + "/": divide, |
| 65 | + "-": subtract |
| 66 | +} |
86 | 67 |
|
87 |
| - last_operator = operation.split()[-1] # We need to print only the result at the end. |
88 |
| - |
89 |
| - elif char == "+": |
90 |
| - |
91 |
| - operation = add(operation_numbers) |
92 |
| - |
93 |
| - expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
94 |
| - last_operator_index = index |
95 |
| - |
96 |
| - last_operator = operation.split()[-1] # We need to print only the result at the end. |
97 |
| - |
98 |
| - elif char == "-": |
99 |
| - |
100 |
| - operation = subtract(operation_numbers) |
101 |
| - |
102 |
| - expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
103 |
| - last_operator_index = index |
104 |
| - |
105 |
| - last_operator = operation.split()[-1] # We need to print only the result at the end. |
106 |
| - |
107 |
| - operation_numbers = deque() |
108 |
| - |
109 |
| -print(last_operator) |
| 68 | +expression = input().split() |
| 69 | +result = 0 |
| 70 | + |
| 71 | +for item in expression.copy(): |
| 72 | + |
| 73 | + index = expression.index(item) |
| 74 | + |
| 75 | + if item in operations.keys(): |
| 76 | + sentence = expression[0:index] |
| 77 | + result = operations[item]() |
| 78 | + # print(result[0]) |
| 79 | + expression = expression[index + 1:] |
| 80 | + expression.insert(0, result[1]) |
| 81 | + |
| 82 | +print(floor(result[1])) |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +# from math import floor |
| 92 | +# from collections import deque |
| 93 | +# |
| 94 | +# ''' |
| 95 | +# Note: |
| 96 | +# We need only the result at the end of an operation. |
| 97 | +# In the returns you may see not just the result, but the formula also. |
| 98 | +# This is made just for debugging. In case something's wrong, |
| 99 | +# we can easily see what the current function does. |
| 100 | +# ''' |
| 101 | +# |
| 102 | +# |
| 103 | +# def multiply(numbers: deque): |
| 104 | +# global result |
| 105 | +# result = 1 # The result is set to one, because any number multiplied by 0 will equal 0 |
| 106 | +# |
| 107 | +# for num in numbers: |
| 108 | +# result *= num |
| 109 | +# |
| 110 | +# return f"{' * '.join([str(num) for num in numbers])} = {result}" |
| 111 | +# |
| 112 | +# |
| 113 | +# def divide(numbers: deque): |
| 114 | +# global result |
| 115 | +# |
| 116 | +# formula = ' / '.join([str(num) for num in numbers]) # This is made here, because after the for loop below the numbers will be different |
| 117 | +# |
| 118 | +# for index in range(len(numbers) - 1): |
| 119 | +# numbers[index + 1] = numbers[index] / numbers[index + 1] # Here, we make the second index(divisor) become the result of the division |
| 120 | +# |
| 121 | +# result = floor(numbers[-1]) # After the for loop, we made the last number result of the whole division, so the result is just the last number. |
| 122 | +# |
| 123 | +# return f"{formula} = {result}" |
| 124 | +# |
| 125 | +# |
| 126 | +# def add(numbers: deque): |
| 127 | +# global result |
| 128 | +# result = sum(numbers) |
| 129 | +# |
| 130 | +# return f"{' + '.join([str(num) for num in numbers])} = {result}" |
| 131 | +# |
| 132 | +# |
| 133 | +# def subtract(numbers: deque): |
| 134 | +# global result |
| 135 | +# |
| 136 | +# formula = ' - '.join([str(num) for num in numbers]) # This is made here, because after the for loop below the numbers will be different |
| 137 | +# |
| 138 | +# for index in range(len(numbers) - 1): |
| 139 | +# numbers[index + 1] = numbers[index] - numbers[index + 1] # Here, we make the second index(divisor) become the result of the subtraction |
| 140 | +# |
| 141 | +# result = numbers[-1] # After the for loop, we made the last number result of the whole subtraction, so the result is just the last number. |
| 142 | +# |
| 143 | +# return f"{formula} = {result}" |
| 144 | +# |
| 145 | +# |
| 146 | +# valid_operations = ["*", "+", "-", "/"] |
| 147 | +# |
| 148 | +# expression = input().split() |
| 149 | +# expression_copy = expression.copy() # We made copy, so I can do whatever I want with it, without affecting the for loop or changing the expression. |
| 150 | +# operation_numbers = deque() |
| 151 | +# |
| 152 | +# last_operator_index = 0 # This is used to see, what the last operation result was, so we work directly with the new number. |
| 153 | +# last_operator = 0 # This is used to print the last result of the operations. |
| 154 | +# |
| 155 | +# for index, char in enumerate(expression): |
| 156 | +# |
| 157 | +# if char in valid_operations: |
| 158 | +# |
| 159 | +# [operation_numbers.append(int(num)) for num in expression_copy[last_operator_index:index]] # We get all numbers, from the last result to the operation. |
| 160 | +# |
| 161 | +# if char == "*": |
| 162 | +# |
| 163 | +# operation = multiply(operation_numbers) |
| 164 | +# |
| 165 | +# expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
| 166 | +# last_operator_index = index |
| 167 | +# |
| 168 | +# last_operator = operation.split()[-1] # We need to print only the result at the end. |
| 169 | +# |
| 170 | +# elif char == "/": |
| 171 | +# |
| 172 | +# operation = divide(operation_numbers) |
| 173 | +# |
| 174 | +# expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
| 175 | +# last_operator_index = index |
| 176 | +# |
| 177 | +# last_operator = operation.split()[-1] # We need to print only the result at the end. |
| 178 | +# |
| 179 | +# elif char == "+": |
| 180 | +# |
| 181 | +# operation = add(operation_numbers) |
| 182 | +# |
| 183 | +# expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
| 184 | +# last_operator_index = index |
| 185 | +# |
| 186 | +# last_operator = operation.split()[-1] # We need to print only the result at the end. |
| 187 | +# |
| 188 | +# elif char == "-": |
| 189 | +# |
| 190 | +# operation = subtract(operation_numbers) |
| 191 | +# |
| 192 | +# expression_copy[index] = f'{result}' # Here we save the result on the place of the operation |
| 193 | +# last_operator_index = index |
| 194 | +# |
| 195 | +# last_operator = operation.split()[-1] # We need to print only the result at the end. |
| 196 | +# |
| 197 | +# operation_numbers = deque() |
| 198 | +# |
| 199 | +# print(last_operator) |
0 commit comments