Python mirrored string function

前端 未结 5 504
攒了一身酷
攒了一身酷 2021-01-16 12:45

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

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 13:27

    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.

提交回复
热议问题