[SOLVED] Ruby – permutation program in one line


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("")

print input_string.permutation().to_a


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:
[["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.