CP164 Final: Stacks
Notes - Stacks - The Mirror Problem
We have already used stacks check if brackets are properly balanced. We can use a similar
idea to determine if a string is a mirror. A mirror can be thought of as a special form of a
palindrome, and consists of a string of the form:
LmR
where:
m is the mirror character at the centre of the string
L and R are strings where R is the reverse of L (They may be empty strings)
L and R are formed from a limited set of characters that do not include m
Some examples of the abc mirror pattern (i.e. L and R are limited to the characters abc):
cmc
bbaamaabb
m
abacmcaba
These strings do not fit the abc mirror pattern:
abmb
gmg
abacmcabb
Document Summary
We have already used stacks check if brackets are properly balanced. We can use a similar idea to determine if a string is a mirror. A mirror can be thought of as a special form of a palindrome, and consists of a string of the form: M is the mirror character at the centre of the string. L and r are strings where r is the reverse of l (they may be empty strings) L and r are formed from a limited set of characters that do not include m. Some examples of the abc mirror pattern (i. e. l and r are limited to the characters abc): cmc bbaamaabb m abacmcaba. These strings do not fit the abc mirror pattern: abmb gmg abacmcabb. Write and test the following function: def is_mirror(string): Determines if a string is a mirror string or not. (uses a stack) Parameters: string - the string to test (str)