In: Computer Science
I'm working on python code which is substring matching. For example, the given string is input "CTTGTGATCTCGTGTCGTGGGTAG", and a substring we want to find in the main one is "GTGG". So the code will print out the position and the substrings in the main which has exactly one different position. For example, start at position 4 the substring is GTGA since there is only one letter different, and so on. Even though we know that string index starts at 0, but when we print out the result should be at that position plus 1.So I know this would be a long task, but can you help to break down the problem so I can see what should be done first and later. Thanks and appreciate!!
def search_str(fstring, substring):
index = 0
if substring in fstring:
c = substring[0]
for ch in fstring:
if ch == c:
if fstring[index:index+len(substring)] == substring:
return index+1
index += 1
return -1
ful_str =raw_input("Enter full string: ")
sub_str =raw_input("Enter substrin, you want to find in full
string: ")
if search_str(ful_str, sub_str) == -1:
print "Entered substring is not found "
else:
print "The position of the substring is:
",search_str(ful_str, sub_str)
Note:
1. Here i am taking two variables ful_str - full sring and sub_str -sub sring
2. Both are taking input from the console that's why i am using raw_input()
3. I'm defining search_str() for search the substring in full string.
4. If substring is finding out it returns the index position other wise it will return -1.