【问题标题】:Array that alphabetizes and alternates between all caps按字母顺序排列并在所有大写字母之间交替的数组
【发布时间】:2015-08-26 17:25:41
【问题描述】:

我需要让用户输入五个单词,我相信我已经做到了。然后程序需要按字母顺序吐出单词,其他单词全部大写,从第一个单词开始,其余单词全部小写。

我最终会将循环切换到 500 个单词,而我需要更改的只是循环中的数字。我怎样才能让它工作?

这是我目前所拥有的:

words = []

5.times do
  puts "Please enter a word"
  words << gets.chomp
end

puts words.sort.odd.upcase

【问题讨论】:

  • 请给我们一个输入%w(orange apple lemon melon lime)的输出示例。很难理解“每隔一个词”和“剩下的”是什么意思。
  • 输入的单词可以大小写混合吗?照原样,您的问题没有得到很好的定义,并且给猜测要求留下了太多空间。
  • odd 在哪里定义?它的合同是什么?

标签: arrays ruby


【解决方案1】:

with_index 可以帮助您解决其他单词问题:

words = %w(hello world this is test)
# => ["hello", "world", "this", "is", "test"]
words.map(&:downcase).sort.map.with_index {|word, index| index.odd? ? word : word.upcase}
# => ["HELLO", "is", "TEST", "this", "WORLD"]

【讨论】:

    【解决方案2】:

    这里有两种不使用索引的方法。

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

    【讨论】:

      【解决方案3】:

      如果我理解得很好(但问题不是很清楚),您想排序,然后将两个单词中的一个单词大写,另一个单词小写:

      words.sort.each_with_index.map{|w,i| i.odd? ? w.upcase : w.downcase }
      

      测试:

      words=%w( orange banana apple melon)
      

      结果:

      ["APPLE", "banana", "MELON", "orange"]
      

      【讨论】:

      • 谢谢,这就是我要找的。​​span>
      • 如果有回答问题的标志将不胜感激! ;)
      • 没有必要或特别希望要求您选择答案。尽你所能回答问题,不要担心分数。分数随着答案的好而累积。
      • 您可能想根据您的代码检查结果。
      【解决方案4】:

      只是出于好奇:

      arr = %w(orange apple lemon melon lime)
      a1, a2 = arr.sort.partition.with_index { |_, i| i.even? }
      a1.map(&:downcase).zip(a2.map &:upcase).flatten.compact
      

      【讨论】:

      • 使用flat_map 而不是map...flatten
      • @theTinMan 我的解决方案中没有map.flatten。或者,至少,我不明白如何对flat_map 做同样的事情。
      • 是的。我错过了mapzip 内。继续。
      • 心理图像:“是的,是的,先生!” :-)
      【解决方案5】:

      我会使用:

      user_input = %w(the quick red fox jumped)
      uc_flag = true
      output = []
      user_input.sort.each { |w| 
        output << if uc_flag
                    w.upcase
                  else
                    w.downcase
                  end
        uc_flag = !uc_flag
      }
      output # => ["FOX", "jumped", "QUICK", "red", "THE"]
      

      它是老式的,但速度非常快,因为它只通过数组一次。

      它可以写得更简洁一点:

      output << (uc_flag ? w.upcase : w.downcase)
      

      但三元语句通常被认为是不可取的。

      如果允许用户输入大小写混合的单词,请使用:

      sort_by(&:downcase) 
      

      而不是sort

      【讨论】:

        猜你喜欢
        • 2017-06-23
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多