Skip to content

Commit 8bcd71b

Browse files
committed
Redid Stacks, Queues, Tuples and Sets - Exercise
1 parent a8524e9 commit 8bcd71b

File tree

1 file changed

+173
-83
lines changed

1 file changed

+173
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,199 @@
11
from math import floor
2-
from collections import deque
32

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-
'''
113

4+
def multiply():
5+
global sentence
126

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
169

17-
for num in numbers:
18-
result *= num
10+
for item in sentence:
11+
result *= int(item)
1912

20-
return f"{' * '.join([str(num) for num in numbers])} = {result}"
13+
manual_check += f" = {result}"
14+
return manual_check, result
2115

2216

23-
def divide(numbers: deque):
24-
global result
17+
def add():
18+
global sentence
2519

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])
2722

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
3025

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.
3226

33-
return f"{formula} = {result}"
27+
def divide():
28+
global sentence
3429

30+
manual_check = " / ".join([str(num) for num in sentence])
31+
result = int(sentence[0])
3532

36-
def add(numbers: deque):
37-
global result
38-
result = sum(numbers)
33+
try:
3934

40-
return f"{' + '.join([str(num) for num in numbers])} = {result}"
35+
for index in range(len(sentence)):
36+
result /= int(sentence[index + 1])
4137

38+
except IndexError:
39+
manual_check += f" = {result}"
40+
return manual_check, result
4241

43-
def subtract(numbers: deque):
44-
global result
4542

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
4745

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])
5048

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:
5250

53-
return f"{formula} = {result}"
51+
for index in range(len(sentence)):
52+
result -= int(sentence[index + 1])
5453

54+
except IndexError:
55+
manual_check += f" = {result}"
56+
return manual_check, result
5557

56-
valid_operations = ["*", "+", "-", "/"]
5758

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 = []
8360

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+
}
8667

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

Comments
 (0)