Find The Merge Point Of Two Lists
September 2, 2016
I took this exercise from a blog on C programming, and the solution was a fearsome recital of pointer manipulation, with the sense of the pointers reversed as they marched down the lists in parallel (the author gave some reason for not marching down the lists one at a time, but it didn’t make sense to me), and auxiliary pointers to something (I never figured that part out, but I’ll admit I didn’t try very hard). It reminded me why I don’t like C.
The solution is very simple. Reverse both lists, then scan them backwards from the end until they differ:
(define (merge-point xs ys) (let loop ((xs (reverse xs)) (ys (reverse ys)) (zs (list))) (cond ((or (null? xs) (null? ys)) (values (reverse xs) (reverse ys) zs)) ((equal? (car xs) (car ys)) (loop (cdr xs) (cdr ys) (cons (car xs) zs))) (else (values (reverse xs) (reverse ys) zs)))))
Since the consequents of the first and third cond
clauses are the same, you could combine them if you want. Here are some examples:
> (merge-point '(1 2 3 7 8 9) '(4 5 6 7 8 9)) (1 2 3) (4 5 6) (7 8 9) > (merge-point '(1 2 3) '(4 5 6)) (1 2 3) (4 5 6) () > (merge-point '(3 4 5) '(1 2 3 4 5)) () (1 2) (3 4 5) > (merge-point '(1 2 3) '(1 2 3)) () () (1 2 3)
You can run the program at http://ideone.com/9mf8zf.
Here’s a solution in Java.
Output:
Here is one for Julia (0.000088 sec to execute, applying a total of 64 memory allocations).
function main{T = ny
x, y = x_, y_
else
x, y = y_, x_
nx, ny = ny, nx
end
dn = ny – nx # negative difference of lengths
for i = (nx – ny + 1):(nx – 1)
j = dn + i
if all(x[i:end] .== y[j:end])
return x[1:(i-1)], y[1:(j-1)], x[i:end]
end
end
return x_, y_, Array(T, 0)
end
This code is optimized for unequal lists, although it works quite fast on equal ones too. As usual, no external libraries were used.
For some reason the code didn’t paste properly. Here it is again, this time on a .png file: https://app.box.com/s/0fhupoyexwx26dv6trb7ud8mgs30ntz9
In Common Lisp :
A few tests:
An error in my last comment, the code is :
In Python3.
(defun merge-point (lst1 lst2)
(do ((l1 (reverse lst1) (cdr l1))
(l2 (reverse lst2) (cdr l2))
(merge-point ‘()))
((or (and (null l1) (null l2)) (not (eq (car l1) (car l2))))
(values (reverse l1) (reverse l2) merge-point))
(push (car l1) merge-point)))
package test;
public class MergePoint {
public static int mergePoint(int a[],int b[]){
int aMergePoint=a.length;
int bMergePoint=b.length;
int alength=a.length;
int blength=b.length;
for(int i=alength-1,j=blength-1;i>=0 && j>=0 && a[i]==b[j]; i–,j–){
aMergePoint–;bMergePoint–;
}
System.out.println(“\n aMergePoint”+ aMergePoint);
System.out.println(“Suffix”);
for(int i=aMergePoint;i<a.length;i++){
System.out.print(a[i]);
}
System.out.println("\nAPrefix");
for(int i=0;i<aMergePoint;i++){
System.out.print(a[i]);
}
System.out.println("\nBPrefix");
for(int i=0;i<bMergePoint;i++){
System.out.print(b[i]);
}
for(int i=aMergePoint;i<aMergePoint;i++){
System.out.print(a[i]);
}
return aMergePoint ;
}
public static void main(String args[]){
int a[]={1,2,3,4,5,6,7,8,9};
int b[]={5,6,7,4,5,6,7,8,9};
mergePoint(a,b);
}
}
@Jérôme. Great job implementing this in Lisp; not many people could handle that! This reminds me of my university days when I scribbled a few Lisp scripts myself. I’m quite curious about the performance of this language for this task, so any information about that would be very much appreciated.
In Common Lisp, using the built-in functions MISMATCH and SUBSEQ:
in PHP
function find_merge($A,$B) {
$a=array_values($A);
$b=array_values($B);
$l=sizeof($A);
$merge=true;
for($i=0; $i<$l ;$i++) {
if($a[$i]==$b[$i]) {
if($merge) {
$mp=$i;
$merge=false;}
}else {$merge=true;
$mp=$l;}
}
$bb=array_slice($b,0,$mp);
$aa=array_slice($a,0,$mp);
$am=array_slice($a,$mp);
return print_r([$aa,$bb,$am]);
}