Hello there,
I have to rearrange the letters of a word in all possible ways in
my rails application.I am not getting the exact way doing so, please
can anybody help me in developing that logic.
Thank you in advance.
Hello there,
I have to rearrange the letters of a word in all possible ways in
my rails application.I am not getting the exact way doing so, please
can anybody help me in developing that logic.
Thank you in advance.
class String
def perm
return [self] if self.length < 2
ret = []
0.upto(self.length - 1) do |n|
#rest = self.split('')
rest = self.split(//u) # for UTF-8 encoded
strings
picked = rest.delete_at(n)
rest.join.perm.each { |x| ret << picked + x }
end
ret
end
end
p “abc”.perm #=> [“abc”, “acb”, “bac”, “bca”, “cab”, “cba”]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs