0

I am working through a program that requires me to determine if a list of strings is a word chain. A word chain is a list of words in which the last letter of a word is the first letter of the next word in the last. The program is to return True if the list is a word chain, and false otherwise. My code is as follows:

def is_word_chain(word_list):
    for i in word_list:
        if i[-1] == (i+1[0]):
            result = True
        else:
            result = False 
    return result

and it returns the following error:

SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
  if i[-1] == (i+1[0]):
Traceback (most recent call last):
  if i[-1] == (i+1[0]):
TypeError: 'int' object is not subscriptable

I am wondering how I would properly reference the next item in the list in order to execute this function.

3
  • is 1[0] in if i[-1] == (i+1[0]): a typo? Commented Mar 19, 2022 at 19:33
  • 2
    zip is your friend. Try zip(word_list, word_list[1:]) Commented Mar 19, 2022 at 19:33
  • 1
    The error is saying you can't access the 0th element of 1, because it's an int. You're trying to access the ith + 1 element of word_list, but i isn't an enumeration of the list. If you want to do it this way you need to enumerate the list and access the next element, e.g. word_list[j + 1]. I'm not saying that this is the most effective way, but if you're learning this is something worth understanding. Commented Mar 19, 2022 at 19:35

3 Answers 3

1

Given:

>>> li1=['word','chain','test','test']
>>> li2=['word','chain','test']

You can use zip to get a pair of words to test:

>>> list(zip(li1,li1[1:]))
[('word', 'chain'), ('chain', 'test'), ('test', 'test')]

Once you have that, you can return True or False like so:

>>> any(t[0][-1]==t[1][0] for t in zip(li1,li1[1:]))
True

>>> any(t[0][-1]==t[1][0] for t in zip(li2,li2[1:]))
False
Sign up to request clarification or add additional context in comments.

Comments

0

Fixed it :)

def is_word_chain(word_list):
    for i in range(len(word_list)-1):
        if word_list[i][-1] == word_list[i+1][0]:
            result = True
        else:
            result = False 
            break
    return result

print(is_word_chain(['apple', 'egg', 'grapes', 'sand']))
print(is_word_chain(['apple', 'egg', 'sand', 'grapes']))

5 Comments

You may find enumerate() helpful in the future, which gives you both the element and the index. You're currently iterating the list three times and you only need to do it twice.
@Ben, thanks for the feedback but Iterating the list 3 times, you mean the time complexity here is 3N ? I don't think that is what you meant, yes, enumerate() does mean that I don't have to reference by index but that isn't the same as iterating thrice, right ?
Sorry, yes. You're right. You're iterating the length of the list thrice but actually only iterating the list twice (via the reference by index).
@Ben, to me "iterating" means I'm performing an O(N) operation, accessing a particular index, say word[i][j] which means the Jth character of the Ith word would still not be iterating, accessing the element at any given index is O(1), so I think I'm iterating the list once, in the for loop.
If you're accessing a given index N times at O(1) complexity, you've got youreslf an O(N) operation Dhiwakar. Otherwise, your solution would have constant complexity, which we know to be untrue - if the list has 1 billion items it'll take longer than if it has 10. There's no difference between accessing the list N times and iteration.
0
def is_word_chain(word_list):
for i in range(0,len(word_list)-1):
    if word_list[i][-1] == word_list[i+1][0]:
        result = True
    else:
        result = False 
return result

in your code you iterate through strings and in if condition you are trying use that string as sort of list which is technically blunder here.

use range and iterate over word_chain as list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.