What is the best way to get the longest common prefix (sub-array that starts from index 0 of the original) and suffix (sub-array that ends with index -1 of the original) of two arrays? For example, given two arrays:
[:foo, 1, :foo, 0, nil, :bar, "baz", false]
[:foo, 1, :foo, 0, true, :bar, false]
the longest common prefix of these arrays is:
[:foo, 1, :foo, 0]
and the longest common suffix of these arrays is:
[false]
When the elements at index 0/-1 differ in the original arrays, then the common prefix/suffix should be an empty array.
One possible solution:
a1 = [:foo, 1, 0, nil, :bar, "baz", false]
a2 = [:foo, 1, 0, true, :bar, false]
a1.zip(a2).take_while { |x, y| x == y }.map(&:first)
#=> [:foo, 1, 0]
Reversing the input arrays and the output array finds a common suffix:
a1.reverse.zip(a2.reverse).take_while { |x, y| x == y }.map(&:first).reverse
#=> [false]
Edge case: zip
fills "argument" arrays with nil
values:
a1 = [true, nil, nil]
a2 = [true]
a1.zip(a2).take_while { |x, y| x == y }.map(&:first)
#=> [true, nil, nil]
This can be avoided by truncating the initial array to the second array's length:
a1[0...a2.size].zip(a2).take_while { |x, y| x == y }.map(&:first)
Another solution without edge case:
a1 = [:foo, 1, 0, nil, :bar, "baz", false]
a2 = [:foo, 1, 0, true, :bar, false]
a1.take_while.with_index {|v,i| a2.size > i && v == a2[i] }
EDIT: Improved performance
class Array
def common_prefix_with(other)
other_size = other.size
take_while.with_index {|v,i| other_size > i && v == other[i] }
end
end
a1.common_prefix_with a2
Three solutions: brutish (#3, initial answer), better (#2, first edit) and best (#1, variant of @Stefan's answer, second edit).
a = [:foo, 1, :foo, 0, nil, :bar, "baz", false]
b = [:foo, 1, :foo, 0, true, "baz", false]
c = [:foo,1,:goo]
d = [:goo,1,:new]
Note b
is slightly changed from the OP's example.
Unless otherwise noted below, the common suffix would be calculated by reversing the arrays, applying common_prefix
and then reversing the result.
#1
Variant of Stefan's answer that rids it of zip
and map
(and retains his technique of truncating one array to at most the length of the other):
def common_prefix(a,b)
a[0,b.size].take_while.with_index { |e,i| e == b[i] }
end
common_prefix(a,b)
#=> [:foo, 1, :foo, 0]
common_prefix(c,d)
#=> []
#2
def common_prefix(a,b)
any, arr = a.zip(b).chunk { |e,f| e==f }.first
any ? arr.map(&:first) : []
end
def common_suffix(a,b)
any, arr = a[a.size-b.size..-1].zip(b).chunk { |e,f| e==f }.to_a.last
any ? arr.map(&:first) : []
end
common_prefix(a,b)
#=> [:foo, 1, :foo, 0]
# Nore: any, arr = a.zip(b).chunk { |e,f| e==f }.to_a
# => [[true, [[:foo, :foo], [1, 1], [:foo, :foo], [0, 0]]],
# [false, [[nil, true], [:bar, :baz], ["baz", false], [false, nil]]]]
common_suffix(a,b)
#=> ["baz", false]
# Note: any, arr = a[a.size-b.size..-1].zip(b).chunk { |e,f| e==f }.to_a
# => [[false, [[1, :foo], [:foo, 1], [0, :foo], [nil, 0], [:bar, true]]],
# [true, [["baz", "baz"], [false, false]]]]
When :first
is sent to the enumerator Enumerable#chunk the first element of the enumerator is returned. It therefore should be comparable in efficiency to using Enumerable#take_while.
common_prefix(c,d)
#=> []
common_suffix(c,d)
#=> []
#3
def common_prefix(a,b)
a[0,(0..[a.size, b.size].min).max_by { |n| (a[0,n]==b[0,n]) ? n : -1 }]
end
common_prefix(a,b)
#=> [:foo, 1, :foo, 0]
common_prefix(c,d)
#=> []
Gentlemen, start your engines!
Methods tested
I tested the second method given by @stefan, both methods proposed by @BroiSatse and the three methods I offered.
class Array #for broiSatse2
def common_prefix_with(other)
other_size = other.size
take_while.with_index {|v,i| other_size > i && v == other[i] }
end
end
class Cars
def self.stefan(a1,a2)
a1[0...a2.size].zip(a2).take_while { |x, y| x == y }.map(&:first)
end
def self.broiSatse1(a1,a2)
a1.take_while.with_index {|v,i| a2.size > i && v == a2[i] }
end
def self.broiSatse2(a1,a2)
a1.common_prefix_with(a2)
end
def self.cary1(a,b)
a[0,b.size].take_while.with_index { |e,i| e == b[i] }
end
def self.cary2(a,b)
any, arr = a.zip(b).chunk { |e,f| e==f }.first
any ? arr.map(&:first) : []
end
def self.cary3(a,b)
a[0,(0..[a.size, b.size].min).max_by { |n| (a[0,n]==b[0,n]) ? n : -1 }]
end
end
I did not include the solution given by @6ftDan (not to be confused with 5' Dan or 7' Dan) because it did not pass all the tests.
Construction of test arrays
random_arrays(n)
constructs one pair of arrays. n
is the size of the smaller of the two. The larger is of size n+1
.
def random_arrays(n)
m = rand(n)
# make arrays the same for the first m elements
a = Array.new(m,0)
b = Array.new(m,0)
if m < n
# make the m+1 elements different
a << 0
b << 1
# randomly assign 0s and 1a to the remaining elements
(n-m-1).times { a << rand(2); b << rand(2) } if m < n - 1
end
# make arrays unequal in length
(rand(2) == 0) ? a << 0 : b << 0
[a,b]
end
Confirm methods tested give identical results
N = 10000 #size of smaller of two input arrays
methods = Cars.methods(false)
# ensure are methods produce the same results and that
# all deal with edge cases properly
20.times do |i|
test = case i
when 0 then [[0,1],[1,1]]
when 1 then [[0],[]]
when 1 then [[0,0,0],[0,0]]
else
random_arrays(N)
end
soln = Cars.send(methods.first, *test)
methods[1..-1].each do |m|
unless soln == Cars.send(m, *test)
puts "#{m} has incorrect solution for #{test}"
exit
end
end
end
puts "All methods yield the same answers\n"
Benchmarking
require 'benchmark'
I = 1000 #number of array pairs to test
@arr = I.times.with_object([]) { |_,a| a << random_arrays(N) } #test arrays
#test method m
def testit(m)
I.times { |i| Cars.send(m, *@arr[i]) }
end
Benchmark.bm(12) { |bm| methods.each { |m| bm.report(m) { testit(m) } } end
Results
All methods yield the same answers
user system total real
stefan 11.260000 0.050000 11.310000 ( 11.351626)
broiSatse1 0.860000 0.000000 0.860000 ( 0.872256)
broiSatse2 0.720000 0.010000 0.730000 ( 0.717797)
cary1 0.680000 0.000000 0.680000 ( 0.684847)
cary2 13.130000 0.040000 13.170000 ( 13.215581)
cary3 51.840000 0.120000 51.960000 ( 52.188477)
This works on unique arrays.
Prefix
x = [:foo, 1, 0, nil, :bar, "baz", false]
y = [:foo, 1, 0, true, :bar, false]
(x & y).select.with_index {|item,index| x.index(item) == index}
OUTPUTS: => [:foo, 1, 0]
Run reverse on Arrays for Affix
(x.reverse & y.reverse).select.with_index {|item,index| x.reverse.index(item) == index}
OUTPUTS: => [false]
来源:https://stackoverflow.com/questions/25406102/longest-common-prefix-and-suffix-of-arrays