お題:文字列を先頭から見て同じところまで除去

お題:文字列を先頭から見て同じところまで除去 - No Programming, No Life

rubyで回答
# coding: CP932
require "rspec"

def shift_same_str(*args)
  args.map(&:chars).map(&:to_a).tap do |x|
    x.min_by(&:count).dup.each {|s| x.each(&:shift) if x.all?{|a| a.first.eql?(s)}}
  end.map!(&:join)
end

describe "shift_same_str" do
  
  context "with argument 'abcdef', 'abc123'" do
    subject { shift_same_str('abcdef', 'abc123') }
    it { should eq ['def', '123'] }
  end

  context "with argument 'あいうえお', 'あいさんさん', 'あいどる'" do
    subject { shift_same_str('あいうえお', 'あいさんさん', 'あいどる') }
    it { should eq ['うえお', 'さんさん', 'どる'] }
  end

  context "with argument '12345', '67890', '12abc'" do
    subject { shift_same_str('12345', '67890', '12abc') }
    it { should eq ['12345', '67890', '12abc'] }
  end
  
end

__END__
>rspec odai.rb -fd

shift_same_str
  with argument 'abcdef', 'abc123'
    should == ["def", "123"]
  with argument 'あいうえお', 'あいさんさん', 'あいどる'
    should == ["うえお", "さんさん", "どる"]
  with argument '12345', '67890', '12abc'
    should == ["12345", "67890", "12abc"]

Finished in 0.01562 seconds
3 examples, 0 failures