[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Jumble character order in a string

jotto

3/5/2006 1:11:00 AM

I'm trying to figure out a way to randomize the letters in a word,
essentially jumbling the spelling.

jumbling the word jumble would give: lbujme or bujlme etc...

right now i'm considering:

rand(string.length) to get a random index of a letter in the string,
then making a new string one character at a time. but this obviously
doesn't take into account using the letters only once.

any ideas?

2 Answers

Bill Kelly

3/5/2006 1:22:00 AM

0

From: "jotto" <jonathan.otto@gmail.com>
>
> I'm trying to figure out a way to randomize the letters in a word,
> essentially jumbling the spelling.
>
> jumbling the word jumble would give: lbujme or bujlme etc...
>
> right now i'm considering:
>
> rand(string.length) to get a random index of a letter in the string,
> then making a new string one character at a time. but this obviously
> doesn't take into account using the letters only once.
>
> any ideas?

Hi, here's one way:

irb(main):034:0> "abcdefg".split(//).sort_by{rand}.join
=> "gafbedc"


Regards,

Bill




Pistos Christou

3/7/2006 6:11:00 PM

0

Bill Kelly gave a nicely concise and elegant solution, so I won't bother
giving mine. But I wanted to ask what this is for? :) A game? :)

Pistos

jotto wrote:
> I'm trying to figure out a way to randomize the letters in a word,
> essentially jumbling the spelling.
>
> jumbling the word jumble would give: lbujme or bujlme etc...
>
> right now i'm considering:
>
> rand(string.length) to get a random index of a letter in the string,
> then making a new string one character at a time. but this obviously
> doesn't take into account using the letters only once.
>
> any ideas?


--
Posted via http://www.ruby-....