Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dhravya/8c424389ef76a1475dab8c42049d9e69 to your computer and use it in GitHub Desktop.
Save Dhravya/8c424389ef76a1475dab8c42049d9e69 to your computer and use it in GitHub Desktop.
text = """
ipsum doloridk sit amet, consectetur adipiscing elit.
sed non risus.adiidkidk
suspendisse lectus tortor,adi dignissim sit amet, adipiscing nec, ultricies sed, dolor.
cras elementum ultrices diam.idkidkidk maecenas ligula massa, varius a, semper congue, euismod non, mi. """
txt1 = "loremhehe loremhehe lorem ipsumhehe ipsum ipsum"
def find_all_non_overlapping_repeating_substrings(text: str):
text = (text + ' ') if text[-1] != ' ' else text
repeating_substrings = {}
f = text
for i in range(len(text)):
for j in range(i + 3, min(i + 31, len(text))):
substring = text[i:j]
if substring in text[j:]:
if substring in repeating_substrings:
repeating_substrings[substring] += 1
else:
repeating_substrings[substring] = 2
for substring in list(repeating_substrings.keys()):
if repeating_substrings[substring] < 3:
repeating_substrings.pop(substring)
for substring in list(repeating_substrings.keys()):
for substring2 in list(repeating_substrings.keys()):
if substring != substring2 and substring in substring2:
repeating_substrings.pop(substring)
break
n_replace_ran = 0
final_list = []
def replace_shit(txt):
nonlocal n_replace_ran
nonlocal f
n_replace_ran += 1
if f.count(txt) == value:
f = f.replace(txt, "")
final_list.append(txt)
n_replace_ran = 0
else:
if 63 > n_replace_ran >= 31:
replace_shit(txt[:-1])
elif n_replace_ran >= 63:
return
else:
replace_shit(txt[1:])
for key in repeating_substrings:
value = repeating_substrings[key]
replace_shit(key)
return final_list
print(find_all_non_overlapping_repeating_substrings(text))
print(find_all_non_overlapping_repeating_substrings(txt1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment