[英]The largest Integer from n integers in python
The program must accept a integer and the list of integers then the output should be the largest possible integer from the given array of integers程序必须接受 integer 和整数列表,然后 output 应该是给定整数数组中可能的最大 integer
from itertools import permutations as p
a=int(input())
print("".join(sorted(list(set(p(list(input().strip().split(" "))))))[-1]))
This is my code but it is not working since it takes too much of time to execute这是我的代码,但它不起作用,因为它需要太多时间来执行
Execution Time Limit: 500ms
执行时间限制:500ms
As others said, sorting is the only key here.
正如其他人所说,排序是这里唯一的关键。 As a oneliner taking standard input:
作为一个采用标准输入的单线器:
print("".join(sorted(input().split(), reverse=True)))
This works in some test cases, but fails miserably with the case 98 987
.这在某些测试用例中有效,但在
98 987
案例中惨遭失败。
def compare(a, b): for i,j in zip_longest(a, b): if i is not None and j is not None: if i<j: return -1 elif i>j: return 1 if i is None: return int(a[0])-int(j) if j is None: return int(b[0])-int(i) return 0
Testing
测试
>>> a = ['24', '26', '28', '987', '556', '214', '398', '476', '542', '192', '878', '566', '744', '232', '456', '98', '2', '4', '76', '78'] >>> print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare))))) 98987878787674456655654247645643982826242322214192 >>> 98987878787674456655654247645643982826242322214192 == 98987878787674456655654247645643982826242322214192 True
So it seems to work.
所以它似乎工作。
Explanation
解释
zip_longest
is the same aszip
, but it pads for unequal lengths.zip_longest
与zip
相同,但它填充的长度不相等。 Examplezip_longest("1", "12")
gives[("1", "1"), (None, "2")]
.示例
zip_longest("1", "12")
给出[("1", "1"), (None, "2")]
。As long as none of the elements in the tuple are
None
everything is supposed to work normally:只要元组中的所有元素都不是
None
一切都应该正常工作:
- If
i
is smaller thanj
, then the number containingi
-- herea
-- must be smaller than the number containingj
-- hereb
-- so we return-1
.如果
i
小于j
,那么包含i
的数字——这里a
a——必须小于包含j
的数字——这里是b
所以我们返回-1
。- The other way around, it's the same.
反过来,也是一样的。
So far, this is the same as the alphanumeric ordering.
到目前为止,这与字母数字排序相同。 But what if one number is
None
?但是,如果一个数字是
None
怎么办? Example: Takea = 987
andb = 98
.示例:取
a = 987
和b = 98
。 At one point,i
is7
andj
isNone
.在某一时刻,
i
是7
而j
是None
。 In alphanumeric ordering,7
would be larger thanNone
,a>b
.在字母数字排序中,
7
将大于None
,a>b
。 But here, we have to take into account, that the numbers will be chained, so we have to in fact check, if7
is greater than the first digit of the other number, which is9
.但是在这里,我们必须考虑到这些数字会被链接起来,所以我们实际上必须检查
7
是否大于另一个数字的第一个数字,即9
。7
is actually smaller, sob
comes first.7
实际上更小,所以b
先出现。If none of the conditions are met, the numbers must be equal and
0
is returned.如果不满足任何条件,则数字必须相等并返回
0
。
This worked in most test cases, but fails miserably with 98 989
.这在大多数测试用例中都有效,但在
98 989
中惨遭失败。
def compare(a, b):
x = a+b
y = b+a
if x>y:
return 1
elif x<y:
return -1
else:
return 0
In [96]: a = ["98", "987"]
In [97]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))
98987
In [98]: a = ["989", "98"]
In [99]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))
98998
In [100]: a = ['24', '26', '28', '987', '556', '214', '398', '476', '542', '192', '878', '566', '744', '232', '456',
...: '98', '2', '4', '76', '78']
In [101]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))
98987878787674456655654247645643982826242322214192
They all work他们都工作
The very first approach was too easy, the second was overcomplex.第一种方法太简单了,第二种方法过于复杂。 This one is the midway and actually very straight forward.
这是中途,实际上非常直截了当。 Combine each pair in both possible way.
以两种可能的方式组合每一对。 Which combination yields the bigger number?
哪个组合产生更大的数字? That will dictate the sorting.
这将决定排序。
I hope everything is clear now and that this withstands any test case I maybe even never thought of.我希望现在一切都清楚了,并且这能经受住我可能从未想过的任何测试用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.