[7 kyu] Exes and Ohs

Check to see if a string has the same amount of 'x’s and 'o’s. The method must return a boolean and be case insensitive. The string can contain any char.

Examples input/output:

XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true 
// when no 'x' and 'o' is present should return true
XO("zzoo") => false

Solution :

def xo(s):
    list_o = []
    list_x = []
    test_string = s.lower()
    for letter in test_string:
        if letter == "o":
              list_o.append(letter)
        elif letter == "x":
              list_x.append(letter)
    count_o = len(list_o)
    count_x = len(list_x)
    if count_o != count_x:
        return False
    return True
发布了16 篇原创文章 · 获赞 0 · 访问量 52

猜你喜欢

转载自blog.csdn.net/HM_773_220/article/details/104764099
7
今日推荐