我试图将大写的单词移到句子的前面.我希望得到这个:
capsort(["a", "This", "test.", "Is"])
#=> ["This", "Is", "a", "test."]
capsort(["to", "return", "I" , "something", "Want", "It", "like", "this."])
#=> ["I", "Want", "It", "to", "return", "something", "like", "this."]
Run Code Online (Sandbox Code Playgroud)
关键是维持单词顺序.
我觉得我非常接近.
def capsort(words)
array_cap = []
array_lowcase = []
words.each { |x| x.start_with? ~/[A-Z]/ ? array_cap.push(x) : array_lowcase.push(x) }
words= array_cap << array_lowcase
end
Run Code Online (Sandbox Code Playgroud)
很想知道其他优雅的解决方案是什么.
ruby ×1