Skip to content

Instantly share code, notes, and snippets.

@wacotaqo
Created January 19, 2009 20:15
Show Gist options
  • Select an option

  • Save wacotaqo/49152 to your computer and use it in GitHub Desktop.

Select an option

Save wacotaqo/49152 to your computer and use it in GitHub Desktop.
Python Challenge #2
===================
URL: http://www.pythonchallenge.com/pc/def/ocr.html
Image of open book with blur texts.
Caption: recognize the characters. maybe they are in the book,
but MAYBE they are in the page source.
Page Source: Message to "Find rare characters in the mess below" and then a jumble of characters.
Solution:
To extra data:
import urllib
lines = urllib.open(r'http://www.pythonchallenge.com/pc/def/ocr.html').readlines()
input = ''.join(lines[37:-2])
Counting the number of occurrences of each character in string input, shows that a few alphabets occur 1 time only. But which order are they in?
dct = {}
order = []
for a in input:
if not a in dct:
dct[a] = 0
order.append(a)
dct[a] += 1
The order in the dict (or a set if used) would be inpredictable. To get the right order, by order of occurrence, compare by position in the list order:
''.join(sorted([c for c in dct if dct[c] == 1], cmp=lambda x,y: order.index(x) - order.index(y)))
gives ==>
equality
URL:
http://www.pythonchallenge.com/pc/def/equality.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment