Answer to Question #91189 in Python for Promise Omiponle

Question #91189
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.
Word1= ['aba', 'xyz', 'aa', 'x', 'bbb']
Word2 = ['', 'x', 'xy', 'xyx', 'xx']
Word3 = ['aaa', 'be', 'abc', 'hello']
1
Expert's answer
2019-06-28T10:12:36-0400

Create a function that as an argument gets a list of strings and returns the number of elements matching the conditions.

def count_string(arg_list):
    result = 0 
    for item in arg_list:
        if (len(item) >= 2 and item[0] == item[-1]):
            result += 1
    return result


Example of a function in a python interpreter:

Word1= ['aba', 'xyz', 'aa', 'x', 'bbb'] 
count_string(Word1)
3




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS