Question
in python and according to this
#Creating class for stack
class My_Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Push(self, d):
self.items.append(d)
def Pop(self):
return self.items.pop()
def Display(self):
for i in reversed(self.items):
print(i,end="")
print()
s = My_Stack()
#taking input from user
str = input('Enter your string for palindrome checking: ')
n= len(str)
#Pushing half of the string into stack
for i in range(int(n/2)):
s.Push(str[i])
print("S",end="")
s.Display()
s.Display()
#for the next half checking the upcoming string charactor with popped
character of stack
for i in range(int((n+1)/2),n):
c= s.Pop()
#string is not palindrome if at any stage popped char does not match
with current char of the string
if(str[i]!=c):
s.Push(c)
print("String is not palindrome.")
break
s.Display()
#if stack is empty, that means the string is a palindrome, otherwise it is
not.
if s.isEmpty():
print('The string is a palindrome.')
else:
print('The string is not a palindrome.')



This is the final project for Foundations of Computing: Using your existing stack machine, validate the following language: [
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The given language is a^n b^2n

The language accepts strings of the form L = {abb,aabbbb,aaabbbbbb,....}

According to SM rules , the following inferences can be made

1 . When input symbol = a, top of the stack = S , we pop S and push Sbb

implies that when input symbol = a , we push 2 b's

2 . When input symbol = b , we pop() from the stack

HENCE the algo is as follows

take input

for each input symbol

if sym = 'a'

push 2 'b's on to stack

if sym = 'b'

pop symbol from stack

after processing all inputs if the stack is empty then our languge is valid

else it is invalid

#Creating class for stack
class My_Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Push(self, d):
self.items.append(d)
def Pop(self):
return self.items.pop()
def Display(self):
for i in reversed(self.items):
print(i,end="")
print()
#Initialize stack object
s = My_Stack()
#Input
string = input('Enter your string: ')
n= len(string)
#Process the input as per the SM rules
for i in range(n):
if string[i] == "a":
s.Push("b")
s.Push("b")
if string[i] == "b":
s.Pop()
#if stack is empty then valid language else not
if s.isEmpty():
print("Valid Language")
else:
print("Invalid Language")

ktuzzy- Spyder (Python 3.J) Eile Edit Searh So un Debug Consales Projects Tools View Help 비 variable explorer 忷凶 카미ect expiorVaridbie expiore IPython console sole 9/A X Console 5/A X Console 7/A X Con Console 3/A X Enter your string: aabb Invalid Lan

Add a comment
Know the answer?
Add Answer to:
in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Po...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT