forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_153.py
More file actions
23 lines (20 loc) · 726 Bytes
/
problem_153.py
File metadata and controls
23 lines (20 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def get_smallest_dist(text, w1, w2):
dist = None
ls_word, ls_index = None, None
for index, word in enumerate(text.split()):
if word == w1 or word == w2:
if (word == w1 and ls_word == w2) or \
(word == w2 and ls_word == w1):
dist = index - ls_index - 1
ls_word = word
ls_index = index
return dist
# Tests
assert not get_smallest_dist(
"hello", "hello", "world")
assert get_smallest_dist(
"hello world", "hello", "world") == 0
assert get_smallest_dist(
"dog cat hello cat dog dog hello cat world", "hello", "world") == 1
assert get_smallest_dist(
"dog cat hello cat dog dog hello cat world", "dog", "world") == 2