I downloaded Google's Python exercises and this is one of the questions:
Given a list of strings, return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same
My answer to this is given below:
def match_ends(words):
a=[]
for word in words:
if len(word)>=2 and word[0]==word[-1]:
a=a.append(word)
return len(a)
However, for this list:
words=['aba', 'xyz', 'aa', 'x', 'bbb']
It just returns "1". I don't understand why append
did not add all strings that match with if turn.
This is Google's solution:
def match_ends(words):
count = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
count = count + 1
return count