nogoriv567
nogoriv567

Reputation: 33

How to create identical flip in python

I have not found any resource where it is implemented.

Need to flip the any special character

s="("

Expected output : ")"

I tried the reversed but it gives the same

Any solution is much appreciated !!!

Upvotes: 2

Views: 190

Answers (3)

Nice Zombies
Nice Zombies

Reputation: 1107

You'll have to extend this, but here's a start, I've commented out every character that's currently not supported, in the final code this dict will be generated from two strings or minified:

mirror = {
    ' ': ' ', '!': '!', '"': '"',
    # '#': '#', '$': '$', '%': '%', '&': '&',#
    "'": "'", '(': ')', ')': '(', '*': '*', '+': '+',
    # ',': ',',#
    '-': '-', '.': '.', '/': '\\', '0': '0',
    # '1': '1', '2': '2',#
    '3': 'Ɛ',
    # '4': '4', '5': '5', '6': '6', '7': '7',#
    '8': '8',
    # '9': '9',#
    ':': ':',
    # ';': ';',#
    '<': '>', '=': '=', '>': '<', '?': '⸮',
    # '@': '@',#
    'A': 'A',
    # 'B': 'B',#
    'C': 'Ɔ',
    # 'D': 'D',#
    'E': 'Ǝ',
    # 'F': 'F', 'G': 'G',#
    'H': 'H', 'I': 'I',
    # 'J': 'J', 'K': 'K',#
    'L': '⅃', 'M': 'M',
    'N': 'И', 'O': 'O',
    # 'P': 'P', 'Q': 'Q',#
    'R': 'Я', 'S': 'Ƨ', 'T': 'T', 'U': 'U', 'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y',
    # 'Z': 'Z',#
    '[': ']', '\\': '/', ']': '[', '^': '^', '_': '_', '`': '´',
    # 'a': 'a',#
    'b': 'd', 'c': 'ɔ', 'd': 'b', 'e': 'ɘ',
    # 'f': 'f', 'g': 'g', 'h': 'h', 'i': 'i', 'j': 'j', 'k': 'k', 'l': 'l',#
    'm': 'm', 'n': 'n', 'o': 'o', 'p': 'q', 'q': 'p',
    # 'r': 'r',#
    's': 'ƨ',
    # 't': 't',#
    'u': 'u', 'v': 'v', 'w': 'w', 'x': 'x',
    # 'y': 'y', 'z': 'z',#
    '{': '}', '|': '|', '}': '{',
    # '~': '~',#
    '¡': '¡',
    # '¢': '¢', '£': '£',#
    '¤': '¤', '¥': '¥', '¦': '¦',
    # '§': '§',#
    '¨': '¨',
    # '©': '©', 'ª': 'ª',#
    '«': '»',
    # '¬': '¬', '®': '®', '¯': '¯',#
    '°': '°', '±': '±',
    # '²': '²', '³': '³',#
    '´': '`',
    # 'µ': 'µ', '¶': '¶',#
    '·': '·',
    # '¸': '¸', '¹': '¹',#
    'º': 'º', '»': '«',
    # '¼': '¼', '½': '½', '¾': '¾', '¿': '¿',#
    'À': 'Á', 'Á': 'À', 'Â': 'Â',
    # 'Ã': 'Ã',#
    'Ä': 'Ä', 'Å': 'Å',
    # 'Æ': 'Æ', 'Ç': 'Ç', 'È': 'È', 'É': 'É', 'Ê': 'Ê', 'Ë': 'Ë',#
    'Ì': 'Í', 'Í': 'Ì', 'Î': 'Î', 'Ï': 'Ï',
    # 'Ð': 'Ð', 'Ñ': 'Ñ',#
    'Ò': 'Ó', 'Ó': 'Ò', 'Ô': 'Ô',
    # 'Õ': 'Õ',#
    'Ö': 'Ö', '×': '×',
    # 'Ø': 'Ø',#
    'Ù': 'Ú', 'Ú': 'Ù', 'Û': 'Û', 'Ü': 'Ü',
    # 'Ý': 'Ý', 'Þ': 'Þ', 'ß': 'ß', 'à': 'à', 'á': 'á', 'â': 'â', 'ã': 'ã', 'ä': 'ä', 'å': 'å', 'æ': 'æ', 'ç': 'ç', 'è': 'è', 'é': 'é', 'ê': 'ê', 'ë': 'ë', 'ì': 'ì', 'í': 'í', 'î': 'î', 'ï': 'ï', 'ð': 'ð', 'ñ': 'ñ',#
    'ò': 'ó', 'ó': 'ò', 'ô': 'ô',
    # 'õ': 'õ',#
    'ö': 'ö', '÷': '÷',
    # 'ø': 'ø',#
    'ù': 'ú', 'ú': 'ù', 'û': 'û', 'ü': 'ü',
    # 'ý': 'ý', 'þ': 'þ', 'ÿ': 'ÿ',#
    'Ā': 'Ā',
    # 'ā': 'ā',#
    'Ă': 'Ă',
    # 'ă': 'ă','Ą': 'Ą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ƕ','Ƿ': 'Ƿ','Ǹ': 'Ǹ','ǹ': 'ǹ','Ǻ': 'Ǻ','ǻ': 'ǻ','Ǽ': 'Ǽ','ǽ': 'ǽ','Ǿ': 'Ǿ','ǿ': 'ǿ'#
}
normal = input("Enter text to mirror and reverse: ")
try:
    mirrored = "".join(mirror[character] for character in reversed(normal))
    print(normal, "is", mirrored, "mirrored and reversed.")
except KeyError as k:
    print("This text can't be mirrored yet, edit the mirror dict. KeyError: %s" % k)

Output:

Enter text to mirror: bod
bod is dob mirrored.

Upvotes: 1

Aidis
Aidis

Reputation: 1280

Try this

flipper = {
    "(": ")",
    ")": "(",
}
symbol = "("
symbol = flipper[symbol]
# symbol now became ")"

V2

Easier way of adding new pairs:

# Basically a config of symbol pairs
letters = "{}()"
# One direction mapping
mapping = dict(zip(letters[::2], letters[1::2]))
# Add reverse mapping
flipper = mapping | {values: keys for keys, values in mapping.items()}
flipper["}"] == "{"
# True

Upvotes: 0

R. Baraiya
R. Baraiya

Reputation: 1530

Here main idea is to create a set of charaters which starts and ends, like [] and then find the ASCII value of each, then find out the nearest ascii value.

Code:

def reverse(NUM):
    print(chr(min([ord(i) for i in set(r"(){}[]<>") if i!=NUM], key=lambda x:abs(x-ord(NUM)))))

reverse('}')

#Output {

Upvotes: 2

Related Questions