From 0cb71dbf9da3a1a5d653fe9dbbd2e0e1419fb700 Mon Sep 17 00:00:00 2001 From: vaibhavpandey11 <61282732+vaibhavpandey11@users.noreply.github.com> Date: Sun, 10 May 2020 16:04:15 +0530 Subject: [PATCH] Update chain-words.py This problem was asked in one of my college assignment. Here is my solution. --- challenges/chain-words.py | 62 +++++++++++++-------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/challenges/chain-words.py b/challenges/chain-words.py index 3a194ec..12ecb2e 100644 --- a/challenges/chain-words.py +++ b/challenges/chain-words.py @@ -1,42 +1,20 @@ -""" -#246 -Dropbox - -Given a list of words, determine whether the words can be chained to form a circle. -A word X can be placed in front of another word Y in a circle if the last character of X is same as the first character of Y. - -For example, the words ['chair', 'height', 'racket', 'touch', 'tunic'] can form the following circle: -chair --> racket --> touch --> height --> tunic --> chair. - -""" - -def checkForCycleHelper(usedWords, remainingWords): - if len(remainingWords) == 0: - # print(usedWords, end=" ") # Uncomment to see the chain - return True - - lastLetter = usedWords[-1][-1] - for i in range(len(remainingWords)): - nextWord = remainingWords[i] - if nextWord.startswith(lastLetter) and checkForCycleHelper(usedWords+[nextWord], remainingWords[:i]+remainingWords[i+1:]): - return True - - return False - - -def checkForCycle(words): - return checkForCycleHelper([words[0]], words[1:]) - -def main(): - print(checkForCycle(["geek", "king"])) # True ['geek', 'king'] - print(checkForCycle(["for", "geek", "rig", "kaf"])) # True ['for', 'rig', 'geek', 'kaf'] - print(checkForCycle(["aab", "bac", "aaa", "cda"])) # True ['aab', 'bac', 'cda', 'aaa'] - print(checkForCycle(["aaa", "bbb", "baa", "aab"])) # True ['aaa', 'aab', 'bbb', 'baa'] - print(checkForCycle(["aaa"])) # True ['aaa'] - print(checkForCycle(["aaa", "bbb"])) # False - print(checkForCycle(["abc", "efg", "cde", "ghi", "ija"])) # True ['abc', 'cde', 'efg', 'ghi', 'ija'] - print(checkForCycle(["ijk", "kji", "abc", "cba"])) # False - - -if __name__ == "__main__": - main() \ No newline at end of file +words = eval(input()) +length = len(words) # number of words +chain = [words[0]] # appending first word +words = words[1:] # slicing out the first word from words +for i in range(0, length - 1): + for word in words: # each word in the list + break_flag = False # used to check whether to break the loop or not + if word[0] == chain[-1][-1]: # chain forming condition + for temp_word in words: # each word in the list + # checks if the list contains word for further chain formation + if len(words) != 1 and word[-1] == temp_word[0]: + chain.append(word) # appending the word + words.remove(word) # removing the word to avoid repeated check + break_flag = True # if word added then no use of further iterations + break + elif len(words) == 1: chain.append(word) # in case only one word is left + if break_flag: break # if word already added stop further iterations +# checks whether the chain formed is correct or not +if len(chain) == length and chain[0][0] == chain[-1][-1]: print(chain) +else: print("Sorry. The words cannot be chained to form a circle")