0
Exses 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 Here is my code please explain why 26 test cases are right and 2 are wrong. Check your and my code here: https://www.codewars.com/kata/55908aad6620c066bc00002a/train/JUMP_LINK__&&__python__&&__JUMP_LINK My code: def xo(s): a = s.count('x') b = s.count('o') if a == b: return True elif 'x' and 'o' not in s: return True else: return False
1 Answer
+ 1
1. Your solution isn't case insensitive (unless you're altering the string prior to calling the function)
s = s.lower()
2. You don't need the if-elif-else block and can just;
return s.count('x') == s.count('o')
def xo(s):
s = s.lower()
return s.count('x') == s.count('o')