Question: Python CODE: #!/usr/bin/env python # Queue.py # Dave Reed # CS161 # 02/24/2006 #---------------------------------------------------------------------- class Queue: #---------------------------------------------------------------------- def __init__(self): '''create an empty FIFO queue''' self.q
Python

CODE:
#!/usr/bin/env python # Queue.py # Dave Reed # CS161 # 02/24/2006 #---------------------------------------------------------------------- class Queue: #---------------------------------------------------------------------- def __init__(self): '''create an empty FIFO queue''' self.q = [] #---------------------------------------------------------------------- def size(self): '''return number of items in the queue pre: none post: returns number of items in the queue''' return len(self.q) #---------------------------------------------------------------------- def enqueue(self, x): '''insert x at end of queue pre: none post: x is added to the queue''' self.q.append(x) #---------------------------------------------------------------------- def front(self): '''return first item in queue pre: queue is not empty; IndexError is raised if empty post: returns first item in the queue''' return self.q[0] #---------------------------------------------------------------------- def dequeue(self): '''remove and return first item in queue pre: queue is not empty; IndexError is raised if empty post: removes and returns first item in the queue''' return self.q.pop(0) #----------------------------------------------------------------------
end of queen.py
--------------------------------------------------------------- # palindrome.py from MyQueue import Queue from Stack import Stack import string #------------------------------------------------------------ def isPalindrome(phrase): forward = Queue() reverse = Stack() extractLetters(phrase, forward, reverse) return sameSequence(forward, reverse) #------------------------------------------------------------ def extractLetters(phrase, q, s): for ch in phrase: if ch.isalpha(): ch = ch.lower() q.enqueue(ch) s.push(ch) #------------------------------------------------------------ def sameSequence(q, s): while q.size() > 0: ch1 = q.dequeue() ch2 = s.pop() if ch1 != ch2: return False return True print(isPalindrome("race car")) print(isPalindrome("Hello"))
4. Suppose a queue is being used to store numbers, and we want to see if the numbers currently in the queue are in order. Write and test a func- tion queueInOrder (someQueue) that returns a Boolean indicating whether someQueue is in sorted order. After calling the function, the queue should look exactly like it did before the function cal. Your function should only make use of the available queue ADT operations; accessing the underlying representation is not allowed
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
