0

Checking for letter matches

I have a sample string "this string gets stuck". I am trying write a loop that will compare the last last letter of a word with the first letter of the next word to see if the match. So, with this four word string I would check the last letter of "this" ("s") and see if it matches the fist letter off "string" ("s"). I know how to check this for the first and second word with string[0][-1] for the last letter, first word and string[1][0] for the first letter, second word. I am trying to figure out how to loop through each word pairs in the string to check for the same. My thought is to maybe have the first loop = string[0][-1]==string[1][0] and then have the loop advance to each successive pair of words by string[0+1][-1] and string[1+1][0] adding one to each word in the string to compare the first and last letters throughout, but I am not sure how to phrase this. I know that I could probably use regex for matches, but I am not exactly sure how I loop through with that method.

28th Sep 2021, 10:39 AM
Robert Haugland
Robert Haugland - avatar
10 odpowiedzi
+ 3
Robert Haugland See if this little snippet helps you lst = ["yellow","white","ergon","night"] for i in range(1, len(lst)): if lst[i-1][-1] == lst[i][0]: print("last letter = first letter")
28th Sep 2021, 11:03 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Prabhas Koya Just keep practicing and learning
28th Sep 2021, 7:11 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Could you give an example in full of what you want the code to do? Like the end goal of the code. Not sure if you check index 0 and 1, then 1 and 2 OR after 0 and 1, it's then 2 and 3.
28th Sep 2021, 10:54 AM
Slick
Slick - avatar
+ 1
S='this string gets stuck' print(['match' for i in range(len(S)) if S[i] == ' ' and S[i-1] == S[i+1]])
28th Sep 2021, 10:54 AM
Prabhas Koya
+ 1
there is a gap between every word so I used that as reference for checking before letter and after letter if they are equal
28th Sep 2021, 11:01 AM
Prabhas Koya
+ 1
Rik Wittkopp can you say how to write any code efficiently, I am beginner
28th Sep 2021, 12:05 PM
Prabhas Koya
+ 1
Rik Whittkopp, The code worked. I tweeked it to fit the perameters of this problem: words = input() wordsp = words.split() sum = 0 for i in range(1, len(wordsp)): if wordsp[i-1][-1]==wordsp[i][0]: sum += 1 else: print("false") exit(0) print("true") So, I had to break down what you wrote so I could understand and learn from it. In "for i in range(1, len(lst)", I figured the range would have to go the length of the string -1 so as not to run the last word last letter, etc. In my mind I was thinking more like "len(lst)-1" which I believe, also works. Then "if lst[i-1][-1]" I believe this is pointing to the last letter of the word to the left of (i) and "lst[i][0]" points to the first letter of (i). The "exit(0)" I learned from a previous problem where it refers to the "if" block being "true", thus allowing me to move the print statement to the far left to output only one reponse versus a response for each loop. Thanks again, you're a true hero!
28th Sep 2021, 3:11 PM
Robert Haugland
Robert Haugland - avatar
+ 1
Rik Whitkopp, Okay, I ran in the loop "print([i-1])" and it output "this", "string", "gets". Then "print([i-1][-1]" output the last letters of these three words "s","g","s". "Print([i])" output "string", "gets","stuck" and "print([i][0])" output the fist letter of three words "s","g","s". So, [i-1] is like slicing "string[:-1]" and [i] is "string[1:]". Interesting!
28th Sep 2021, 3:43 PM
Robert Haugland
Robert Haugland - avatar
0
in the if statement: "if i == ' ' ..." <-- what is that for?
28th Sep 2021, 10:55 AM
Slick
Slick - avatar
28th Sep 2021, 7:11 PM
Rik Wittkopp
Rik Wittkopp - avatar