Code golf: combining multiple sorted lists into a single sorted list

前端 未结 26 1838
余生分开走
余生分开走 2020-12-29 12:42

Implement an algorithm to merge an arbitrary number of sorted lists into one sorted list. The aim is to create the smallest working programme, in whatever language you like.

相关标签:
26条回答
  • 2020-12-29 13:12

    resubmitted

    Python - 74 chars (counting whitespace and newlines)

    def m(i):
     y=[];x=sum(i,[])
     while x:n=min(x);y+=[n];x.remove(n)
     return y
    

    i is input as list of lists

    Usage:

    >>> m([[1,5],[6,3]])
    [1, 3, 5, 6]
    
    0 讨论(0)
  • 2020-12-29 13:12

    Haskell like (158, but more than 24 spaces could be removed.):

    mm = foldl1 m where
      m [] b = b
      m a [] = a
      m (a:as) (b:bs)
       | a <= b = a : m as (b:bs)
       | true   = b : m (a:as) bs
    
    0 讨论(0)
提交回复
热议问题