start() & end()
These expressions return the indices of the start and end of the substring matched by the group.
Code
>>> import re
>>> m = re.search(r'\d+','1234')
>>> m.end()
4
>>> m.start()
0
Task
You are given a string .
Your task is to find the indices of the start and end of string in .
Input Format
The first line contains the string .
The second line contains the string .
Constraints
Output Format
Print the tuple in this format: (start _index, end _index).
If no match is found, print (-1, -1)
.
Sample Input
aaadaa
aa
Sample Output
(0, 1)
(1, 2)
(4, 5)
My Solution
import re
target_string = input()
target_find = input()
result = re.findall(target_find, target_string)
store = []
if(result):
for i in range(len(target_string)):
if(target_string[i:i+len(target_find)] == target_find):
store.append(tuple((i,(i+len(target_find))-1 )))
else:
store.append(tuple((-1,-1)))
for i in store:
print(i)
Source : HackerRank
No comments:
Post a Comment