Find The Merge Point Of Two Lists
September 2, 2016
Given two lists, the merge point is the point after which the two lists are identical and can be merged. For instance, given the lists (a b c x y z) and (g h i x y z), the merge point is at (x y z). If the last items in the two lists differ, the merge point is null; if the two lists are the same, the entire list is the merge point.
Your task is to write a program that finds the merge point of two lists; it should return the unique prefix of the first list, the unique prefix of the second list, and the common suffix of the two lists. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
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]);
}