-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.py
More file actions
82 lines (61 loc) · 2.13 KB
/
distance.py
File metadata and controls
82 lines (61 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import sys
import dictionary
import graph
def parse():
try:
dict_file, start_word, stop_word = sys.argv[1:5]
return (dict_file, start_word, stop_word)
except (IndexError, ValueError):
msg = "Usage: {} /path/to/dict startWord stopWord".format(
sys.argv[0])
print(msg)
raise(SystemExit(-1))
def check_word_lengths(start_word, stop_word):
"""Check if the both words are of equal length.
:param start_word: Starting word
:param stop_word: Stopping word
:returns: True if lengths are equal, False otherwise
:rtype: bool
"""
if len(start_word) != len(stop_word):
print("Words {} and {} are of different length".format(
start_word, stop_word))
return False
return True
def check_words(dictionary_, start_word, stop_word):
"""Check if both words are defined in the dictionary.__doc__
If either word is not found, an appropriate warning will be printed
to stdout
:param dictionary_ : dictionary object
:type: Dictionary
:param start_word: Starting word
:type: string
:param stop_word: Stopping word
:type: string
:returns: True if both words are defined, False othwerise
:rtype: bool
"""
if dictionary_.is_real_word(start_word) is False:
print("Word {} not found in the dictionary".format(start_word))
return False
if dictionary_.is_real_word(stop_word) is False:
print("Word {} not found in the dictionary".format(stop_word))
return False
return True
def main():
dict_file, start_word, stop_word = parse()
if check_word_lengths(start_word, stop_word) is False:
raise SystemExit(0)
d = dictionary.Dictionary.build(dict_file)
if check_words(d, start_word, stop_word) is False:
raise SystemExit(0)
g = graph.Graph(d)
path = g.bfs(start_word, stop_word)
if path is None:
print("There is no path between words {} and {}".format(
start_word, stop_word))
else:
print("One possible path between words: {}".format("->".join(path)))
if __name__ == "__main__":
main()