1. 问题描述:
给定一个单词,单词中既可能包含大写字母也可能包含小写字母。现在,要将单词进行统一大小写的操作,具体操作规则为:
如果一个单词中包含的大写字母数量大于小写字母数量,则将所有字母统一为大写,例如 ViP 应改为 VIP。
如果一个单词中包含的大写字母数量小于小写字母数量,则将所有字母统一为小写,例如 HoUse 应改为 house。
如果一个单词中包含的大写字母数量等于小写字母数量,则将所有字母统一为小写,例如 maTRIx 应改为 matrix。
输入格式
第一行包含整数 T,表示共有 T 组测试数据。每组数据占一行,包含一个由大小写字母构成的字符串 s。(注意,字符串可能只包含小写字母或只包含大写字母)。
输出格式
每组数据输出一行结果,一个字符串,表示统一大小写后的单词。
数据范围
本题共两个测试点。
小测试点,如样例所示。
大测试点满足:1 ≤ T ≤ 30,字符串长度取值范围 [1,100]。
输入样例:
3
HoUse
ViP
maTRIx
输出样例:
house
VIP
matrix
来源:https://www.acwing.com/problem/content/3958/
2. 思路分析:
分析题目可以知道模拟整个过程即可。
3. 代码如下:
class Solution:
def process(self):
T = int(input())
for i in range(T):
s = input()
# count统计小写字母的数量
count = 0
for c in s:
if "a" <= c <= "z":
count += 1
res = ""
if count >= len(s) - count:
for c in s:
if "A" <= c <= "Z":
res += chr(ord(c) - ord("A") + ord("a"))
else:
res += c
print(res)
else:
for c in s:
if "a" <= c <= "z":
res += chr(ord(c) - ord("a") + ord("A"))
else:
res += c
print(res)
if __name__ == '__main__':
Solution().process()