我有以下任务
-
确定尚未写出的最长字符串
-
在没有写出的最长字符串中,按字典顺序选择最小的字符串
-
我有以下代码
strings = ['5', 'z', 'yy', 'y', 'zyz', 'zzz']
strings = [x for x in strings if not x.isdigit()]
result = []
for _ in range(0, len(strings)):
value = max(strings, key=len)
result.append(strings.pop(strings.index(value)))
print(result)
input array ['5', 'z', 'yy', 'y', 'zyz', 'zzz'] -> output ['zyz', 'zzz', 'yy', 'y', 'z']
['zyz', 'zzz', 'yy', 'z', 'y']
另一个正确的例子
input array [5, dbccdacaac, abcddbbaab, ccaaaccdab, cdaadbcbdc, dcabdabccb] -> output [abcddbbaab, ccaaaccdab, cdaadbcbdc, dbccdacaac, dcabdabccb]