Array that alphabetizes and alternates between all caps

后端 未结 5 1000
面向向阳花
面向向阳花 2020-12-22 10:44

I need to get the user to input five words, which I believe I have. Then the program needs to spit the words in alphabetical order with every other word being in all-caps, s

5条回答
  •  滥情空心
    2020-12-22 10:52

    with_index can help you with the every other word problem:

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

提交回复
热议问题