CodingBat python String-2

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

おすすめ

転載: blog.csdn.net/xiji333/article/details/110184825