class Array
def tail
ary = dup
ary.empty? ? [] : ary.drop(1)
end
def tails
ary = dup
ary.empty? ? [[]] : ([ary] + ary.tail.tails)
end
end
class String
def max_dup_string
ary = dup.split('')
ary.tails.sort.tail.zip(ary.tails.sort).map do |fst, snd|
fst.zip(snd).take_while{|l, r| l == r}.map{|l, r| l}.join.strip
end.sort{|a, b| b.length <=> a.length}[0]
end
end
p "Ask not what your country can do for you, but what you can do for your country".max_dup_string
# => "can do for you"
p "Hello, World. Hello, Ruby!".max_dup_string
# => "Hello,"
p "mississippi".max_dup_string
# => "issi"
def tail
ary = dup
ary.empty? ? [] : ary.drop(1)
end
def tails
ary = dup
ary.empty? ? [[]] : ([ary] + ary.tail.tails)
end
end
class String
def max_dup_string
ary = dup.split('')
ary.tails.sort.tail.zip(ary.tails.sort).map do |fst, snd|
fst.zip(snd).take_while{|l, r| l == r}.map{|l, r| l}.join.strip
end.sort{|a, b| b.length <=> a.length}[0]
end
end
p "Ask not what your country can do for you, but what you can do for your country".max_dup_string
# => "can do for you"
p "Hello, World. Hello, Ruby!".max_dup_string
# => "Hello,"
p "mississippi".max_dup_string
# => "issi"
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.4.0]
Enjoy!
0 件のコメント:
コメントを投稿