Transform the Expression CodeChef Solution

Problem – Transform the Expression CodeChef Solution

Reverse Polish Notation (RPN) is a mathematical notation where every operator follows all of its operands. For instance, to add three and four, one would write “3 4 +” rather than “3 + 4”. If there are multiple operations, the operator is given immediately after its second operand; so the expression written “3 − 4 + 5” would be written “3 4 − 5 +” first subtract 4 from 3, then add 5 to that.

Transform the algebraic expression with brackets into RPN form.

You can assume that for the test cases below only single letters will be used, brackets [] will not be used and each expression has only one RPN form (no expressions like a*b*c)

Input

The first line contains t, the number of test cases (less then 100).

Followed by t lines, containing an expression to be translated to RPN form, where the length of the expression is less then 400.

Output

The expressions in RPN form, one per line.

Example

Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

Transform the Expression CodeChef Solution in C++14

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	
	int top=-1;
	while(t--){
	    
	    string a;
	   cin>>a;
	   char s[405];
	   for(int i=0;i<a.length();i++){
	       if(a[i]>='a' && a[i]<='z'){
	           cout<<a[i];
	       }
	       else if(a[i]==')'){
	           cout<<s[top];
	           top--;
	       }
	       else if(a[i]!='('){
	           top++;
	           s[top]=a[i];
	       }
	   }
	   cout<<endl;
	}
	return 0;
}

Transform the Expression CodeChef Solution in Pyth 3

# cook your dish here
t=int(input())
while(t>0):
    s=input()
    a="+-*%^/!"
    b=[]
    for i in s:
        if i in a:
            b.append(i)
        elif(i==')'):
            print(b.pop(),end="")
        elif(i!='('):
            print(i,end="")
    print()
    t=t-1

Transform the Expression CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sca=new Scanner(System.in);
		int T=sca.nextInt();
	
		while(T-->0){
		    String str=sca.next();
		    Stack<Character> s=new Stack();
		    String r="";
	
		    for(int i=0;i<str.length();i++){
		        char c=str.charAt(i);
	
		    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c=='^'){
		        s.push(c);
		    }
	
		    else if(c==')'){
		        while(s.peek()!='('){
		            r+=s.pop();
		        }
		        s.pop();
		    }
	
		    else{
		        r+=c;
		    }
		    }
		    System.out.println(r);
		}
	}
}
Transform the Expression CodeChef Solution Review:

In our experience, we suggest you solve this Transform the Expression CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Transform the Expression CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Transform the Expression CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *