- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
return s
Example (Python)
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))
Input
"G()()()(al)(al)"
Output
Goooalal