Tuesday, February 8, 2022

Exes and Ohs Python

 

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

def xo(s):
    count_o = 0
    count_x = 0
    for i in s:
        if i.lower() == 'x':
            count_x += 1
        if i.lower() == 'o':
            count_o += 1
    return True if count_o == count_x else False

print(xo("xxooo"))

def xo(s):
    s = s.lower()
    return s.count('x') == s.count('o')

Training on Exes and Ohs | Codewars



Related Posts


No comments :

Post a Comment