Sort by

recency

|

338 Discussions

|

  • + 0 comments

    Different approach :

    import re, sys
    
    S = input()
    k = input()
    
    def occurences_indexer(text, target):
        occurences_indexs = []
        initial_text_len = len(text)
    
        while True:
            occurence = re.search(f'{target}', text)  
    
            if occurence != None:
                delta_target_len = (initial_text_len-len(text))
                occurences_indexs.append((occurence.start()+delta_target_len, occurence.end()+delta_target_len-1))
    
                if re.search(f'{target[0]}', target[1:]) != None: #Searchs for occurence stacking
                    text = re.sub(f'{target}', f'{target[1:]}', text, count=1)
                    
                else:
                    text = re.sub(f'{target}', '', text, count=1)
    
                continue
            
            else:
                break
    
            first_search = False
        
        if occurences_indexs == []:
            occurences_indexs.append((-1, -1))
        
        return occurences_indexs
        
    for index_tuple in occurences_indexer(S, k):
        print(index_tuple)
        
    
  • + 0 comments
    import re
    
    full_string = input().strip()
    search_string = input().strip()
    l = []
    i = 0
    while i <= len(full_string):
        string = re.search(search_string , full_string[i:] )
        if(string):
            l.append(tuple([string.start() + i, string.end() - 1 + i]))
            i += int(string.span()[0]) + 1
        else:
            break;
    [print(x) for x in l] if len(l)!=0 else print("(-1, -1)")
    
  • + 0 comments

    import re

    s = input() k = input() pattern = rf"(?=({re.escape(k)}))" if matches := list(re.finditer(pattern, s)):

    for match in matches:
        print((match.start(1),int(match.end(1) - 1)))
    

    else: print((-1, -1))

  • + 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)