如何从节目中脱颖而出

2024-10-01 09:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:
给定一个字符串S,查找最大的字母字符,其大写和小写均显示在S中。应返回大写字符。例如,对于S=“admeDCAB”,返回“D”。如果没有此类字符,请返回“否”

s=input()
l=[0]*26
u=[0]*26
ln=len(s)
for i in range(ln):
    if(s[i].islower()):
        l[int(ord(s[i])-ord(('a')))]=1
    else:
        u[int(ord(s[i])-ord(('A')))]=1
for i in range(25,-1,-1):
    if(l[i]==1 and u[i]==1):
        print(chr(i+ord('A')))
        
print(NO)

打印完成后如何退出(chr(i+ord('A'))打印


Tags: 字符串inforif字母range字符int
1条回答
网友
1楼 · 发布于 2024-10-01 09:36:30

要回答你的实际问题

Given a string S, find the largest alphabetic character, whose both uppercase and lowercase appear in S. The uppercase character should be returned. For example, for S = "admeDCAB", return "D". If there is no such character, return "NO".

以一种类似Python的方式

def f(s):
    s = set(s)
    for c in 'ZYXWVUTSRQPONMLKJIHGFEDCBA':
        if c in s and c.lower() in s:
            return c
    return 'NO'

相关问题 更多 >