-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathchallenge5.py
47 lines (45 loc) · 1.39 KB
/
challenge5.py
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
def conditionResult(stack):
if stack[1] == "==":
return int(stack[0] == stack[2]) # int() is used to convert True or False to 0 or 1
if stack[1] == "<=":
return int(stack[0] <= stack[2])
if stack[1] == ">=":
return int(stack[0] >= stack[2])
if stack[1] == "!=":
return int(stack[0] != stack[2])
if stack[1] == "<":
return int(stack[0] < stack[2])
if stack[1] == ">":
return int(stack[0] > stack[2])
def findResult(conditions):
result = []
stack = []
no = ""
condition = ""
for c in conditions:
if c.isdigit():
if condition:
stack.append(condition)
condition = ""
no += c
elif c != " ":
if no:
stack.append(int(no))
no = ""
condition += c
else:
if condition:
stack.append(condition)
condition = ""
if no:
stack.append(int(no))
no = ""
if len(stack) == 3:
result.append(conditionResult(stack))
stack = []
if no: # For last condition
stack.append(int(no))
result.append(conditionResult(stack))
return result
c = input("Enter the conditions: ")
print(*findResult(c)) # Print the values of returned array with space separation.