I\'m trying to develop a function mirror() that takes a string and returns its mirrored string but only if the mirrored string can be represented using \"mirror
mirror()
Try this:
def mirror(s): mir = {'b': 'd', 'd': 'b', 'o': 'o', 'p': 'q', 'q': 'p', 'v': 'v', 'w': 'w', 'x': 'x'} if not set(s).issubset(mir.keys()): return 'INVALID' return ''.join(map(lambda x: mir[x], s[::-1]))
Here use set to judge whether the chars in str s is valid or not.
set
s