Java 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.
import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.Stack; public class ExpressionEvaluation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] lee = s.split("\\s"); StringBuilder st = new StringBuilder(); boolean flag = false; for (String i : lee) { if (!do1.containsKey(i)) { String jik = conv(i); if (jik.equals("-1")) { flag = true; System.out.print("expression evaluation stopped invalid words present"); break; } st.append(jik).append(" "); } else { st.append(do1.get(i)).append(" "); } } if (!flag) { System.out.print(pfix(st.toString())); } } static int pfix(String exq) { Stackta = new Stack<>(); HashSet ops = new HashSet<>(); ops.add('+'); ops.add('-'); ops.add('*'); ops.add('/'); ops.add('%'); ops.add('^'); String[] to = exq.split("\\s"); for (int i = to.length - 1; i >= 0; i--) { String t1 = to[i]; if (t1.matches("\\d+")) { ta.push(Integer.parseInt(t1)); } else if (ops.contains(t1.charAt(0))) { if (t1.equals("^")) { if (ta.size() < 2) { return -1; // expression is not complete or invalid } int ex1 = ta.pop(); int be = ta.pop(); ta.push((int) Math.pow(be, ex1)); } else if (t1.equals("/")) { if (ta.size() < 2 || ta.peek() == 0) { return -1; // expression is not complete or invalid } int op2 = ta.pop(); int op1 = ta.pop(); ta.push(op1 / op2); } else if (t1.equals("%")) { if (ta.size() < 2 || ta.peek() == 0) { return -1; // expression is not complete or invalid } int op2 = ta.pop(); int op1 = ta.pop(); ta.push(op2 % op1); } else { if (ta.size() < 2) { return -1; // expression is not complete or invalid } int op1 = ta.pop(); int op2 = ta.pop(); switch (t1.charAt(0)) { case '+': ta.push(op1 + op2); break; case '-': ta.push(op1 - op2); break; case '*': ta.push(op1 * op2); break; } } } else { return -1; // expression is not complete or invalid } } return ta.size() == 1 ? ta.pop() : -1; // expression is not complete or invalid } static HashMap dn2 = new HashMap<>() {{ put("one", 1); put("two", 2); put("three", 3); put("four", 4); put("five", 5); put("six", 6); put("seven", 7); put("eight", 8); put("nine", 9); put("zero", 0); }}; static HashMap do1 = new HashMap<>() {{ put("add", '+'); put("sub", '-'); put("mul", '*'); put("rem", '%'); put("pow", '^'); }}; static String conv(String s) { String[] a1 = s.split("c"); int nx = 0; for (String i : a1) { if (dn2.containsKey(i)) { nx = nx * 10 + dn2.get(i); } else { return "-1"; } } return String.valueOf(nx); } }