मेरा समाधान नहीं (स्पष्ट रूप से मैं न तो नीग्रिग नहीं हूँ!), लेकिन यहाँ उसका (थोड़ा संशोधित) प्रश्न शिष्टाचार का हल है:
http://nbviewer.ipython.org/url/norvig.com/ipython-xkcd1313.ipynb
वह जो कार्यक्रम देता है वह इस प्रकार है (उसका काम, मेरा नहीं):
def findregex(winners, losers):
"Find a regex that matches all winners but no losers (sets of strings)."
# Make a pool of candidate components, then pick from them to cover winners.
# On each iteration, add the best component to 'cover'; finally disjoin them together.
pool = candidate_components(winners, losers)
cover = []
while winners:
best = max(pool, key=lambda c: 3*len(matches(c, winners)) - len(c))
cover.append(best)
pool.remove(best)
winners = winners - matches(best, winners)
return '|'.join(cover)
def candidate_components(winners, losers):
"Return components, c, that match at least one winner, w, but no loser."
parts = set(mappend(dotify, mappend(subparts, winners)))
wholes = {'^'+winner+'$' for winner in winners}
return wholes | {p for p in parts if not matches(p, losers)}
def mappend(function, *sequences):
"""Map the function over the arguments. Each result should be a sequence.
Append all the results together into one big list."""
results = map(function, *sequences)
return [item for result in results for item in result]
def subparts(word):
"Return a set of subparts of word, consecutive characters up to length 4, plus the whole word."
return set(word[i:i+n] for i in range(len(word)) for n in (1, 2, 3, 4))
def dotify(part):
"Return all ways to replace a subset of chars in part with '.'."
if part == '':
return {''}
else:
return {c+rest for rest in dotify(part[1:]) for c in ('.', part[0]) }
def matches(regex, strings):
"Return a set of all the strings that are matched by regex."
return {s for s in strings if re.search(regex, s)}
answer = findregex(winners, losers)
answer
# 'a.a|i..n|j|li|a.t|a..i|bu|oo|n.e|ay.|tr|rc|po|ls|oe|e.a'
जहां विजेता और हारने वाले क्रमशः विजेता (हारने वाले) सूची होते हैं (या कोई भी 2 सूचियाँ) विस्तृत विवरण के लिए लेख देखते हैं।
/^item1|atem2|item3|item4$/
संभवत: अनपेक्षित पूर्वता है (स्ट्रिंग के साथ या तो शुरू होता हैitem1
, होता हैatem2
, होता हैitem3
, या समाप्त होता हैitem4
)।