Hey, do you want to view all the possible arrangement of the letters of a word, then try this.
# file : wordperm.rb
# Author : Selasie Agbavor <volsbit@gmail.com>
# Date : May, 16 2008
# Time : 14:20:00 G.M.T
class String
def shuffle
self.split(//).sort_by{rand}.to_s
end
end
class Integer
def fact
if self.zero?
1
else
self * (self-1).fact
end
end
end
word = gets.chomp
array_of_letters = word.split(//)
hash = Hash.new(0)
array_of_letters.each{ |lett|
hash[lett] += 1
}
counts_greater_than_one = []
hash.each_value{ |val|
counts_greater_than_one << val if val > 1
}
if !counts_greater_than_one.empty?
product = 1
counts_greater_than_one.each{ |num|
product *= num.fact
}
permutation = word.size.fact / product
else
permutation = word.size.fact
end
already_shuffled_words = []
count = 0
while count < permutation
shuffled_word = word.shuffle
if !already_shuffled_words.include? shuffled_word
puts shuffled_word
#sleep 1 # optional
already_shuffled_words << shuffled_word
count += 1
end
end
puts “\n”
puts “#{word} can be arranged #{permutation} ways taking all characters”