0

How to print an array of elements in reverse order not just single digit number but also multi-digit numbers.

[2, 5, 6 7]

It should print the array elements in reverse order as 7 6 5 2 by following a space for each number.

I already wrote the code for this.

   puts "Enter the array elements"
    arr = gets.strip
    arr = arr.split(' ').map(&:to_i)
    x = arr.reverse_each {|f| }
    z = x.join(" ")
    print z.reverse

That is cool with single digit numbers, how can I reverse the multi-digit numbers in an array of inputs given by the user input like:

[45, 76, 87 ] # this should reverse the array as `87 76 45`


[556, 674, 878 ] # this should reverse the array as `878 674 556`
[8797, 7347, 9374 ] # this should reverse the array as `9374 7374 8797`
7
  • What is n? It is never used in the above code. Commented Mar 9, 2016 at 6:11
  • n is just used to know many elements does the array contain. However, I didn't use n in the above code. Maybe I should remove that code.! Commented Mar 9, 2016 at 6:13
  • I don't have time to post an answer, but here's a hint: two Ruby core classes have a reverse method, Array and String. You are using the latter one. Commented Mar 9, 2016 at 6:16
  • puts gets.strip.split(' ').map(&:to_i).reverse.inspect Commented Mar 9, 2016 at 6:20
  • Thanks for the hit. Am getting the same output [34, 54,76] => [67, 45, 43]' with both reverse` methods reverse_each and reverse which is not I want. Commented Mar 9, 2016 at 6:20

2 Answers 2

5

If you like one-liners:

gets.strip.split(' ').reverse.join(' ')

This will take the input 1 2 3 45 678 9 and convert it to "9 678 45 3 2 1"

Sign up to request clarification or add additional context in comments.

3 Comments

#Optmized code. This worked like a charm!. Thank you.
shouldn't it be 9 876 54 3 2 1 ? instead of your posted output 9 678 45 3 2 1
No. It would be 9 876 54 3 2 1 if you reversed the string. In my solution, reverse is being called on the output of split which is an array. This reverses the order of the elements in the array, but not the content of the elements themselves.
4

Input: [8797, 7347, 9374 ]

Output: "9374 7374 8797"

arr = gets.chomp
arr = arr.split(' ').map(&:to_i)
x = arr.reverse.join(' ')
print x

Use reverse and join chained and it should return a String type that joined your reversed array.

4 Comments

Please next time use “edit” to modify/update/correct the already posted answer, not just delete the old one and post the new version as a separate answer. Plus, the reference to Array#reverse was helpful.
@mudasobwa Sorry about that, will definitely do that next time
@philip yoo, Perfect solution. Thank you.
@Packer hey check out Scott's solution as well. I realized you don't need to change them to Integer type, but rather just split, reverse, then join again. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.