From ed7db004aa2fd8423c71a6e20b0d6135f78fba8f Mon Sep 17 00:00:00 2001 From: anon Date: Sun, 10 Mar 2024 15:57:35 +0100 Subject: [PATCH] Added group.py --- group.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 group.py diff --git a/group.py b/group.py new file mode 100644 index 0000000..d60529d --- /dev/null +++ b/group.py @@ -0,0 +1,31 @@ +import difflib + +# Example list of strings +strings = [ + "apple pie", + "apple cider", + "apple juice", + "banana bread", + "banana split", + "banana smoothie" +] + +# Create a dictionary to store the groups +groups = {} + +# Iterate through the strings to find the longest common substrings +for i in range(len(strings)): + for j in range(i+1, len(strings)): + match = difflib.SequenceMatcher(None, strings[i], strings[j]).find_longest_match(0, len(strings[i]), 0, len(strings[j])) + common_substring = strings[i][match.a: match.a + match.size] + if common_substring: + if common_substring not in groups: + groups[common_substring] = [strings[i], strings[j]] + else: + groups[common_substring].extend([strings[i], strings[j]]) + +# Print the groups +for key, value in groups.items(): + print(f"Group: {key}") + print(value) + print()