Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
631417e
commit ccfd96d
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Given a sorted array of strings which is interspersed with empty strings, write a method to find the location of | ||
a given string. | ||
""" | ||
|
||
# @param string str the string to be located | ||
# @param string str_lst list of strings interspersed with empty strings | ||
def find_string_location(str, str_lst, start, end): | ||
if start > end or not str_lst or not str: | ||
return -1 | ||
|
||
middle = (start + end) / 2 | ||
|
||
middle_str = str_lst[middle] | ||
if middle_str == "": | ||
left = middle - 1 | ||
right = middle + 1 | ||
|
||
while True: | ||
if left < start and right > end: | ||
return -1 | ||
|
||
if left >= start and str_lst[left] != "": | ||
middle = left | ||
break | ||
elif right <= end and str_lst[right] != "": | ||
middle = right | ||
break | ||
|
||
left = left - 1 | ||
right = right + 1 | ||
|
||
if str_lst[middle] == str: | ||
return middle | ||
elif str > middle_str: | ||
return find_string_location(str, str_lst, middle+1, end) | ||
elif str < middle_str: | ||
return find_string_location(str, str_lst, start, middle-1) | ||
|
||
if __name__ == "__main__": | ||
assert find_string_location("danger", ["apple", "", "", "danger", "edible", "viscous", "", "zip"], 0, 7) == 3 | ||
assert find_string_location("apple", ["apple", "", "", "danger", "edible", "viscous", "", "zip"], 0, 7) == 0 | ||
assert find_string_location("zip", ["apple", "", "", "danger", "edible", "viscous", "", "zip"], 0, 7) == 7 | ||
assert find_string_location("", ["apple", "", "", "danger", "edible", "viscous", "", "zip"], 0, 7) == -1 | ||
print "All test cases passed successfully." |