Python Code for No More Symbols (TCS Codevita) | PrepInsta
No More Symbols
TCS CodeVita is a coding competition organized by TCS every year, in search of world’s best coder. This is a global level coding competition in which coders from all around the world compete for the title of World’s Best Coder. No More Symbols is one of the sample problem of this year TCS CodeVita season 11 competition.
Question -: Prabhu excels at typing letters but struggles with symbols and numbers. To simplify, he decided to represent mathematical expressions using words. For numbers, he mentions each digit individually with the character ‘c’ to signify the entire word representing the number. Prabhu exclusively uses lowercase letters as he’s not proficient with shift keys or caps lock.
For instance
111 is written as oneconecone
120 is written as onectwoczero
For a single operation: Operation Operand1 Operand2
Example: “add one two” represents 1+2.
For two functions: Operation1 Operation2 Operand1 Operand2 Operand3
Example: “add mul twoctwo threecone two” equals (22*31)+2.
For another variation: Operation1 Operand1 Operation2 Operand2 Operand3
Example: “add oneconecone div onectwoczeroczero twoctwo” equals 111+(1200/22).
Prabhu uses the following operations:
- add for addition (e.g., 2+2=4).
- sub for subtraction (e.g., 2-2=0).
- mul for multiplication (e.g., 2*2=4).
- rem for remainder (e.g., 2%2=0).
- pow for power (e.g., 2^2=4).
Note:
- The input does not contain float or negative numbers.
- Verify the correctness of words first, followed by correctness of expression.
0 < Characters in the first line including space < 100
0 < Operands in the expression < 20
Single line denoting the expression.
Single integer repersenting the result of the expression evalutated in numbers not in words.
1
Input
add one sub twochundered one
Output
expression evaluation stopped invalid words present
Explanation
In word twochundred, hundred is not a valid word only zero to nine can be used.
Example 2
Input
five mul six six fourcninecnine zero
Output
expression is not complete or invalid
Explanation
Everywords in the expression is valid but the expression cannot be evaluated only mul operation is found there. After executing, there are still other words are left so the expression is not complete or invalid.
Example 3
Input
mul add sub six five oneczero two
Output
22
Explanation
The above prabhu expression represents ((6-5)+10)*2.
def pfix(exq): ta = [] ops = set(['+', '-', '*', '/', '%', '^']) to = exq.split() for t1 in reversed(to): if t1.isdigit(): ta.append(int(t1)) elif t1 in ops: if t1 == '^': if len(ta) < 2: return "expression is not complete or invalid" ex1 = ta.pop() be = ta.pop() ta.append(ex1 ** be) elif t1 == '/': if len(ta) < 2 or ta[-1] == 0: return "expression is not complete or invalid" op2 = ta.pop() op1 = ta.pop() ta.append(op1 // op2) elif t1 == '%': if len(ta) < 2 or ta[-1] == 0: return "expression is not complete or invalid" op2 = ta.pop() op1 = ta.pop() ta.append(op2 % op1) else: if len(ta) < 2: return "expression is not complete or invalid" op1 = ta.pop() op2 = ta.pop() if t1 == '+': ta.append(op1 + op2) elif t1 == '-': ta.append(op1 - op2) elif t1 == '*': ta.append(op1 * op2) else: return "expression is not complete or invalid" if len(ta) == 1: return ta[0] else: return "expression is not complete or invalid" dn2 = { "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "zero": 0 } do1 = { "add": '+', "sub": '-', "mul": '*', "rem": '%', "pow": '^' } def conv(s): a1 = s.split('c') nx = 0 for i in a1: if i in dn2: nx = nx * 10 + dn2[i] else: return "-1" return str(nx) s = input() lee = s.split() st = "" flag = False for i in lee: if i not in do1: jik = conv(i) if jik == "-1": flag = True print("expression evaluation stopped invalid words present", end='') break st = st + jik + ' ' else: st = st + do1[i] + ' ' if not flag: print(pfix(st), end='')