4

2 つの配列の最も長い共通プレフィックス (元のインデックス 0 から始まるサブ配列) とサフィックス (元のインデックス -1 で終わるサブ配列) を取得する最良の方法は何ですか? たとえば、次の 2 つの配列があるとします。

[:foo, 1, :foo, 0, nil, :bar, "baz", false]
[:foo, 1, :foo, 0, true, :bar, false]

これらの配列の最も長い一般的なプレフィックスは次のとおりです。

[:foo, 1, :foo, 0]

これらの配列の最も長い一般的なサフィックスは次のとおりです。

[false]

インデックス 0/-1 の要素が元の配列で異なる場合、共通のプレフィックス/サフィックスは空の配列にする必要があります。

4

5 に答える 5

3

考えられる解決策の 1 つ:

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]

入力配列と出力配列を逆にすると、共通のサフィックスが見つかります。

a1.reverse.zip(a2.reverse).take_while { |x, y| x == y }.map(&:first).reverse
#=> [false]

エッジケース: zip「引数」配列にnil値を入力します:

a1 = [true, nil, nil]
a2 = [true]

a1.zip(a2).take_while { |x, y| x == y }.map(&:first)
#=> [true, nil, nil]

これは、最初の配列を 2 番目の配列の長さに切り詰めることで回避できます。

a1[0...a2.size].zip(a2).take_while { |x, y| x == y }.map(&:first)
于 2014-08-20T13:47:00.733 に答える
2

エッジケースのない別のソリューション:

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] }

編集:パフォーマンスの向上

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
于 2014-08-20T14:45:45.503 に答える
1

諸君、エンジンを始動せよ!

テストされた方法

@stefan によって提供された 2 番目の方法、@BroiSatse によって提案された両方の方法、および私が提供した 3 つの方法をテストしました。

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

すべてのテストに合格しなかったため、@ 6ftDan (5' Dan または 7' Dan と混同しないでください) によって提供されたソリューションは含めませんでした。

テストアレイの構築

random_arrays(n)配列の 1 つのペアを構築します。n小さい方のサイズです。大きい方のサイズ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

テストした方法が同一の結果をもたらすことを確認する

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"

ベンチマーク

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

結果

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)
于 2014-08-21T03:02:13.510 に答える