kata练习题

This time no story, no theory. The examples below show you how to write function accum:

Examples:

accum("abcd")    # "A-Bb-Ccc-Dddd"
accum("RqaEzty") # "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") # "C-Ww-Aaa-Tttt"


#野生程序员
def
accum(s): s=list(s) s_new=[] #列表的长度 len=s.__len__() #循环列表里面的每个元素 for i in range(len): #将每个元素的大写加入列表 s_new.append(s[i].upper()) if i > 0: for num in range(i): #将后续的元素小写加入列表 s_new.append(s[i].lower()) if i !=len-1: #将最后一个——去掉 s_new.append('-') #将列表转换为字符串 s="".join(s_new) return s


print(accum("ZpglnRxqenU"))


#高手的做法
def
accum(s):
  #c是s里面的元素,i是s里面的下标
return '-'.join(c.upper() + c.lower() * i for i, c in enumerate(s))
print(accum("ZpglnRxqenU"))

猜你喜欢

转载自www.cnblogs.com/randomlee/p/9068643.html