CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Scrambling

 



kidd
User

Apr 11, 2002, 7:25 PM

Post #1 of 12 (997 views)
Scrambling Can't Post

Hey I wanted to know if anyone has an idea of how to accomplish this...

Lets say I have a phrase:
"The force will be with you always"

How can I scramble the words around in a random order...this will be considering I will have other phrases...

"So you want to be a cowboy
Well you know it's more than just a ride
Guess you got to know the real thing
If you want to know the other side
Ain't nobody riding shotgun
In this world tonight"


Kanji
User

Apr 11, 2002, 9:03 PM

Post #2 of 12 (994 views)
Re: [kidd] Scrambling [In reply to] Can't Post

Split your phrase into an array, and then use rand and splice to juggle it around ...


Code
 my @phrase = split ' ' => $phrase; 
my @random;

while (@phrase) {
my $index = rand scalar @phrase;
push @random, splice( @phrase, $index, 1 );
}

$phrase = "@random";


--k.
--k.


kidd
User

Apr 12, 2002, 4:18 PM

Post #3 of 12 (987 views)
Re: [Kanji] Scrambling [In reply to] Can't Post

It worked great....thanks

"So you want to be a cowboy
Well you know it's more than just a ride
Guess you got to know the real thing
If you want to know the other side
Ain't nobody riding shotgun
In this world tonight"


Paul
Enthusiast

Apr 14, 2002, 4:20 AM

Post #4 of 12 (983 views)
Re: [Kanji] Scrambling [In reply to] Can't Post

That doesn't put the words in a random order, it puts individual letters in a random order...also some letters will be missed and others duplicated.

Try:


Code
my $phrase = "My name is Paul"; 
my @phrase = split /\s/, $phrase;
my $rand;
my %rand;

for (@phrase) {
{
$rand = int rand @phrase;
exists $rand{$rand} ? redo : $rand{$rand}++;
}
print "$phrase[$rand] ";
}



(This post was edited by RedRum on Apr 14, 2002, 4:22 AM)


Jasmine
Administrator

Apr 14, 2002, 7:22 AM

Post #5 of 12 (974 views)
Re: [RedRum] Scrambling [In reply to] Can't Post

I've tested both [ yours ] and [ Kanji ]'s, and both work fine. I'm a little confused about your post? splicing the element out of the array prevents missing/duplicate elements. But I do prefer your method of splitting on \s instead of the literal space ' ' (* see update below).

However, one is significantly slower than the other...


Code
use Benchmark; 

my $phrase = 'May the force be with you.';

timethese( 500_000, {

'Kanji' => sub {
my @phrase = split ' ' => $phrase;
my @random;
while (@phrase) {
my $index = rand scalar @phrase;
push @random, splice( @phrase, $index, 1 );
}

},
'RedRum' => sub {
my @phrase = split /\s/, $phrase;
my $rand;
my %rand;
for (@phrase) {
{
$rand = int rand @phrase;
exists $rand{$rand} ? redo : $rand{$rand}++;
}
}
}
}
);


Benchmark: timing 500000 iterations of Kanji, RedRum...
Kanji: 15 wallclock secs (15.76 usr + 0.00 sys = 15.76 CPU) @ 31725.89/s (n=500000)
RedRum: 36 wallclock secs (36.47 usr + 0.00 sys = 36.47 CPU) @ 13709.90/s (n=500000)


*Update: The use of splitting on spaces with ' ' is better than \s because ' ' treats consecutive spaces as a single space. I have been duly chastised Smile


(This post was edited by Jasmine on Apr 14, 2002, 7:54 AM)


mhx
Enthusiast / Moderator

Apr 14, 2002, 7:29 AM

Post #6 of 12 (973 views)
Re: [RedRum] Scrambling [In reply to] Can't Post


In Reply To
That doesn't put the words in a random order, it puts individual letters in a random order...also some letters will be missed and others duplicated.

Umm, it actually does work...
Either you've missed something, or your Perl interpreter behaves very strangely.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



mhx
Enthusiast / Moderator

Apr 14, 2002, 7:37 AM

Post #7 of 12 (970 views)
Re: [Jasmine] Scrambling [In reply to] Can't Post


In Reply To
But I do prefer your method of splitting on \s instead of the literal space ' '.


I do prefer Kanji's method, because (from the split manpage) ...


Code
As a special case, specifying a PATTERN of space 
(`' '') will split on white space just as `split'
with no arguments does. Thus, `split(' ')' can be
used to emulate awk's default behavior, whereas
`split(/ /)' will give you as many null initial
fields as there are leading spaces. A `split' on
`/\s+/' is like a `split(' ')'
except that any
leading whitespace produces a null first field. A
`split' with no arguments really does a
`split(' ', $_)' internally.


So instead of splitting at every whitespace, Kanji's version splits at every sequence of whitespace, which means he really gets a list of separated words.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Kanji
User

Apr 14, 2002, 7:43 AM

Post #8 of 12 (968 views)
Re: [Jasmine] Scrambling [In reply to] Can't Post


Quote
But I do prefer your method of splitting on \s instead of the literal space ' '.


Ah, but that's the beauty of it: I'm not splitting on a literal space!

perldoc -f split

As a special case, specifying a PATTERN of space (' ') will split on white space just as split() with no arguments does.

--k.


Jasmine
Administrator

Apr 14, 2002, 7:49 AM

Post #9 of 12 (966 views)
Re: [mhx] Scrambling [In reply to] Can't Post

Touche! Nice catch Cool

Jasmine rushes to re-read the manpages for those extremely useful tidbits Smile


Paul
Enthusiast

Apr 14, 2002, 9:22 AM

Post #10 of 12 (961 views)
Re: [mhx] Scrambling [In reply to] Can't Post

>>
Umm, it actually does work...
Either you've missed something, or your Perl interpreter behaves very strangely.
<<

Perhaps it does, but I copied and pasted the exact code from the post and when I run it, it scrambles all letters and produces duplicates and also misses letters.


Paul
Enthusiast

Apr 14, 2002, 9:23 AM

Post #11 of 12 (960 views)
Re: [Jasmine] Scrambling [In reply to] Can't Post

Yes of course mine will be slower as it keeps looping if a duplicate is hit so theoretically if rand wasn't very random it could loop for infinity Wink.


(This post was edited by RedRum on Apr 14, 2002, 9:24 AM)


Paul
Enthusiast

Apr 15, 2002, 3:07 AM

Post #12 of 12 (949 views)
Re: [Kanji] Scrambling [In reply to] Can't Post

Kanji, reasons why your method isn't so good:

This is bad because splice is already O(N), and since you do it N times, you just invented a quadratic algorithm; that is, O(N**2). This does not scale, although Perl is so efficient that you probably won't notice this until you have rather largish arrays.


(This post was edited by RedRum on Apr 15, 2002, 3:11 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives