Home > other >  Python3 sorted scheduling problems, no CMP
Python3 sorted scheduling problems, no CMP

Time:10-11

Chanced upon a Leetcode topic Largest Number:
Given a list of non negative integers, arrange them to that they form the largest number.
For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.
Note: The result may be very large, so you need to return a string home an integer.
It means that there is a are positive integers in the list, how can the combination of the largest digital, because the Numbers may be large, direct output string to represent Numbers,

The inside Python2 of CMP is easy to realize with Sorted, but due to cancelled the built-in objects in Python3 __cmp__ method, so Sorted function of afferent comparison function parameters of CMP also cancelled,

So in Python3 have what good way to solve the problem?

I wrote a, but feels more ugly, is in the following, you can't see the

& gt;> A_list=[3, 30, 34, 5, 9]
> Print ('. Join (map (STR, sorted (a_list, key=lambda x: STR (x) ljust (Max (map (lambda I: len (STR) (I), a_list)), '9'), reverse=True))))
9533430
>

CodePudding user response:

 
> [3, 30, 34, 5, 9]
> Print ('. Join (map (STR, sorted (a_list, key=lambda x: STR (x) ljust (Max (map (lambda I: len (STR) (I), a_list)), STR (x) [0]), reverse=True))))
9534330

CodePudding user response:

Checked the Python3 manual found a more elegant way
 
The from functools import cmp_to_key
Cmp2key=cmp_to_key (lambda x, y: int (y + x) - int (x + y))
Print ('. Join (sorted (map (STR, a_list), key=cmp2key)))

CodePudding user response:

 import functools as ft 
X=[3, 30, 34, 5 16/32-bit, 9]
E=ft. Reduce (lambda x, y, x + y, sorted (map (STR, x), reverse=1))
Print (e)

CodePudding user response:

Oh, seems to want to consider the problem of 30 with 3

CodePudding user response:

This thing is can't judge bits and ten, if there is a single digit has ten roll together, that's all confusion
  • Related