ruby - Method to front capitalized words -
i trying move capitalized words front of sentence. expect this:
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."]
the key maintaining word order.
i feel i'm close.
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
curious see other elegant solutions might be.
def capsort(words) words.partition{|s| s =~ /\a[a-z]/}.flatten end 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."]
Comments
Post a Comment