Recientemente, en una entrevista de trabajo me dieron el siguiente problema:Juego de una carta ¿Problema?
escribir un script capaz de ejecutar en la línea de comandos Python
Se debe tomar en dos palabras en la línea de comandos (o opcionalmente, si lo prefiere, puede consultar al usuario para que suministre las dos palabras a través de la consola).
Teniendo en cuenta estas dos palabras: a. Asegúrese de que tengan la misma longitud b. Asegúrese de que ambas palabras estén presentes en el diccionario de palabras válidas en el idioma inglés que descargó.
Si es así, calcule si puede alcanzar la segunda palabra del primero mediante una serie de pasos como sigue a. Puede cambiar una letra a la vez b. Cada vez que cambie una letra, la palabra resultante también debe existir en el diccionario c. No puede agregar o eliminar letras
Si las dos palabras son alcanzables, la secuencia de comandos debe imprimir la ruta que conduce como una ruta única y más corta de una palabra a la otra.
Puede/usr/share/dict/words para su diccionario de palabras.
Mi solución consistió en utilizar la primera búsqueda de amplitud para encontrar una ruta más corta entre dos palabras. Pero al parecer eso no era lo suficientemente bueno para hacer el trabajo :(
¿Quieres que chicos saben lo que pude haber hecho mal? Muchas gracias.
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
Gracias por todo el gran Creo que lo que realmente me atrapó es el hecho de que repetí TODAS las palabras en el diccionario cada vez para considerar posibles palabras vecinas. En vez de eso, podría haber usado un índice invertido como señalan Duncan y Matt Anderson. he ayudado también. Muchas gracias, ahora sé lo que he hecho mal.
aquí es el mismo código con índice invertido:
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
y una comparación de tiempo: (. que se encuentra en 91.57 seg)
% pitón one_letter_game_old.py feliz hola El camino más corto es :
=> feliz - arpía - arpas - ciervos - detiene - salas - infiernos - hola% pitón one_letter_game.py feliz hola El camino más corto (encontrado en.1,71 seg) es:
=> feliz - arpía - arpas - ciervos - detiene - salas - infiernos - hola
no fui a través de su código, pero sólo porque no lo hicieron obtener el trabajo no significa que este fue su error. ¿Te dijeron eso? – MJB
Bueno, traté de preguntar, pero su política es que "no se les permite proporcionar más comentarios" ... –
Problema similar: http://stackoverflow.com/questions/2534087/successive-adding-of-char-to- get-the-longest-word-in-the-dictionary-closed – FogleBird