railsbugs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Apr-01 12:44 UTC
rearranging the letters of a word
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
# Based on: # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/139290 # Author: Endy Tjahjono 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"] On Apr 1, 8:44 am, "railsb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <railsb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---