Function to merge sorted lists
Ignacio Vazquez-Abrams
ignacio at openservices.net
Fri Aug 31 02:27:40 EDT 2001
On Fri, 31 Aug 2001, Edward C. Jones wrote:
> Ignacio Vazquez-Abrams wrote:
> > On Thu, 30 Aug 2001, Edward C. Jones wrote:
> >
> >
> >>Has anyone written in C a merge function for sorted Python sequences?
>
> > Sounds interesting, but... and don't take this the wrong way... I can barely
> > understand what you're trying to say. An example might help.
>
> Here is a Python program that does the same thing:
>
> def merge(seq1, seq2):
> n1 = len(seq1)
> n2 = len(seq2)
> i = j = 0
> merged = []
> while i < n1 and j < n2:
> if seq1[i] < seq2[j]:
> merged.append(seq1[i])
> i += 1
> else:
> merged.append(seq2[j])
> j += 1
> if i == n1:
> tail1 = []
> tail2 = seq2[j:]
> else:
> tail2 = []
> tail1 = seq1[i:]
> return merged, tail1, tail2
Here, I'm going to throw this code into the public domain.
It works real easy under Linux (you're on your own for other platforms):
1) Edit Makefile and change the two options to suit your favourite version of
Python (you won't need to ask).
2) 'make; make install'
Then you can 'import ecj' and then use ecj.merge().
It only takes lists of ints, but it's a start.
Worked for me under 1.5.2 and 2.1.1.
--
Ignacio Vazquez-Abrams <ignacio at openservices.net>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ecjmodule-0.0.tar.gz
Type: application/x-gzip
Size: 1181 bytes
Desc:
URL: <http://mail.python.org/pipermail/python-list/attachments/20010831/dbd8cf2b/attachment.bin>
More information about the Python-list
mailing list