Reputation: 40
I'm writing a function that takes two arguments: a search string
and a target string
. The function will look for the search string within the target string and should return a string representing where in the target string the search string was found.
For example if the target string is target_string = "Georgia Tech"
and search_string = "Georgia"
it should return the string "Beginning"
. If the search string is "gia"
it should return "Middle"
and if the search string is "Tech"
it should return "End"
Code:
def string_finder(target_string, search_string):
len_search = int(len(search_string))
len_target = int(len(target_string))
if search_string in target_string:
if target_string.find(search_string)<=len_search:
return "Beginning"
elif #***missing statement:***
return "Middle"
elif target_string.find(search_string, -len_search):
return "End"
else:
return "Not found"
The code seems to work as is now but I'm having trouble figuring out what condition should be met in order to determine that the the search string is in the middle of the target string.
My guess is that the statement should look like this:
target_string.find(search_string, len_str, len_dif ):
Where len_dif
is the difference between the lengths of search_string and target string. By doing that it prints the correct answer but messes up the final elif
. So if I run the following code
print(string_finder("Georgia Tech", "Georgia"))
print(string_finder("Georgia Tech", "gia"))
print(string_finder("Georgia Tech", "Tech"))
print(string_finder("Georgia Tech", "Nothing"))
Instead of printing "Beginning" , "Middle" , "End" and "Not Found" it prints "Beginning" , "Middle" , "Middle" and "Not Found"
Upvotes: 0
Views: 1688
Reputation: 6185
I find the following code rather clear:
from __future__ import print_function
target_string = "Georgia Tech"
for search_string in ("Georgia", "gia", "Tech", "bla"):
print("Result of searching for %s in %s: " % (search_string, target_string),
end="")
if target_string.startswith(search_string):
print("Beginning")
elif target_string.endswith(search_string):
print("End")
elif target_string.find(search_string) != -1:
print("Middle")
else:
print("Absent")
which gives:
Result of searching for Georgia in Georgia Tech: Beginning
Result of searching for gia in Georgia Tech: Middle
Result of searching for Tech in Georgia Tech: End
Result of searching for bla in Georgia Tech: Absent
Upvotes: 0
Reputation: 2144
Don't reinvent the wheel. Python has some useful string methods you could use:
if target_string.startswith(search_string):
return 'Beginning'
elif target_string.endswith(search_string):
return 'End'
elif target_string in search_string):
return 'Middle'
else return 'Not Found'
Upvotes: -1
Reputation: 15310
Your code confirms that the search string is in the target string. That's fine. The only question is the circumstances under which you will return one of the result strings.
I'd suggest that you compute the location of the string one time and then use that number in the rest of your code:
pos = target_string.find(search_string)
if pos < len_search:
return "Beginning"
Next, you're unclear on what condition to use for the "middle" portion of the check. That's also fine. Usually, when I'm unclear on something like that I just let it be "the rest" or "the default."
When you're doing an if/elif type construct, "the default" is the else:
block:
elif pos >= len_target - 2 * len_search:
return "End"
else:
return "Middle"
Please note that when the search string is "exactly" at the end, the result of str.find()
is going to be the location of the start of the search string, which is len_target - len_search
. If you want any "fuzzy" in your "End" result, you'll have to allow some extra. I chose to use 2 * len_search
because it matches your allowance at the beginning.
Upvotes: 0
Reputation: 71451
You can try something like this:
s = "Georgia Tech"
target = "Georgia"
if target in s.split():
the_index = s.split().index(target)
if the_index == len(s.split())-1:
print "end"
elif the_index == 0:
print "Beginning"
else:
the_index = s.index(target)
if the_index > 0 and the_index < len(s) -1:
print "middle"
elif the_index == 0:
print "Beginning"
elif the_index == len(s)-2:
print "end"
Output:
Beginning
Upvotes: 0
Reputation: 402433
The right condition for "End"
is to see whether s1.find(s2) + len(s2) == len(s1)
or not.
In [660]: def string_finder(s1, s2):
...: i = s1.find(s2)
...: if i == 0:
...: return "Beginning"
...:
...: elif i > 0:
...: if i + len(s2) == len(s1):
...: return "End"
...: else:
...: return "Middle"
...:
...: else:
...: return "Not Found"
...:
In [661]: string_finder("Georgia Tech", "Georgia")
...: string_finder("Georgia Tech", "gia")
...: string_finder("Georgia Tech", "Tech")
...: string_finder("Georgia Tech", "Nothing")
...:
Beginning
Middle
End
Not Found
Upvotes: 2