Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+7 votes

Hey everyone! So I am trying to finish up the routine practice, and I am pretty sure my code is all good since it can identify which words are palindromes and then can take them out. However, each time I run the entire code (ispalindporems() and removePalindromes()), it completely ignores one of the words in my list.

I set up my code so that is loops through the entire list by saying "for word in aList". But in the first example ['civic', 'radar', 'hello', 'repaper', 'paper'], my final output is ['radar', 'hello', 'paper']. It doesn't even look at the word "radar", it just completely skips over it (hence why it leaves it in the list). For the second example, I get the output ['house', 'grand', 'noone', 'minim']. Again, the word "minim" isn't even taken through the loop. Does anyone know how I might be able to fix this?

asked in CSC201 Spring 2021 by (1 point)
+2

Nevermind! I messed with my code some more and tried a moving backwards strategy and that worked for me. So if anyone has similar issues, I recommend trying that. :)

+2

Yeah, the backward strategy is the go-to, it saves so much time. The reason why the forward strategy didn't work for this question is that every time you remove an item from the list, 'for word in list' looks at the next word, but the next word is in the spot of the removed word, so it's basically skipping it. If you want to know how to fix this, for every time it removes an item subtract 1 from the index

2 Answers

+2 votes

Without looking at your code, it sounds like one of your ranges may be off somewhere?? Are you putting it in range (len(numList)-1)? If that's the case maybe take off the -1 because it seems like it's stopping before the end of your list.

answered by (1 point)
+1

Hmmm I don't have any ranges besides the "for word in list", so that's why I'm so confused why it would just skip over a word.

+1 vote

I got the same problem. try working backwards instead of forward.everytime you remove an item, the index changed for the next one unless you work backward. use:
for i in range(len(alist)-1,-1,-1):

answered by (1 point)
...