Skip to content

Instantly share code, notes, and snippets.

@abdullahmujahidali
Last active April 23, 2020 20:48
Show Gist options
  • Save abdullahmujahidali/87da93c4658b2a96a1c0eed0138b8b94 to your computer and use it in GitHub Desktop.
Save abdullahmujahidali/87da93c4658b2a96a1c0eed0138b8b94 to your computer and use it in GitHub Desktop.
Write a function neighboursRime(...) that receives a list (with at least 3 strings(words), and returns the number of “riming contiguous words” in the list. (The last word in the list is considered to be contiguous to the first word in the list.)
def neighboursRime(words):
pairs=0 # pairs found
done=[] # list of words that are checked
counter=0 # index of the list
contiguous="" # contuguous words
# iterate the list
for word in words:
if not word in done:
# get the length of string
length = len(word)
# Get last character of string
last_char = word[length -1]
# look for a word with same last char in the list
i=counter+1
contiguous=word
while i<len(words):
l=len(words[i])
lchar=words[i][l-1]
if last_char==lchar:
contiguous=contiguous+", "+words[i]
done.append(words[i])
i+=1
done.append(word)
counter+=1
print(contiguous)
pairs+=1
words= ["Axa","Bxb","Cxa", "Dxxa", "Ea","Fxb","Gxb"]
neighboursRime(words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment