【发布时间】:2011-02-12 20:55:29
【问题描述】:
最近在一次求职面试中,我遇到了以下问题:
编写一个能够在命令行上运行的脚本为python
它应该在命令行中包含两个单词(或者,如果您愿意,它可以通过控制台查询用户以提供这两个单词)。
鉴于这两个词: 一种。确保它们的长度相等 湾。确保它们都是英语有效单词词典中的单词 你下载的那个。
如果是这样,通过以下一系列步骤计算您是否可以从第一个单词到达第二个单词 一种。您可以一次更改一个字母 湾。每次更改字母时,结果单词也必须存在于字典中 C。您不能添加或删除字母
如果这两个词是可达的,脚本应该打印出从一个词到另一个词的单一最短路径。
您可以将 /usr/share/dict/words 用于您的词典。
我的解决方案包括使用广度优先搜索来找到两个单词之间的最短路径。但显然这还不足以得到这份工作:(
你们知道我做错了什么吗?非常感谢。
import collections
import functools
import re
def time_func(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
timed = time.time() - start
setattr(wrapper, 'time_taken', timed)
return res
functools.update_wrapper(wrapper, func)
return wrapper
class OneLetterGame:
def __init__(self, dict_path):
self.dict_path = dict_path
self.words = set()
def run(self, start_word, end_word):
'''Runs the one letter game with the given start and end words.
'''
assert len(start_word) == len(end_word), \
'Start word and end word must of the same length.'
self.read_dict(len(start_word))
path = self.shortest_path(start_word, end_word)
if not path:
print 'There is no path between %s and %s (took %.2f sec.)' % (
start_word, end_word, find_shortest_path.time_taken)
else:
print 'The shortest path (found in %.2f sec.) is:\n=> %s' % (
self.shortest_path.time_taken, ' -- '.join(path))
def _bfs(self, start):
'''Implementation of breadth first search as a generator.
The portion of the graph to explore is given on demand using get_neighboors.
Care was taken so that a vertex / node is explored only once.
'''
queue = collections.deque([(None, start)])
inqueue = set([start])
while queue:
parent, node = queue.popleft()
yield parent, node
new = set(self.get_neighbours(node)) - inqueue
inqueue = inqueue | new
queue.extend([(node, child) for child in new])
@time_func
def shortest_path(self, start, end):
'''Returns the shortest path from start to end using bfs.
'''
assert start in self.words, 'Start word not in dictionnary.'
assert end in self.words, 'End word not in dictionnary.'
paths = {None: []}
for parent, child in self._bfs(start):
paths[child] = paths[parent] + [child]
if child == end:
return paths[child]
return None
def get_neighbours(self, word):
'''Gets every word one letter away from the a given word.
We do not keep these words in memory because bfs accesses
a given vertex only once.
'''
neighbours = []
p_word = ['^' + word[0:i] + '\w' + word[i+1:] + '$'
for i, w in enumerate(word)]
p_word = '|'.join(p_word)
for w in self.words:
if w != word and re.match(p_word, w, re.I|re.U):
neighbours += [w]
return neighbours
def read_dict(self, size):
'''Loads every word of a specific size from the dictionnary into memory.
'''
for l in open(self.dict_path):
l = l.decode('latin-1').strip().lower()
if len(l) == size:
self.words.add(l)
if __name__ == '__main__':
import sys
if len(sys.argv) not in [3, 4]:
print 'Usage: python one_letter_game.py start_word end_word'
else:
g = OneLetterGame(dict_path = '/usr/share/dict/words')
try:
g.run(*sys.argv[1:])
except AssertionError, e:
print e
感谢您提供的所有出色答案。我认为真正吸引我的是我每次都会遍历字典中的所有单词以考虑可能的单词邻居。相反,我可以使用 Duncan 和 Matt Anderson 指出的倒排索引。 A* 方法肯定也会有所帮助。非常感谢,现在我知道我做错了什么。
下面是倒排索引的相同代码:
import collections
import functools
import re
def time_func(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
timed = time.time() - start
setattr(wrapper, 'time_taken', timed)
return res
functools.update_wrapper(wrapper, func)
return wrapper
class OneLetterGame:
def __init__(self, dict_path):
self.dict_path = dict_path
self.words = {}
def run(self, start_word, end_word):
'''Runs the one letter game with the given start and end words.
'''
assert len(start_word) == len(end_word), \
'Start word and end word must of the same length.'
self.read_dict(len(start_word))
path = self.shortest_path(start_word, end_word)
if not path:
print 'There is no path between %s and %s (took %.2f sec.)' % (
start_word, end_word, self.shortest_path.time_taken)
else:
print 'The shortest path (found in %.2f sec.) is:\n=> %s' % (
self.shortest_path.time_taken, ' -- '.join(path))
def _bfs(self, start):
'''Implementation of breadth first search as a generator.
The portion of the graph to explore is given on demand using get_neighboors.
Care was taken so that a vertex / node is explored only once.
'''
queue = collections.deque([(None, start)])
inqueue = set([start])
while queue:
parent, node = queue.popleft()
yield parent, node
new = set(self.get_neighbours(node)) - inqueue
inqueue = inqueue | new
queue.extend([(node, child) for child in new])
@time_func
def shortest_path(self, start, end):
'''Returns the shortest path from start to end using bfs.
'''
assert self.in_dictionnary(start), 'Start word not in dictionnary.'
assert self.in_dictionnary(end), 'End word not in dictionnary.'
paths = {None: []}
for parent, child in self._bfs(start):
paths[child] = paths[parent] + [child]
if child == end:
return paths[child]
return None
def in_dictionnary(self, word):
for s in self.get_steps(word):
if s in self.words:
return True
return False
def get_neighbours(self, word):
'''Gets every word one letter away from the a given word.
'''
for step in self.get_steps(word):
for neighbour in self.words[step]:
yield neighbour
def get_steps(self, word):
return (word[0:i] + '*' + word[i+1:]
for i, w in enumerate(word))
def read_dict(self, size):
'''Loads every word of a specific size from the dictionnary into an inverted index.
'''
for w in open(self.dict_path):
w = w.decode('latin-1').strip().lower()
if len(w) != size:
continue
for step in self.get_steps(w):
if step not in self.words:
self.words[step] = []
self.words[step].append(w)
if __name__ == '__main__':
import sys
if len(sys.argv) not in [3, 4]:
print 'Usage: python one_letter_game.py start_word end_word'
else:
g = OneLetterGame(dict_path = '/usr/share/dict/words')
try:
g.run(*sys.argv[1:])
except AssertionError, e:
print e
还有时间对比:
% python one_letter_game_old.py 开心 你好最短路径(在 91.57 秒)是:
=> 快乐 -- harpy -- 竖琴 -- harts -- 停止 -- 大厅 -- 地狱 -- 你好% python one_letter_game.py 开心 你好最短路径(在 1.71 秒)是:
=> 快乐 -- harpy -- 竖琴 -- harts -- 停止 -- 大厅 -- 地狱 -- 你好
【问题讨论】:
-
我没有检查你的代码,但仅仅因为你没有得到这份工作并不意味着这是你的错误。他们告诉你了吗?
-
好吧,我想问,但他们的政策是“不允许他们提供进一步的反馈”......
-
我同意 MJB。可能有更有效的解决方案,但您的代码看起来不错。如果他们变得含糊不清且不善于交流,我无法想象那会是一个有趣的工作场所。
-
求职面试问题似乎有点疯狂。除非这是一个互联网类型的问题或与求职面试官类似的情况,否则我不会期望一个完美的解决方案。不过,正如其他人所说,振作起来,仅此并不意味着你没有得到这份工作。有时面试不会成功。
标签: python optimization letter