-1

I am trying to take a sentence, and reverse the positions of the letters in each word.

Below is my code that does not work:

def test(sentence)
 array = []
 array << sentence.split
 array.collect {|word| word.reverse}
end

My problem is with:

array << sentence.split

It says it divides each word, but when I use interpolation, it reverses the whole sentence. Below is a similar code that works:

def test2
 dog = ["Scout", "kipper"]
 dog.collect {|name| name.reverse}
end

But it does not accept a sentence, and it already has the array defined.

sawa
  • 165,429
  • 45
  • 277
  • 381

3 Answers3

4

I'm thinking you want to split, then map each element of the array to its reversed version then rejoin the array into a string:

def test(sentence)
  sentence.split.map {|word| word.reverse}.join(" ")
end

More concise using symbol-to-proc (credit @MarkThomas in comments)

sentence.split.map(&:reverse).join " "

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
  • I need to use .collect in this problem, and your right about the join at the end, I have yet to get the words to reverse so haven't included that yet haha. I feel as my split is not truly splitting the words up. I have been getting this as a return [["Hello" "World"]] <- ive never encountered the double bracket but could be my issue as well. Tried looking it up but havent found anything on it yet – Ryne Stajcar Jan 04 '18 at 17:47
  • Sorry for a deleted comment here, it was misleading (I misremembered some ruby specifics). Seems like `collect` and `map` are basically the same (detail: https://stackoverflow.com/a/5254764/13956) so you can just swap `map` in the above answer with `collect`. – Mike Tunnicliffe Jan 04 '18 at 17:58
  • `split` is not what was creating the nested array here, it gives you an array. The nesting came from using `<<` to append the array as an element in the other array you created with `array = []`. (That is: you are effectively doing `[] << ["Hello", "World"]` which gives `[["Hello", "World"]]`.) – Mike Tunnicliffe Jan 04 '18 at 18:02
  • Awesome, that helps me a lot. Thanks for taking the time to explain that for me! – Ryne Stajcar Jan 04 '18 at 18:07
  • No problem, happy to help. – Mike Tunnicliffe Jan 04 '18 at 18:08
  • 2
    More concise: `sentence.split.map(&:reverse).join " "` – Mark Thomas Jan 04 '18 at 18:08
  • 1
    @MarkThomas Darn, you're one char shorter... `sentence.reverse.split.reverse.join " "`. On the other hand, mine appears to be a bit faster... – Stefan Pochmann Jan 04 '18 at 18:14
  • This answer grabs punctuation during reversal, I suspect this is not what the OP wants. – Sagar Pandya Jan 04 '18 at 18:20
  • @SangarPandya Indeed, the question is not a detailed specification and there are likely many enhancements that could be made depending on further detail. However, I think this answer addresses the main concern (feel free to elaborate though @RyneStajcar) – Mike Tunnicliffe Jan 04 '18 at 18:24
2

Unlike methods that break up the sentence into words, reverses each word and then rejoins them, the use of Array#gsub with a regular expression preserves non-word characters (such as the comma in the example below) and multiple spaces.

"vieille, mère    Hubbard".gsub(/\b\p{L}+\b/, &:reverse)
  #=> "ellieiv, erèm    drabbuH"
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0
def reverse(str)
  str.split.map { |s| s.length < 5 ? s : s.reverse }.join(' ')
end
puts reverse ("this is a catalogy")

This should reverse each word thats length upto 5

S.Shah
  • 19
  • 7