본문 바로가기
나는 이렇게 학습한다/Algorithm & SQL

0831. Correct the mistakes of the character recognition software

by daco2020 2022. 8. 31.

Character recognition software is widely used to digitise printed texts. Thus the texts can be edited, searched and stored on a computer.

When documents (especially pretty old ones written with a typewriter), are digitised character recognition softwares often make mistakes.

Your task is correct the errors in the digitised text. You only have to handle the following mistakes:

S is misinterpreted as 5 O is misinterpreted as 0 I is misinterpreted as 1 The test cases contain numbers only by mistake.



Solution:

def replace(func):
    def wrapper(s):
        return func(i.isdigit() and {"0":"O","1":"I","5":"S"}[i] or i for i in s)
    return wrapper

@replace
def correct(arr):
    return ''.join(arr)


Other Solution:

def correct(string):
    return string.translate(str.maketrans("501", "SOI"))


def correct(string):
    return string.replace('1','I').replace('0','O').replace('5','S')