ではない私の解決策(明らかに私はピーター・ノーヴィグないよ!)が、ここでは彼の(わずかに変更された)質問の礼儀のソリューションです:
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
)。