Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69e2383af7832c8032603b93 Challenge 277: Mirror Image 29 challenge-277

--description--

Given two strings, determine if the second string is a mirror image of the first.

A mirror image is formed by reversing the string and replacing each character with its mirror equivalent.

  • Symmetric characters look like themselves in a mirror:

W, T, Y, U, I, O, H, A, X, V, M, w, o, x, v, 0, 8, =, +, :, |, -, _, *, ^, !, ., and the space ( ).

  • Mirrored pairs swap with each other in a mirror:
Character Swaps with
[ ]
{ }
< >
b d
p q
( )

If either string includes a character not in the lists above, it doesn't have mirror image that can be created from the characters.

For example, the mirrored image of "[HOW]" is "[WOH]".

--hints--

is_mirror_image("[HOW]", "[WOH]") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("[HOW]", "[WOH]"), True)`)
}})

is_mirror_image("MOM", "MOM") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("MOM", "MOM"), True)`)
}})

is_mirror_image("vow", "wov") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("vow", "wov"), True)`)
}})

is_mirror_image("TIM", "TIM") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("TIM", "TIM"), False)`)
}})

is_mirror_image("{WOW}", "}WOW{") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("{WOW}", "}WOW{"), False)`)
}})

is_mirror_image("XXVII", "IIV%X") should return False.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("XXVII", "IIV%X"), False)`)
}})

is_mirror_image("><(((*>", "<*)))><") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("><(((*>", "<*)))><"), True)`)
}})

is_mirror_image("WTYUIOHAXVMwoxv08=+:|-_*^!.[]{}<>bdpq()", "()pqbd<>{}[].!^*_-|:+=80vxowMVXAHOIUYTW") should return True.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_mirror_image("WTYUIOHAXVMwoxv08=+:|-_*^!.[]{}<>bdpq()", "()pqbd<>{}[].!^*_-|:+=80vxowMVXAHOIUYTW"), True)`)
}})

--seed--

--seed-contents--

def is_mirror_image(s1, s2):

    return s1

--solutions--

def is_mirror_image(s1, s2):
    symmetric = set('WTYUIOHA XVMwoxv08=+:|-_*^!.')
    pairs = {'[': ']', ']': '[', '{': '}', '}': '{', '<': '>', '>': '<',
             'b': 'd', 'd': 'b', 'p': 'q', 'q': 'p', '(': ')', ')': '('}
    mirrored = []
    for c in reversed(s1):
        if c in symmetric:
            mirrored.append(c)
        elif c in pairs:
            mirrored.append(pairs[c])
        else:
            return False
    return ''.join(mirrored) == s2