3

This Python function interlocks the characters of two words (e.g., "sho" + "col" -> "school"). word1-char1 + word2-char1 + word1-char2 + ...

def interlock(a,b):
    i = 0
    c = ""
    d = "" 
    while (i < len(a) and len(b)):
        c = (a[i]+b[i])
        d = d + c
        i+=1
    return(d)

interlock("sho", "col")

Now, I would like to apply this function to a list of words. The goal is to find out any interlock corresponds to an item of a list.

word_list = ["test", "col", "tele", "school", "tel", "sho", "aye"]

To do that, I would first have to create a new list that has all the interlocks in it. This is exactly where I am stuck - I don't know how to iterate over word_list using interlock.

Thanks for your help!

9
  • 1
    i < len(a) and len(b) means (i < len(a)) and len(b) Commented Feb 20, 2015 at 10:47
  • are test-col and col-test valid pairings or do you only pair once? Commented Feb 20, 2015 at 11:04
  • @PadraicCunningham: test-col and col-test are both not valid. E.g., test + col -> tceoslt : invalid // test + tele -> tteeslte : invalid // col + test -> ctoelst : invalid // sho + col -> school : valid Commented Feb 20, 2015 at 11:10
  • but they both appear in the list? I am talking about combinations to pass to interlock Commented Feb 20, 2015 at 11:10
  • interlock is applied to individual character of the words, not the words. thanks for pointing this out! Commented Feb 20, 2015 at 11:13

3 Answers 3

1

If you want all possible permutations of the list to pass to interlock without pairing a word with itself i.e we won't get interlock("col", "col"):

def interlock(s1,s2):
    out = ""
    while s1 and s2: # keep looping until any string is empty
        out += s1[0] + s2[0]
        s1, s2 = s1[1:], s2[1:]
    return out +  s1 + s2 # add the remainder of any longer string

word_list = ["test", "col", "tele", "school", "tel", "sho","col" "aye"]

from itertools import permutations 
# get all permutations of len 2 from our word list
perms = permutations(word_list,2)

st = set(word_list)
for a, b in perms:
    res = interlock(a,b)
    if res in st:
        print(res)
school

You can also achieve the same result using itertools.zip_longest using a fillvalue of "" to catch the end of the longer words:

from itertools import permutations, zip_longest

perms = permutations(word_list, 2)

st = set(word_list)
for a, b in perms:
    res = "".join("".join(tup) for tup in zip_longest(a,b,fillvalue=""))
    if res in st:
        print(res)
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it using product function from itertools module:

from itertools import product

for a, b in product(word_list, word_list):
    interlock(a, b)

https://docs.python.org/2/library/itertools.html#itertools.product

Comments

0

Try this.

def interlockList(A):
    while Len(A) > 2:
      B = interlock(A[0],A[1])
      A.remove(A[0])
      A.remove(A[1])
      A.insert(0, B)
    return B

1 Comment

c = (a[i]+b[i]) IndexError: string index out of range -> basically, my function only works if all the strings have the same number of characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.