Python solve array to re-combine to find the minimum value (Youku)

Source: Internet
Author: User

Title: the integer array containing n elements, the n elements are re-assembled to find the smallest number, such as (321,3,32,) the minimum number is 321323

Topic Analysis:

Fills all the elements in the array into equal numbers, fills the number with the last digit, as shown in this case, after the padding is {321,333,322}, and then sorts by size,

After sorting, the numbers are removed and the number after stitching is the minimum number, which is {321,322,333} after sorting, minus the padding number {321,32,3}, which is 321323

Python implementation

defMergeminvalue (LST):#generating a list of stringsLST =list (map (str,lst))#get the longest number lengthm = Len (max (lst,key=len)) #get a new list based on the original integerNewList = [(i,i+i[-1]* (M-len (i))) forIinchLST]#sort the padded numeric charactersNewlist.sort (key=LambdaItem:item[1])    #stitching the original numbersresult ="'. Join ((item[0) forIteminchnewlist)) returnInt (Result)

Test examples:

LST = [321,3,31,38,93]print(Mergeminvalue (LST))

Test output

3132133893

Data reference: Python Cottage

Python solve array to re-combine to find the minimum value (Youku)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.