We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defcrosswordPuzzle(crossword,words):# Write your code heresolSteps=[]words=words.split(';')foriinrange(len(crossword)):forjinrange(len(crossword[i])):ifcrossword[i][j]=='-':ifj<len(crossword[i])-1andcrossword[i][j+1]=='-'and(crossword[i][j-1]!='-'andj>0):k=0whilecrossword[i][j+k]=='-':ifj+k==9:k+=1breakk+=1solSteps.append("h"+str(i)+str(j)+str(k))elifj==0andcrossword[i][1]=='-':k=0whilecrossword[i][k]=='-':ifk==9:k=10breakk+=1solSteps.append("h"+str(i)+str(j)+str(k))elifi<len(crossword)-1andi>0andcrossword[i+1][j]=='-'andcrossword[i-1][j]!='-':k=0forxinrange(i,len(crossword)):ifcrossword[x][j]!='-':breakk+=1solSteps.append("v"+str(i)+str(j)+str(k))elifi==0andcrossword[1][j]=='-':k=0forxinrange(i,len(crossword)):ifcrossword[x][j]!='-':breakk+=1solSteps.append("v"+str(i)+str(j)+str(k))defcanFillWord(word,blankword):orientation,row,col,size=blankword[0],blankword[1],blankword[2],blankword[3:]row,col,size=int(row),int(col),int(size)iflen(word)!=size:returnFalseiforientation=='v':forrinrange(row,row+len(word)):ifcrossword[r][col]!="-"andcrossword[r][col]!=word[r-row]:returnFalseforrinrange(row,row+len(word)):ifcrossword[r][col]=="-"orcrossword[r][col]==word[r-row]:l=list(crossword[r])l[col]=word[r-row]crossword[r]="".join(l)iforientation=='h':forcinrange(col,col+len(word)):ifcrossword[row][c]!="-"andcrossword[row][c]!=word[c-col]:returnFalsel=list(crossword[row])forcinrange(col,col+len(word)):ifcrossword[row][c]=="-"orcrossword[row][c]==word[c-col]:l[c]=word[c-col]crossword[row]="".join(l)returnTruedefunFill(word,blankword):orientation,row,col,size=blankword[0],int(blankword[1]),int(blankword[2]),int(blankword[3:])iforientation=='v':forrinrange(row,row+len(word)):ifcrossword[r][col]==word[r-row]:l=list(crossword[r])l[col]="-"crossword[r]="".join(l)iforientation=='h':l=list(crossword[row])forcinrange(col,col+len(word)):ifcrossword[row][c]==word[c-col]:l[c]="-"crossword[row]="".join(l)defsolve(words,solSteps,i=0,k=0,done=[]):iflen(done)==len(words):returncrosswordifcanFillWord(words[i],solSteps[k]):ifwords[i]notindone:ifk<len(solSteps)-1:k+=1done.append(words[i])i=0returnsolve(words,solSteps,i,k,done)else:ifi<len(words)-1:i+=1else:i=0ifdone:w=done[len(done)-1]forxinrange(len(words)):iflen(words[x])==len(w)andwords[x]!=w:i=xunFill(done[len(done)-1],solSteps[k-1])deldone[len(done)-1]ifdone:canFillWord(done[len(done)-1],solSteps[len(done)-1])breakk=len(done)print(words[i],solSteps[k])returnsolve(words,solSteps,i,k,done)returnsolve(words,solSteps,i=0,k=0,done=[])
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Crossword Puzzle
You are viewing a single comment's thread. Return to all comments →