File size: 1,100 Bytes
ffa6aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pytesseract
import numpy as np


def solve(image: np.ndarray) -> str:
    for mode in [7, 10, 11, 12, 13]:
        result = normalize(
            pytesseract.image_to_string(
                image, lang="eng", config=f"--oem 3 --psm {mode}", timeout=0.5
            ).strip()
        )
        if result != "":
            return result

    return "not sure"


def normalize(s: str) -> str:
    print(s)
    if "\n" in s:
        return ""

    s = s.replace(" ", "").lower()

    # if first is number
    if s[0].isdigit() and s[2].isdigit():
        if s[1] in ["+", "4"]:
            return str(int(s[0]) + int(s[2]))
        elif s[1] in ["-", "_"]:
            return str(int(s[0]) - int(s[2]))
        else:
            return str(int(s[0]) * int(s[2]))

    # possible alphabet mapping
    mapping = {
        ")": "l",
        "¥": "y",
        "2": "z",
        "é": "e",
    }

    for k, v in mapping.items():
        s = s.replace(k, v)

    # if not all alphabet
    if not all([c.isalpha() for c in s]):
        return ""

    if len(s) != 4:
        return ""

    return s