Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Mono Captcha by marcopunteri
from typing import List
from itertools import starmap, dropwhile
from operator import eq
FONT = ("--X--XXX-XXX-X-X-XXX--XX-XXX-XXX--XX-XX--",
"-XX----X---X-X-X-X---X-----X-X-X-X-X-X-X-",
"--X---XX--X--XXX-XX--XXX--X--XXX-XXX-X-X-",
"--X--X-----X---X---X-X-X-X---X-X---X-X-X-",
"--X--XXX-XXX---X-XX---XX-X---XXX-XX---XX-")
def elaborate(matrix,font=False):
chunks = [[] for _ in range(len(matrix[0])//4)]
for row in matrix:
for pos,col in enumerate(row[1:-1]):
if pos%4!=3:
if font:
chunks[pos//4].append(0 if col == '-' else 1)
else:
chunks[pos//4].append(col)
return chunks
def checkio(image: List[List[int]]) -> int:
fonts, numbers = elaborate(FONT,font=True), elaborate(image)
res = ''
for number in numbers:
diff = dropwhile(lambda font: sum(starmap(eq,zip(number,font))) < 14, fonts )
try:
detected = fonts.index(next(diff))
except StopIteration:
detected = 9
res += str((detected+1)%10)
return int(res)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
''' assert checkio([[0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0]]) == 394, "394 clear"'''
assert checkio([[0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0]]) == 394, "again 394 but with noise"
print("Coding complete? Click 'Check' to earn cool rewards!")
March 17, 2024