아니 내 솔루션 (! 분명 내가 피터 노르 빅 아니에요)하지만 그를 여기의 (약간 수정) 질문 의례의 솔루션입니다 :
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
).