- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find goal parser interpretation command in Python
Suppose we have a Goal Parser that can interpret a given string command. A command consists of
An alphabet "G",
Opening and closing parenthesis "()"
and/or "(al)" in some order.
Our Goal Parser will interpret "G" as the string "G", "()" as "o", and "(al)" as the string "al". Finally interpreted strings are then concatenated in the original order. So if we have string command, we have to find the Goal Parser's interpretation of command.
So, if the input is like command = "G()()()(al)(al)", then the output will be Goooalal.
To solve this, we will follow these steps −
s:= blank string
for i in range 0 to size of command - 1, do
if command[i] is not same as "(" and command[i] is not same as ")", then
s := s concatenate command[i]
if command[i] is same as "(" and command[i+1] is same as ")" and i+1<len(command) , then
s := s concatenate 'o'
if command[i] is same as "(", then
go for next iteration
if command[i] is same as ")", then
go for next iteration
Let us see the following implementation to get better understanding −
def solve(command): s="" for i in range(len(command)): if command[i]!="(" and command[i]!=")": s+=command[i] if command[i]=="(" and command[i+1]==")" and i+1<len(command): s+='o' if command[i]=="(": continue if command[i]==")": continue return s command = "G()()()(al)(al)" print(solve(command))
- Python Parser for command line options
- C-style parser for command line options in Python
- Program to find out the shortest path to reach the goal in Python
- Configuration file parser in Python (configparser)
- urllib.robotparser - Parser for robots.txt in Python
- html.parser — Simple HTML and XHTML parser in Python
- Ternary Expression Parser in C++
- HTML Entity Parser in C++
- What is interpretation of sequential consistency of instruction execution?
- Command Line Arguments in Python
- What is Recursive Descent Parser?
- What is a Predictive Parser?
- What is SLR (1) Parser?
- What is CLR (1) Parser?
- What is LALR (1) Parser?