Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

EXISTING CODE class Stack: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): if self.is_empty(): raise IndexError('ERROR:

image text in transcribed

EXISTING CODE

class Stack: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): if self.is_empty(): raise IndexError('ERROR: The stack is empty!') return self.items.pop() def peek(self): if self.is_empty(): raise IndexError('ERROR: The stack is empty!') return self.items[len(self.items) - 1] def size(self): return len(self.items) def __str__(self): return str(self.items)[:-1] + ' Write a function named longest_mirror_pattern(string) which takes as input a string and returns the length of the longest sequence of characters which reads the same forwards and backwards and has an even length. For example, in the string "aabfggf" there are two mirror patterns: "aa" (length 2) and "fggf" (length 4). Hence in this case the function would return 4. You can assume that mirror patterns do not overlap, e.g. you don't need to handle cases such as "abbaccab" where "abba" and "baccab" are overlapping mirror patterns. Examples: - The length of the longest mirror pattern in "aa" is 2 . - The length of the longest mirror pattern in "ccabba" is 4 ("abba"). - The length of the longest mirror pattern in "aba" is 0 (while "aba" reads the same forwards and backwards, it does not have an even length). - The length of the longest mirror sequence in "xyzyxzghiihgabba" is 6 ("ghiihg"). NOTE: The problem is similar to the "balanced braces" task discussed in the lectures. You might want to use a stack in your solution. A Stack ADT with the methods Stack(), push(), pop(), and peek(). is defined. Click here to download the source code for the Stack class. For example: Answer: (penalty regime: 0,0,5,10,15,20,25,30,35,40,45,50% )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions