Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Regular Expressions: now you have two problems solution in Clear category for Long Repeat Inside by rossras
import re
def repeat_inside(line):
"""
first the longest repeating substring
"""
# Regex from the outside in:
# Outer (?=...) lookahead to capture potentially overlapping matches
# Next (...) to capture whole match as group 1
# Inner (...)\2+ to match group 2, followed by itself one or more times
# .+? the unit that gets repeated. + to ensure at least one character,
# non-greedy +? so that 'aaaaa' will match as five repetitions of 'a'
# instead of as two repetitions of 'aa'.
matches = re.findall(r'(?=((.+?)\2+))', line)
return max((m[0] for m in matches), key=len, default='')
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert repeat_inside('aaaaa') == 'aaaaa', "First"
assert repeat_inside('aabbff') == 'aa', "Second"
assert repeat_inside('aababcc') == 'abab', "Third"
assert repeat_inside('abc') == '', "Forth"
assert repeat_inside('abcabcabab') == 'abcabc', "Fifth"
print('"Run" is good. How is "Check"?')
Aug. 10, 2018
Comments: