Header Ad

HackerRank Re.start() & Re.end() solution in python

In this Re.start() & Re.end() problem, You are given a string S. Your task is to find the indices of the start and end of string K in S.

HackerRank Re.start() & Re.end() solution in pythonHackerRank Re.start() & Re.end() solution in python


Problem solution in Python 2 programming.

import re
S = raw_input()
k = raw_input()
anymatch = 'No'
for m in re.finditer(r'(?=('+k+'))',S):
    anymatch = 'Yes'
    print (m.start(1),m.end(1)-1)
if anymatch == 'No':
    print (-1, -1)



Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
S = input()
k = input()
import re
pattern = re.compile(k)
r = pattern.search(S)
if not r: print("(-1, -1)")
while r:
    print("({0}, {1})".format(r.start(), r.end() - 1))
    r = pattern.search(S,r.start() + 1)


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
s1=raw_input()
s2=raw_input()
import re
pattern = re.compile(s2)
lenn=len(s1)

#print m
output=set([])

for i in range(lenn):
    m=re.search(s2,s1[i:])
    if m is None:
        continue
    if m.start()+i in output:
        continue
    else:
        output.add(i+m.start())
        print (i+m.start(),i+m.end()-1)
if len(output)==0:
    print (-1,-1)
#for n in m:
#    print n.start(), n.end()-1
#print m


Problem solution in pypy3 programming.

import re
s, k = input(), input()
matches = list(re.finditer(r'(?={})'.format(k), s))
if matches:
    print('\n'.join(str((match.start(),
          match.start() + len(k) - 1)) for match in matches))
else:
    print('(-1, -1)')


Post a Comment

0 Comments