-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringperm.rb
More file actions
27 lines (25 loc) · 800 Bytes
/
stringperm.rb
File metadata and controls
27 lines (25 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
=begin
STRING PERMUTATIONS
CHALLENGE DESCRIPTION:
Write a program which prints all the permutations of a string in alphabetical order. We consider that digits < upper case letters < lower case letters. The sorting should be performed in ascending order.
INPUT SAMPLE:
Your program should accept a file as its first argument. The file contains input strings, one per line.
For example:
hat
abc
Zu6
OUTPUT SAMPLE:
Print to stdout the permutations of the string separated by comma, in alphabetical order.
For example:
aht,ath,hat,hta,tah,tha
abc,acb,bac,bca,cab,cba
6Zu,6uZ,Z6u,Zu6,u6Z,uZ6
=end
lines = File.readlines(ARGV[0])
lines.each do |line|
strings = []
line.chomp.chars.permutation.each do |perm|
strings << perm.join
end
puts strings.sort.join(",")
end