diff --git a/Challenge questions/anantkaushik/challenge5.py b/Challenge questions/anantkaushik/challenge5.py new file mode 100644 index 0000000..048bea0 --- /dev/null +++ b/Challenge questions/anantkaushik/challenge5.py @@ -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. \ No newline at end of file