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
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):
s = s.lower()
return s.count('x') == s.count('o')
或者:
def xo(s):
return s.lower().count('x') == s.lower().count('o')
或者:(这个写的有点傻)
def xo(s):
a=0
b=0
for i in s:
if i=='x' or i=='X': #此处别忘了 or i==
a+=1
elif i=='o'or i=='O':
b+=1
return a==b