拉了盏灯
发表于 2020-2-16 22:45:42
def fc(string):
for each in sorted(set(string.upper()))[::-1]:
if each in string and each.lower() in string:
return print(each)
print('~')
好久没发帖了。
阴阳神万物主
发表于 2020-2-16 23:00:56
本帖最后由 阴阳神万物主 于 2020-2-17 20:55 编辑
小生也是姗姗来迟啊。
def solve(s:str)->str:
a = set(list(s))
b = set(list(s.swapcase()))
c = a.difference(a.difference(b))
res = list(c)
if res:res.sort()
else:return '~'
return res[-1].upper()
if __name__ == '__main__':
print('示例1 输出:',repr(solve("aAbBcD")))
print('示例2 输出:',repr(solve("looGVSSPbR")))
减少一行代码,过程没有区别。
def solve(s:str)->str:
a = set(list(s))
b = set(list(s.swapcase()))
c = a.difference(a.difference(b))
res = sorted(c)
if res:return res[-1].upper()
else:return '~'
if __name__ == '__main__':
print('示例1 输出:',repr(solve("aAbBcD")))
print('示例2 输出:',repr(solve("looGVSSPbR")))
546623863
发表于 2020-2-16 23:08:36
def fun333(x : str):
s = set(x)
s = list(s)
s.sort()
if(s > 'z'):
return "~"
while s != []:
temp = s.pop()
if(temp < 'a'):
return "~"
temp = temp.upper()
if(temp in s):
return temp
return "~"
fan1993423
发表于 2020-2-16 23:51:46
def fun333(s):
ns=''.join(sorted(set(s+'^'),reverse=True)).split('^')
if not ns:return '~'
for i in ns:
if i.upper() in s:
return i.upper()
return '~'
wanting-for
发表于 2020-2-16 23:56:47
本帖最后由 wanting-for 于 2020-2-16 23:58 编辑
set函数去重
def solve_333(s: str):
s = set(list(s))
s = sorted(s,reverse = True)
for x in s:
if x.isupper():
return '~'
elif x.upper() in s:
return x.upper()
return '~'
字典去重
def solve_333(s: str):
s,d = sorted(list(s),reverse = True), {}
for x in s:
if not d.get(x):
if x.isupper():
return '~'
elif x.upper() in s:
return x.upper()
else:
d = ''
return '~'
题目很清晰,主要是如何提升效率,如果大规模数据,毕竟重复量很大,
所以用了字典,和set,来进行去重
麻烦楼主测试一下,这俩时间分别是多少
谢谢啦{:5_109:}
ouyunfu
发表于 2020-2-17 00:28:44
def f333(s:str)->str:
s1 = sorted(list(set(s)),reverse=True)
for i in s1:
if i.islower() and i.upper() in s1:
return i.upper()
return "~"
print(f333('aAbBcD'))
print(f333('looGVSSPbR'))
print(f333('"qsgrqngjupumrwkrfexznulmfyvniptzgatcqhtlrdrljhuvhbknlggxfwblkigcikgufovjtrpbzwpzmmnjkmmgakmohwgjtptzozqlprcmusckbigdbwswszauybezwccjmbqjmrrgodytzrhhhpzfcrqnqnkeighyynhrtvcsyiynrfogwkqcsjuwkmipwpuonvpvbcningpoxbbllbnftcjtsmpbpfddglqgfzpwsbboueyuftxewkdtyvvlsjcqwethrlesomuxntonpxbeabzsjcxpcknemneoqyvlwzpkjtftgjjdqinsnyvhbhtzpywxqwzilbkihaeqrparegqkjqkjekezdicltsuvztjbqoocrwgnbwmcomesxbunhmsyxnpippajvybcdppofmeincxhikpvmyoyeeoftctmhvteitflfmynrncngzfthxduthqzkjkhqdqjykxltavdyktyoiwyzymfjonxmyzyhwsvgxbtmifkbuhlelmefxtcjrubripfqjqcdbconqupshsebsgzqtzrzmmnfjcjhigdyetnagggxrbvfpygczegedkftiqbzhvcatqjcifczqorlncdgfibjglaxazlvcxjcclqbginbwvafxaxdbbgznlerbnrrizpzesgegalodsqqggammpvnqxroqqsaudmpotfvgjttndzryaykjoeesfoisaqcsgmjosyjykposxixvuwulclfsvledzpqjgcrcvszhravwtsjkcodokgyvbvrxmdqqgzefpdkyokbsfpdmyadugqkzjovqjxdbofqxkppffpolyfynxnbltqishyfopsihxydkaqvvymncseymdyzvinvbnazjjstetcdentulfnbndnkcwtaizpmfxiojtnpdejfmpkttpsleflgzwaaquvmnljemzfuzobrtwpoesaygwxtyhbutivunopyckftuxybhapravbgosmiqncjtel"'))
yexing
发表于 2020-2-17 00:41:18
本帖最后由 yexing 于 2020-2-17 00:50 编辑
def func333(s:str) -> str:
string = []
for i in s:
string.append(i)
string.sort(reverse=True)
for i in string:
if i.upper() in s and i.lower() in s:
st = i
flag = 1
break
else:
flag = 0
return (st.upper() if flag==1 else "~")
zltzlt
发表于 2020-2-17 08:55:24
kinkon 发表于 2020-2-16 22:16
59 ms
zltzlt
发表于 2020-2-17 08:55:46
840613937 发表于 2020-2-16 22:38
150 ms,还可以改进哦
zltzlt
发表于 2020-2-17 08:57:05
William4869 发表于 2020-2-16 22:38
来迟了
输入以下数据返回的是 None
"qsgrqngjupumrwkrfexznulmfyvniptzgatcqhtlrdrljhuvhbknlggxfwblkigcikgufovjtrpbzwpzmmnjkmmgakmohwgjtptzozqlprcmusckbigdbwswszauybezwccjmbqjmrrgodytzrhhhpzfcrqnqnkeighyynhrtvcsyiynrfogwkqcsjuwkmipwpuonvpvbcningpoxbbllbnftcjtsmpbpfddglqgfzpwsbboueyuftxewkdtyvvlsjcqwethrlesomuxntonpxbeabzsjcxpcknemneoqyvlwzpkjtftgjjdqinsnyvhbhtzpywxqwzilbkihaeqrparegqkjqkjekezdicltsuvztjbqoocrwgnbwmcomesxbunhmsyxnpippajvybcdppofmeincxhikpvmyoyeeoftctmhvteitflfmynrncngzfthxduthqzkjkhqdqjykxltavdyktyoiwyzymfjonxmyzyhwsvgxbtmifkbuhlelmefxtcjrubripfqjqcdbconqupshsebsgzqtzrzmmnfjcjhigdyetnagggxrbvfpygczegedkftiqbzhvcatqjcifczqorlncdgfibjglaxazlvcxjcclqbginbwvafxaxdbbgznlerbnrrizpzesgegalodsqqggammpvnqxroqqsaudmpotfvgjttndzryaykjoeesfoisaqcsgmjosyjykposxixvuwulclfsvledzpqjgcrcvszhravwtsjkcodokgyvbvrxmdqqgzefpdkyokbsfpdmyadugqkzjovqjxdbofqxkppffpolyfynxnbltqishyfopsihxydkaqvvymncseymdyzvinvbnazjjstetcdentulfnbndnkcwtaizpmfxiojtnpdejfmpkttpsleflgzwaaquvmnljemzfuzobrtwpoesaygwxtyhbutivunopyckftuxybhapravbgosmiqncjtel"
zltzlt
发表于 2020-2-17 08:57:27
拉了盏灯 发表于 2020-2-16 22:45
好久没发帖了。
56 ms
zltzlt
发表于 2020-2-17 08:58:11
阴阳神万物主 发表于 2020-2-16 23:00
小生也是姗姗来迟啊。
55 ms
zltzlt
发表于 2020-2-17 08:58:37
546623863 发表于 2020-2-16 23:08
输入空字符串 "" 报错
zltzlt
发表于 2020-2-17 08:59:04
fan1993423 发表于 2020-2-16 23:51
58 ms
zltzlt
发表于 2020-2-17 09:00:34
wanting-for 发表于 2020-2-16 23:56
set函数去重
字典去重
不用说,当然是集合去重更快~
zltzlt
发表于 2020-2-17 09:00:56
ouyunfu 发表于 2020-2-17 00:28
def f333(s:str)->str:
s1 = sorted(list(set(s)),reverse=True)
for i in s1:
发代码请用 "<>"
zltzlt
发表于 2020-2-17 09:01:52
ouyunfu 发表于 2020-2-17 00:28
def f333(s:str)->str:
s1 = sorted(list(set(s)),reverse=True)
for i in s1:
62 ms
William4869
发表于 2020-2-17 09:02:01
zltzlt 发表于 2020-2-17 08:57
输入以下数据返回的是 None
改了
zltzlt
发表于 2020-2-17 09:02:34
William4869 发表于 2020-2-16 22:38
来迟了
可以了,80 ms
zltzlt
发表于 2020-2-17 09:03:13
yexing 发表于 2020-2-17 00:41
输入空字符串 "" 报错