interesting, but I am trying to avoid using any C. It's too "hardcore" for me right now. Maybe in a few years. Also the list doesn't have to be a size of 2. It can be size of N and I need to append it over and over if there's a special comparison. Better if you could represent your answer in psuedo code.
However, I am really not liking the fact that I am using check and maxNum. Do you have any idea to further improve my algorithm?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
def frags(strings):
check = 1
maxNum = 1
for x in range(0,len(strings)):
for y in range(0,len(strings)):
if x != y:
for i in range(0,len(strings[y])):
if strings[x].find(strings[y][:i]) > maxNum:
check = 0
maxNum = strings[x].find(strings[y][:i])
if check == 0:
toReturn = strings[x][:maxNum] + strings[y]
strings.append(toReturn)
check = 1
maxNum = 1
return toReturn
Ah, sorry. You did say you were sticking to Python.
You are doing way too much there. The check and maxNum exist to limit the answers you are getting.
The algorithm I gave you checks exactly two strings against each other (non-commutative). Find the number of characters that match in two strings:
- from the last to the first in string a
- from the first to the last in string b
If n >= N then you have a match.
The outer algorithm, then, needs to loop through the strings to find all valid combinations of matches > N. For each valid match, generate the result and add it to your list of results.
You will get further if you try to write it as two separate functions.