Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Mono Captcha by Rounin
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 flip(matrix):
if len(matrix) == 0:
return []
out = []
for col in range(len(matrix[0])):
out.append(list())
for row in range(len(matrix)):
out[col].append(matrix[row][col])
return out
def split(chars):
chars = flip(chars)
out = list()
tmpbuffer = list()
for col in chars:
if col == [0,0,0,0,0]:
if len(tmpbuffer) > 0:
out.append(flip(tmpbuffer))
tmpbuffer = list()
else:
tmpbuffer.append(col)
return out
rowlen = int(len(FONT)/5)
FONT = [FONT[0:rowlen],FONT[rowlen:rowlen*2],FONT[rowlen*2:rowlen*3],FONT[rowlen*3:rowlen*4],FONT[rowlen*4:rowlen*5]]
FONT = list(map(lambda row: list(map(int, list(row.replace("-","0").replace("X","1")))), FONT))
FONT = split(FONT)
FONT = [FONT[9]]+FONT[0:9]
def safeget(matrix, row, col):
try:
return matrix[row][col]
except:
return 0
def diffimages(image1, image2):
diff = 0
for row in range(len(image1)):
for col in range(max(len(image2[row]), len(image1[row]))):
diff += safeget(image2, row, col) ^ safeget(image1, row, col)
return diff
def compare(image1, image2):
diffs = list()
diffs.append(diffimages(image1, image2))
displacedimage1 = flip([[0,0,0,0,0]]+flip(image1))
diffs.append(diffimages(displacedimage1, image2))
displacedimage2 = flip([[0,0,0,0,0]]+flip(image2))
diffs.append(diffimages(displacedimage2, image1))
return min(diffs)
def checkio(image):
out = 0
image = split(image)
for idigit in range(len(image)):
found = False
out *= 10
for fdigit in range(0,10):
diff = compare(image[idigit], FONT[fdigit])
if diff <= 1:
out += fdigit
found = True
if found == False and idigit+1 < len(image):
# Correct mis-segmented digits
out = int(out/10)
image[idigit+1] = flip(flip(image[idigit])+[[0,0,0,0,0]]+flip(image[idigit+1]))
elif found == False:
raise Exception("Couldn't parse all of the digits")
return out
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"
Sept. 30, 2017