‘’’
字符串分割(二)
给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。
对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;
反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。
输入描述
输入为两行,第一行为参数K,第二行为字符串S。
输出描述
输出转换后的字符串。‘’’
def zifuchuanfenge2(k,s):
start=len(s)
for i in range(len(s)):
if s[i]=='-':
start=i
break
head=s[:start]
s=s[start+1:]
count=j=0
ans=temp=''
upper=lower=0
print(head,s)
while j<len(s):
if s[j]=='-':
j+=1
continue
upper = upper + 1 if s[j].isupper() else upper
lower = lower + 1 if s[j].islower() else lower
temp += s[j]
count += 1
if count%k==0:
if upper>lower:
temp=temp.upper()
if upper<lower:
temp=temp.lower()
ans+='-'+temp
temp=''
upper=lower=0
j+=1
if count%k!=0:
if upper > lower:
temp.upper()
if upper < lower:
temp.lower()
ans += '-' + temp
return head+ans
print(zifuchuanfenge2(12,'12abc-abCABc-4aB@'))