Files
autoconfig
cgi
cross_compile
flex
gdb_pretty_print
linking
.gitignore
1st_day_of_month.cpp
JJbY.cpp
Makefile
alternatice_bracket.c
ascii_injection.py
ascii_nn_input_helper.py
bind_test.sh
blumba.html
bootrap.html
brython_hw.html
c.php
c_old_argument_notation.c
cnn.c
comp.c
conditional_const.c
const.c
cpp_regex_error.cpp
current_year.cpp
dda2.cpp
dog.jpg
dog2.jpg
dpt.py
dropdown.html
else_while.c
example.m4
extension_cut.Makefile
fddl.js
fizzbuzz.f90
for_ctags.cpp
format.py
free_null.c
gcc_include_next.c
gdb_graph.c
getopt_test.c
gnu_decimals.c
gnu_history.c
gnu_regex.c
gnu_regex2.c
graph.py
group.py
guards.hs
header.h
index_in_initializer_list_compiler_extension.c
initials_test.html
int_memory.ctype.py
int_memory.py.py
knr.c
levenshtein_dist_-_usage.cpp
map_initialization.cpp
menu.vim
ncurses_labels.c
ncurses_mv_win.cpp
ncurses_plus_readline.cpp
ncurses_resize2.cpp
ncurses_resize_hello_world.cpp
ncurses_resize_test.c
ncurses_scroll.c
nf.c
null_printf.c
null_printf.cpp
otest.py
portable_namespace.c
portable_namespace.h
pta.c
tests/group.py
2024-03-10 15:57:35 +01:00

32 lines
910 B
Python

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()