お題:文字列を先頭から見て同じところまで除去 - No Programming, No Life
rubyでやってみた
ソース
$ cat cut_down_str.rb class CutDownString def hoge(*strs) i = 0 (strs.map(&:length).min.size - 1).times do break if strs.map{|s| s[i]}.uniq.size != 1 i += 1 end strs.map{|s| s[i..-1]} end end
rspec
$ cat cut_down_str_spec.rb # -*- coding: UTF-8 -*- require_relative 'cut_down_str' describe 'CutDownString' do before do @cds = CutDownString.new end describe '#hoge' do context 'when "abcdef", "abc123"' do subject { @cds.hoge("abcdef", "abc123") } it { should eq ["def", "123"] } end context 'when "あいうえお", "あいさんさん", "あいどる"' do subject { @cds.hoge("あいうえお", "あいさんさん", "あいどる") } it { should eq ["うえお", "さんさん", "どる" ] } end context 'when "12345", "67890", "12abc"' do subject { @cds.hoge("12345", "67890", "12abc") } it { should eq ["12345", "67890", "12abc"] } end end end
結果
$ rspec cut_down_str_spec.rb -f CutDownString #hoge when "abcdef", "abc123" should == ["def", "123"] when "あいうえお", "あいさんさん", "あいどる" should == ["うえお", "さんさん", "どる"] when "12345", "67890", "12abc" should == ["12345", "67890", "12abc"] Finished in 0.00238 seconds
iとかbreakとか邪道な気がする。