
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to evaluate ternary expression in C++
Suppose we have an expression that holds the ternary expression, we have to evaluate the result of the expression. It supports some values like T and F for True and False and “?” and “:” characters. There are some properties:
- The length of the given string must be less than or equal to 10000.
- The conditional expressions group right-to-left.
- The condition will always be either T or F. So the condition will never be a digit.
- The result of the expression will always evaluate to T or F.
So for example, if the input is like “T ? T ? F : T : T”, so the output will be F.
To solve this, we will follow these steps:
- ret := an empty string, n := size of s,
- create a stack st
- for i in range n – 1 down to 0
- x := s[i]
- if st is not empty and top of stack is ‘?’, then
- delete from st
- first := top of st, then delete two elements from stack
- second := top of stack, and delete from st
- if x is T, then insert first into st, otherwise insert second into st
- otherwise insert x into st
- while st is not empty, then
- ret := ret + top of st and delete from st
- reverse ret and return ret
Let us see the following implementation to get better understanding:
Example
#include using namespace std; class Solution { public: string parseTernary(string s) { string ret = ""; int n = s.size(); stack st; for(int i = n - 1; i >= 0; i--){ char x = s[i]; if(!st.empty() && st.top() == '?'){ st.pop(); char first = st.top(); st.pop(); st.pop(); char second = st.top(); st.pop(); if(x == 'T'){ st.push(first); } else st.push(second); } else{ st.push(x); } } while(!st.empty()){ ret += st.top(); st.pop(); } reverse(ret.begin(), ret.end()); return ret; } }; main(){ Solution ob; cout << (ob.parseTernary("T?T?F:T:T")); }
Input
" T?T?F:T:T"
Output
F
- Related Articles
- Ternary Expression Parser in C++
- Program to evaluate s-expression as string in Python
- C++ Program to Evaluate an Expression using Stacks
- Program to evaluate Boolean expression from a string in Python?
- Convert Ternary Expression to a Binary Tree in C++
- Program to build and evaluate an expression tree using Python
- Program to evaluate one mathematical expression without built-in functions in python
- Evaluate Postfix Expression
- C++ Program to Implement Ternary Tree
- Evaluate a boolean expression represented as string in C++
- Evaluate an array expression with numbers, + and - in C++
- Program to find the Largest Number using Ternary Operator in C++
- Program to evaluate Postfix Notation in C++
- Why does the JavaScript void statement evaluate an expression?
- Evaluate the following expression:$2+3-7\times 2$.

Advertisements