Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Challenge questions/anantkaushik/challenge5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.