The Challenge :
You have a list conversations, in which each element is a conversation that is represented as an array of words. You need to create a chatbot that will complete a conversation that is currently in progress,
currentConversation.To do that, the chatbot must find the conversation from the given list that has the largest number of unique words that match with words from the
currentConversation. If there are several conversations that match this condition, the chatbot should use the one that appears first in conversations. If no conversation from the list contains any matching words fromcurrentCoversation, the chatbot should leavecurrentConversationas it is.If there is a conversation that can complete
currentConversation, the chatbot should find the first word in it that appears after all the matching words. The chatbot should then append this word, along with all the words that follow it in that conversation, tocurrentConversation.Return the final state of
currentConversation.Example
For conversations =
[ ["where", "are", "you", "live", "i", "live", "in", "new", "york"], ["are", "you", "going", "somewhere", "tonight", "no", "i", "am", "too", "tired", "today"], ["hello", "what", "is", "your", "name", "my", "name", "is", "john"]] and currentConversation = ["hello", "john", "do", "you", "have", "a", "favorite", "city", "to", "live", "in", "yes", "it", "is"], the output should be chatBot(conversations, currentConversation) = ["hello", "john", "do", "you", "have", "a", "favorite", "city", "to", "live", "in", "yes", "it", "is", "new", "york"].The second conversation has only one matching word, "you". But the other two conversations both have three unique matching words. In the first conversation, the matches are "you", "live", and "in". In the third conversation, the matches are "hello", "john", and "is". Since we have two options that could complete our current conversation, we should choose the one that appears earlier in the list, so we use the first conversation. In that conversation, the last matching word is "in", so we add the last two words, "new" and "york", to
currentConversationto complete it.For conversations =
[ ["lets", "have", "some", "fun"], ["i", "never", "get", "it"], ["be", "aware", "of", "this", "house"], ["he", "will", "call", "her"]] and currentConversation = ["can", "you", "please"], the output should be chatBot(conversations, currentConversation) = ["can", "you", "please"].None of the conversations have any words that match words in
currentConversation, so we add nothing to it.Input/Output
[time limit] 4000ms (py3) [input] array.array.string conversationsAn array of conversations, where each conversation is represented as an array of strings. Each string contains only lowercase English letters.
Guaranteed constraints:
1 ≤ conversations.length ≤ 1041 ≤ conversations[i].length < 1001 ≤ conversations[i][j].length ≤ 15
[input] array.string currentConversationThe conversation in progress, which needs to be completed by the chatbot. Each string contains only lowercase English letters.
Guaranteed constraints:
1 ≤ currentConversation.length ≤ 1001 ≤ currentConversation[i].length ≤ 15
[output] array.stringThe completed
currentConversation.
MY SOLUTION ok i compiled it, it works but it is not fast enough
def is_unique(word,wlist): nb = 0 for w in wlist: if w == word: nb = nb+1 if nb == 1: return True return False def find_max(conversations_stats): maxs = conversations_stats[0] ind_max = 0 for x in range(1,len(conversations_stats)): if conversations_stats[x] > maxs: maxs = conversations_stats[x] ind_max = x return ind_max, maxs def chatBot(conversations, currentConversation): rslt = currentConversation lc = len(conversations) conversations_stats = [0 for i in range(lc)] conversations_li = [0 for i in range(lc)] #for x in range(lc): for x, wlist in enumerate(conversations, start=0): # Python indexes start at zero #wlist = conversations[x] #wl = len(wlist) conversations_li[x]=0 conversations_stats[x] = 0 #for y in range(wl): for y, a_word in enumerate(wlist, start=0): #a_word = wlist[y] if a_word in currentConversation: if is_unique(a_word,wlist): conversations_stats[x] = conversations_stats[x] + 1 conversations_li[x]=y #print('word c'+str(conversations_li[x])+' cc'+str(x)+' :'+a_word) # ok the one with max unique matching ind_max, maxs = find_max(conversations_stats) #seaching for the last match #print(maxs) if maxs == 0: return rslt else: wlist = conversations[ind_max] cl=len(wlist) for k in range(conversations_li[ind_max]+1,cl): rslt.append(wlist[k]) return rslt return rslt