0

I'm trying to write a function that takes in a string and spits out the number of repeated patterns:

string1="abcabc"

string 2="abcdabcdabcd"

solution(string1)=2

solution(string2)=3

My code is below. It works for most cases but I'm still failing a hidden test case (last one out of 10)

def solution(s):
    final_score=[]
    for x in range(1,50,1):
        pattern=s[0:x]
        repeats=[(s[i:i+x]) for i in range(x,len(s),x)]
        #print(pattern,repeats)
        if all(pattern in x for x in repeats):
            #print(len(repeats))
            final_score.append(len(repeats)+1)
        else:
            continue
    #print(final_score)
    return(max(final_score))

Any advice would be much appreciated, thanks!

Edit: For the case of "abababab", or where multiple patterns are available ("ab" and "abab"), I'm trying to return the highest frequency (in this case, "ab" repeats 4 times, so the function should return 4)

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Houster
  • 9
  • 4
  • Are you not able to find out what the final case is? Is it possible that it is to do with runtime – 7koFnMiP Aug 11 '20 at 15:27
  • Your code returns 1 for `aaazaaa`, but I was expecting to see 2. – John Gordon Aug 11 '20 at 15:30
  • 3
    What is the expected output for `abababab`? Is that four repetitions of `ab`, or two repetitions of `abab`? – John Gordon Aug 11 '20 at 15:31
  • Related: https://stackoverflow.com/questions/43035406/find-the-repeating-substring-a-string-is-composed-of-if-it-exists – Tomerikoo Aug 11 '20 at 15:32
  • Welcome to SO! Also see https://www.codewars.com/kata/5491689aff74b9b292000334 – ggorlen Aug 11 '20 at 15:34
  • @JohnGordon I need the maximum frequency of patterns. So "abababab" should read "ab" with the highest frequency (4) and return 4 Also, "aaazaaa" should return 1 because there is only 1 pattern available ("aaazaaa" itself) – Houster Aug 11 '20 at 15:50
  • Is it possible that the last test case has a non-repeating value in the first character of the string? It seems like you assume all patterns are present at the beginning of the string, but you haven't provided a problem statement to require that. – aghast Aug 11 '20 at 15:54

2 Answers2

0

It's actually possible to do this via regex - assuming, at least, that the string consists only of one repeated pattern.

import re

def solution(string):
    match = re.match(r'(.+?)\1+', string)  # matches an arbitrary-length pattern,
                                           # followed by at least one repetition
    if match:
        pattern = match.group(1)           # extract which pattern was repeated
        return len(string) // len(pattern) # number of repetitions
    else:
        return 1                           # or whatever your case is for if there  
                                           # are no repeated patterns

The regex is written non-greedy, so it will use the smallest possible pattern it finds. Though, this will work incorrectly if the pattern is repeated once, but then followed by something that isn't a full repetition. Maybe try re.fullmatch() if this is a problem.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

Making Python do the hard work:

>>> for s in 'abcabc', 'abcdabcdabcd', 'abababab', 'aaazaaa':
        print(s, len(s) // (s+s).find(s, 1))

abcabc 2
abcdabcdabcd 3
abababab 4
aaazaaa 1

Based on this.

superb rain
  • 5,300
  • 2
  • 11
  • 25