Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input: ((1, 4, 7), (2, 5, 8), (3, 6, 9))
- output: (1, 2, 3, 4, 5, 6, 7, 8, 9)
- input: ((1, 10), (), (2, 5, 6, 7))
- output: (1, 2, 5, 6, 7, 10)
- sorted(sum(lists,[])) # cheating: out of bounds!
- (defun multi-merge (&rest lists)
- (reduce (lambda (a b) (merge 'list a b #'<)) lists))
- (defun multi-merge (&rest lists)
- (reduce (lambda (a b) (merge 'list a b #'<)) lists))
- (defun m(&rest l)(reduce(lambda(a b)(merge 'list a b #'<))l))
- let f=List.fold_left(List.merge compare)[]
- def m(i)
- a=[]
- i.each{|s|s.each{|n|a.insert((a.index(a.select{|j|j>n}.last)||-1)+1,n)}}
- a.reverse
- end
- def sorted_join(numbers)
- sorted_numbers=[]
- numbers.each do |sub_numbers|
- sub_numbers.each do |number|
- bigger_than_me = sorted_numbers.select { |i| i > number }
- if bigger_than_me.last
- pos = sorted_numbers.index(bigger_than_me.last) + 1
- else
- pos = 0
- end
- sorted_numbers.insert(pos, number)
- end
- end
- sorted_numbers.reverse
- end
- a = [[1, 4, 7], [2, 4, 8], [3, 6, 9]]
- n = 50000
- Benchmark.bm do |b|
- b.report { n.times { m(a) } }
- b.report { n.times { a.flatten.sort } }
- end
- user system total real
- 2.940000 0.380000 3.320000 ( 7.573263)
- 0.380000 0.000000 0.380000 ( 0.892291)
- def m(i):
- y=[];x=sum(i,[])
- while x:n=min(x);y+=[n];x.remove(n)
- return y
- >>> m([[1,5],[6,3]])
- [1, 3, 5, 6]
- def m(c,l):
- try:
- c += [l[min((m[0], i) for i,m in enumerate(l) if m)[1]].pop(0)]
- return m(c,l)
- except:
- return c
- # called as:
- >>> m([], [[1,4], [2,6], [3,5]])
- [1, 2, 3, 4, 5, 6]
- max_ = 2**31-1
- def f(N):
- L = random.sample(xrange(max_), n)
- L.sort()
- return L
- f.__name__ = "sorted_random_%d" % max_
- $ git clone git://gist.github.com/51074.git
- m l|all null l=[]
- |True=x:(m$a++(xs:b))
- where
- n=filter(not.null)l
- (_,min)=minimum$zip(map head n)[0..]
- (a,((x:xs):b))=splitAt min n
- L[99][99];N;n[99];m[99];i;I;b=0;main(char t){while(scanf("%d%c",L[i]+I,&t)+1){++
- I;if(t==10){n[i++]=I;I=0;}}if(I)n[i++] = I;N=i;while(b+1){b=-1;for(i=0;i<N;++i){
- I=m[i];if(I-n[i])if(b<0||L[i][I]<L[b][m[b]])b=i;}if(b<0)break;printf("%d ",L[b][
- m[b]]);++m[b];}puts("");}
- 1 4 7
- 2 5 8
- 3 6 9
- (EOF)
- let p l=
- let f a b=List.filter(a b) in
- let rec s=function[]->[]|x::y->s(f(>)x y)@[x]@s(f(<=)x y) in
- [for a in l->>a]|>s
- let golf l=
- // filters my list with a specified filter operator
- // uses built-in F# function
- // ('a -> 'b -> bool) -> 'a -> ('b list -> 'b list)
- let filter a b = List.filter(a b)
- // quicksort algorithm
- // ('a list -> 'a list)
- let rec qsort =function
- | []->[]
- | x :: y -> qsort ( filter (>) x y) @ [x] @ qsort ( filter (<=) x y)
- // flattens list
- [for a in l ->> a ] |> qsort
- function merge(a) {
- var r=[], p;
- while(a.length>0) {
- for (var i=0,j=0; i<a.length && p!=a[j][0]; i++)
- if (a[i][0]<a[j][0])
- j = i;
- r.push(p = a[j].shift());
- if (!a[j].length)
- a.splice(j, 1);
- }
- return r;
- }
- var arr = [[1, 4, 7], [2, 5, 8], [3, 6, 9]];
- alert(merge(arr));
- Dim m1 As List(Of Integer) = New List(Of Integer)
- Dim m2 As List(Of Integer) = New List(Of Integer)
- Dim m3 As List(Of Integer) = New List(Of Integer)
- Dim m4 As List(Of Integer) = New List(Of Integer)
- m1.Add(1)
- m1.Add(2)
- m1.Add(3)
- m2.Add(4)
- m2.Add(5)
- m2.Add(6)
- m3.Add(7)
- m3.Add(8)
- m3.Add(9)
- Dim m5 As List(Of List(Of Integer)) = New List(Of List(Of Integer))
- m5.Add(m1)
- m5.Add(m2)
- m5.Add(m3)
- While m5.Count > 0
- Dim idx As Integer = 0
- Dim min As Integer = Integer.MaxValue
- For k As Integer = 0 To m5.Count - 1
- If m5(k)(0) < min Then min = m5(k)(0) : idx = k
- Next
- m4.Add(min) : m5(idx).RemoveAt(0)
- If m5(idx).Count = 0 Then m5.RemoveAt(idx)
- End While
- Private Function Comp(ByVal l1 As List(Of Integer), ByVal l2 As List(Of Integer)) As Integer
- Return l1(0).CompareTo(l2(0))
- End Function
- .
- .
- .
- While m5.Count > 0
- m5.Sort(AddressOf Comp)
- m4.Add(m5(0)(0)) : m5(0).RemoveAt(0)
- If m5(0).Count = 0 Then m5.RemoveAt(0)
- End While
- Dim rand As New Random
- Dim m1 As List(Of Integer) = New List(Of Integer)
- Dim m2 As List(Of Integer) = New List(Of Integer)
- Dim m3 As List(Of Integer) = New List(Of Integer)
- Dim m4 As List(Of Integer) = New List(Of Integer)
- Dim m5 As List(Of List(Of Integer)) = New List(Of List(Of Integer))
- m5.Add(m1)
- m5.Add(m2)
- m5.Add(m3)
- For Each d As List(Of Integer) In m5
- For i As Integer = 0 To 100000
- d.Add(rand.Next())
- Next
- d.Sort()
- Next
- Dim sw As New Stopwatch
- sw.Start()
- While m5.Count > 0
- Dim idx As Integer = 0
- Dim min As Integer = Integer.MaxValue
- For k As Integer = 0 To m5.Count - 1
- If m5(k)(0) < min Then min = m5(k)(0) : idx = k
- Next
- m4.Add(min) : m5(idx).RemoveAt(0)
- If m5(idx).Count = 0 Then m5.RemoveAt(idx)
- End While
- sw.Stop()
- 'Dim sw As New Stopwatch
- 'sw.Start()
- 'While m5.Count > 0
- ' m5.Sort(AddressOf Comp)
- ' m4.Add(m5(0)(0)) : m5(0).RemoveAt(0)
- ' If m5(0).Count = 0 Then m5.RemoveAt(0)
- 'End While
- 'sw.Stop()
- Console.WriteLine(sw.Elapsed)
- Console.ReadLine()
- def merge_sort(arrs)
- o = Array.new
- arrs.each do |a|
- o = o | a
- end
- o.sort!
- end
- arrs = [ [ 90, 4, -2 ], [ 5, 6, -100 ], [ 5, 7, 2 ] ]
- merge_sort(arrs)
- static void f(params int[][] b)
- {
- var l = new List<int>();
- foreach(var a in b)l.AddRange(a);
- l.OrderBy(i=>i).ToList().ForEach(Console.WriteLine);
- }
- static void Main()
- {
- f(new int[] { 1, 4, 7 },
- new int[] { 2, 5, 8 },
- new int[] { 3, 6, 9 });
- }
- let f x=List.sort(List.concat x)
- let f x=List.sort(Seq.toList(seq{for l in x do yield!l}))
- import heapq
- def m(i):
- return list(heapq.merge(*i))
- print m(((1, 4, 7), (2, 5, 8), (3, 6, 9)))
- import heapq
- def m(i): return list(heapq.merge(*i))
- sub a{sort map{@$_}@_}
- my @sorted = a([1, 2, 3], [5, 6, 89], [13, -1, 3]);
- print "@sorted" # prints -1, 1, 1, 2, 3, 3, 5, 6, 89
- l1.merge(l2); // Removes the elements from the argument list, inserts
- // them into the target list, and orders the new, combined
- // set of elements in ascending order or in some other
- // specified order.
- sort -m file1 file2 file3 ...
- # This merges two lists together
- m(){
- [[ -z $1 ]] && echo $2 && return;
- [[ -z $2 ]] && echo $1 && return;
- A=($1); B=($2);
- if (( ${A[0]} > ${B[0]} ));then
- echo -n ${B[0]} ;
- unset B[0];
- else
- echo -n ${A[0]} ;
- unset A[0];
- fi;
- m "${A[*]}" "${B[*]}";
- }
- # This merges multiple lists
- M(){
- A=$1;
- shift;
- for x in $@; do
- A=`m "$A" "$x"`
- done
- echo $A
- }
- $ M '1 4 7' '2 5 8' '3 6 9'
- 1 2 3 4 5 6 7 8 9
- Function s(i)
- s=New List(Of Byte)
- Dim m,c
- Dim N=Nothing
- Do
- m=N
- For Each l In i:
- If l.Count AndAlso(l(0)<m Or m=N)Then m=l(0):c=l
- Next
- If m<>N Then s.Add(m):c.Remove(m)
- Loop Until m=N
- End Function
- def f(l):
- n=[]
- for t in l:
- for i in t: n+=[t]
- s=[]
- while n: s.+=[min(n)]; n.remove(min(n))
- return s
- Sub Main()
- f(New Int32() {1, 4, 7}, _
- New Int32() {2, 5, 8}, _
- New Int32() {3, 6, 9})
- End Sub
- Sub f(ByVal ParamArray b As Int32()())
- Dim l = New List(Of Int32)
- For Each a In b
- l.AddRange(a)
- Next
- For Each a In l.OrderBy(Function(i) i)
- Console.WriteLine(a)
- Next
- End Sub
- from heapq import *
- def m(l):
- r=[]
- h=[]
- for x in l:
- if x:
- heappush(h, (x[0], x[1:]))
- while h:
- e,f=heappop(h)
- r.append(e)
- if f:
- heappush(h, (f.pop(0),f))
- return r
Add Comment
Please, Sign In to add comment