double_char('The') → 'TThhee'
double_char('AAbb') → 'AAAAbbbb'
double_char('Hi-There') → 'HHii--TThheerree'
def double_char(str):
result = ""
for i in range(len(str)):
result += str[i:i+1] + str[i:i+1]
return result
count_hi('abc hi ho') → 1
count_hi('ABChi hi') → 2
count_hi('hihi') → 2
def count_hi(str):
count = 0
for i in range(len(str)-1):
if str[i:i+2] == "hi":
count += 1
return count
cat_dog('catdog') → True
cat_dog('catcat') → False
cat_dog('1cat1cadodog') → True
def cat_dog(str):
catCount = 0
dogCount = 0
for i in range(len(str) - 2):
if str[i : i + 3] == "cat":
catCount += 1
elif str[i : i + 3] == "dog":
dogCount += 1
return catCount == dogCount
Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.
count_code('aaacodebbb') → 1
count_code('codexxcode') → 2
count_code('cozexxcope') → 2
def count_code(str):
count = 0
for i in range(len(str)):
if str[i-2:i] == "co" and str[i+1:i+2] == "e":
count += 1
return count
Given two strings, return True if either of the strings appears at the very end of the other string, ignoring upper/lower case differences (in other words, the computation should not be "case sensitive"). Note: s.lower() returns the lowercase version of a string.
end_other('Hiabc', 'abc') → True
end_other('AbC', 'HiaBc') → True
end_other('abc', 'abXabc') → True
def end_other(a, b):
return a.lower().endswith(b.lower()) or b.lower().endswith(a.lower())
Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.
xyz_there('abcxyz') → True
xyz_there('abc.xyz') → False
xyz_there('xyz.abc') → True
def xyz_there(str):
for i in range(len(str) - 2):
if str[i:i+3] == "xyz" and str[i-1:i] != ".":
return True
return False