1、字符串分割
给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。
输入描述:
输入为两行,第一行为参数K,第二行为字符串S。
输出描述:
输出转换后的字符串。
def split_string(strs, K):
# 使用列表推导式按照n个字符分割字符串
return [strs[i:i+K] for i in range(0, len(strs), K)]
def count_lowercase(strs):
# 初始化计数器为0
count = 0
# 遍历字符串的每一个字符
for char in strs:
# 如果字符是小写字母,增加计数器
if char.islower():
count += 1
# 返回小写字母的数量
return count
def count_uppercase(strs):
# 初始化计数器为0
count = 0
# 遍历字符串的每一个字符
for char in strs:
# 如果字符是大写字母,增加计数器
if char.isupper():
count += 1
# 返回大写字母的数量
return count
k=3
S='12abc-abtCABc-4aB@'
result_list=[]
sub_S_list = S.split('-')
result_list.append(sub_S_list[0])
for i in sub_S_list[1:]:
sub_sub_list = split_string(i,k)
for j in sub_sub_list:
upper_cnt = count_uppercase(j)
lower_cnt = count_lowercase(j)
if lower_cnt< upper_cnt:
t=j.upper()
elif lower_cnt> upper_cnt:
t=j.lower()
else:
t=j
result_list.append(t)
result=("-").join(result_list)
print(result)
2、
def max_number(nums):
num_strs = list(map(str, nums))
num_strs.sort(reverse=True)
max_num = int(''.join(num_strs))
return max_num
# 示例
nums = [3, 30, 34, 5, 9]
print(max_number(nums))