From 16d417e212c03e3bab93416332c3189e48ffc27b Mon Sep 17 00:00:00 2001 From: Anant Kaushik <anant.kaushik2@gmail.com> Date: Thu, 17 Oct 2019 02:06:36 +0530 Subject: [PATCH] Challenge 5 --- .../anantkaushik/challenge5.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Challenge questions/anantkaushik/challenge5.py 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