Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: quick and dirty cryptogram puzzle

by Snarius (Sexton)
on Jan 01, 2008 at 19:59 UTC ( [id://659880]=note: print w/replies, xml ) Need Help??


in reply to Re: quick and dirty cryptogram puzzle
in thread quick and dirty cryptogram puzzle

Thanks for the criticism! I've not played many cryptograms, so I wasn't familiar with that idea. I wouldn't have called it a "complete shuffle" though. An "incomplete shuffle", maybe :)

I've added a bit to my shuffling function to ensure a derangement. It's a bit quick and dirty...
sub gen_random_key{ ####sub fisher_yates_shuffle { my @array = split ("", $alpha); my @alpha = @array; for (my $i = @array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @array[$i, $j] = @array[$j, $i]; } for (0..$#alpha){ $decrypt{$alpha[$_]} = $array[$_]; $encrypt{$array[$_]} = $alpha[$_]; } #ensure a derangement for (0..$#alpha){ if ($alpha[$_] eq $array[$_]) { gen_random_key(); #warn 'key not deranged, setting another..'; return; } } }

Replies are listed 'Best First'.
Re^3: quick and dirty cryptogram puzzle
by runrig (Abbot) on Jan 02, 2008 at 18:09 UTC
    next if $i == $j;
    This line is unnecessary. It's been discussed before here and elsewhere, but in an array of any significant size (>10 or 20 or so), the odds that i and j are equal are low enough that the logic to test if they are equal outweighs the cost of just swapping the array element with itself (i.e. you're testing to see if they're equal on every iteration, costing something on every iteration, where if you dont' test you'd hardly ever be swapping something with itself anyway).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://659880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found