Sort by

recency

|

335 Discussions

|

  • + 0 comments

    Here's my code:

    from re import finditer
    s, k = input().strip(), input().strip()
    print(*[(m.start(1), m.end(1) - 1) for m in finditer(f'(?=({k}))', s)] or [(-1, -1)], sep='\n')
    
  • + 0 comments

    without using regex

    string_S = input()
    string_k = input()
    
    len_S = len(string_S)
    len_k = len(string_k)
    
    listed_script = []
    
    for idS in range(len_S):
        if(idS+(len_k-1)<len_S):
            if ''.join([string_S[idS+idk] for idk in range(len_k)]) == string_k:
                listed_script.append((idS,idS+(len_k-1)))
    
    if(len(listed_script)==0):
        listed_script.append((-1,-1))
    
    for lsc in listed_script:
        print(lsc)
    
  • + 0 comments
    import re
    S = input()
    k = input()
    R = list(re.finditer(f'(?={re.escape(k)})', S))
    if R:
        for x in R:
            print((x.start(), x.start()+len(k)-1))
    else:
        print((-1, -1))
    
  • + 0 comments

    Python 3 solution using only the exercise material, without any extra knowledge of re package.

    import re

    S = input() k = input()

    i = 0

    while i < len(S) - 1:

    m = re.search(k, S[i:])
    
    if not m:
        if i ==0:
            print((-1, -1))
        break
    
    elif m:
        print((m.start()+i, m.end()+i-1))
        i += m.end() - 1 if len(k)!=1 else m.end()
    
  • + 0 comments
    import re
    s,k = [input() for _ in range(2)]
    p = r'(?=(%s))' % re.escape(k)
    print("\n".join("(%s, %s)"%(match.start(1),match.end(1)-1) for match in re.finditer(p, s)) or "(-1, -1)")