https://codingbat.com/python/String-2
double_char
def double_char(str):
ans=""
for i in range(len(str)):
ans+=str[i]+str[i]
return ans
count_hi
def count_hi(str):
return str.count("hi")
cat_dog
def cat_dog(str):
return str.count("cat") == str.count("dog")
count_code
def check(str,idx):
if str[idx]=='c' and str[idx+1]=='o' and str[idx+3]=='e':
return 1
return 0
def count_code(str):
ans=0
for i in range(len(str)-3):
ans+=check(str,i)
return ans
end_other
def end_other(a, b):
a=a.lower()
b=b.lower()
return a.endswith(b) or b.endswith(a)
xyz_there
def xyz_there(str):
idx,n = 0,len(str)
while idx<n:
idx=str.find("xyz",idx)
if idx == 0:
return True
elif idx == -1:
return False
elif str[idx-1] != '.':
return True
idx+=3
return False