[SOLVED] Ruby – permutation program in one line

Issue

just started ruby 🙂

I am creating a small ruby program where a user inputs a string of letters and it prints all the possible permutations.

I am trying to write a function that is only 1 line long, but im having trouble getting it to run

Any help please 🙂

puts "Enter phrase:"
input_string = gets.split("")

#function
print input_string.permutation().to_a

Solution

Try calling chomp() before calling split():

puts "Enter phrase:"
input_string = gets.chomp().split("")
print input_string.permutation().to_a, "\n"

Example Usage:

Enter phrase:
ABC
[["A", "B", "C"], ["A", "C", "B"], ["B", "A", "C"], ["B", "C", "A"], ["C", "A", "B"], ["C", "B", "A"]]

Try it out here.

Answered By – Sash Sinha

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *