下面是不使用索引兩種方式。
arr = %w|the quicK brown dog jumpEd over the lazy fox|
#=> ["the", "quicK", "brown", "dog", "jumpEd", "over", "the", "lazy", "fox"]
注:
arr.sort
#=> ["brown", "dog", "fox", "jumpEd", "lazy", "over", "quicK", "the", "the"]
#1
e = [:UC, :LC].cycle
arr.sort.map { |w| (e.next == :UC) ? w.upcase : w.downcase }
# => ["BROWN", "dog", "FOX", "jumped", "LAZY", "over", "QUICK", "the", "THE"]
#2
arr.sort.each_slice(2).flat_map { |u,v| v ? [u.upcase, v.downcase] : [u.upcase] }
# => ["BROWN", "dog", "FOX", "jumped", "LAZY", "over", "QUICK", "the", "THE"]
請給我們一個輸入'%w(橙子蘋果檸檬甜瓜酸橙)'的例子。很難理解「隔一個字」和「剩餘」的意思。 – mudasobwa
輸入詞可以混合大小寫嗎?原因是,你的問題沒有很好的定義,並留下太多的空間來猜測需求。 –
定義「奇數」在哪裏?它的合同是什麼? –