Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Cryptogram Generator

by Elihu (Novice)
on Feb 03, 2000 at 07:48 UTC ( [id://2774]=sourcecode: print w/replies, xml ) Need Help??
Category: Fun
Author/Contact Info Elihu <elihu@atdot.org>
Description: This takes a quote in the form of a small text file or a fortune with the '-f' flag and performs a (random) mono-alphabetic substitution cipher on it to create a cryptogram - just like the newspapers!
#!/usr/bin/perl -w
#
# Script for creating a cryptogram out of plaintext
# By:      Rob Hudson <elihu@atdot.org>
# Created: 22 Jan 2000
# Updated: 07 Feb 2000 : Changed to using FYShuffle
# Updated: 15 Apr 2000 : Added fortune support
use Text::Wrap;
my $text = "";
my @lines;
my $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

## Set up fortune locale and which files to exclude
my $fortune_dir = "/usr/share/games/fortunes/";
my $exclude = "ascii-art|translate-me";

if (@ARGV && $ARGV[0] ne '-f') {
  my $inFile = $ARGV[0] if ($ARGV[0] ne "") or die "No file specified\
+n";
  # Read it, Scrunch it, Up it.
  open IN, $inFile or die "Can't open input file: $!";
  while (<IN>) {
    s/[\s]+/ /g;
    $text .= $_;
  }
  close (IN);

  print "\nHere is your quote:\n\n";
}
else {
  opendir FORTUNE_DIR, $fortune_dir or die "Can't read directory $fort
+une_dir: $!";
  my @files = grep !/^\.|.dat$|$exclude/, readdir FORTUNE_DIR;

  # Pick a random file from which to get the fortune
  my $fortune_file = $files[rand @files];

  # This is inefficient from a memory usage standpoint, but it's much 
+easier
  $/ = '%';
  open FILE, "$fortune_dir$fortune_file" or die "Can't open file: $!";
  while (<FILE>) {
    push @lines, $_;
  }
  close (FILE);

  $text = $lines[rand @lines];
  $text =~ s/%$//g;       # remove the %
  $text =~ s/[\s]+/ /g;   # make spacing uniform
  $text =~ s/^\s+//g;     # remove whitespace at beginning

  print "\nQuote taken from fortune file '" . $fortune_file . "':\n\n"
+;
}

## Start cryptogram processing
$text = uc($text);
# print wrap ("","", $text), "\n\n";

## Build random alphabet string, not allowing repeat letters
my @alpha = split //, $alpha;
my $substit = join '', fisher_yates_shuffle(\@alpha);

## Have to use an eval since $vars don't get interpreted inside a tr c
+all.
eval "\$text =~ tr/$alpha/$substit/";
print wrap ("","", $text), "\n\n";

## Taken from perlfaq4 (thanks btrott)
sub fisher_yates_shuffle {
  my $array = shift;
  for (my $i = @$array; --$i; ) {
    my $j = int rand ($i+1);
    next if $i == $j;
    @$array[$i, $j] = @$array[$j, $i];
  }
  return join '', @$array;
}
Replies are listed 'Best First'.
RE: Cryptogram Generator
by btrott (Parson) on Feb 07, 2000 at 23:30 UTC
    A couple of comments:

    1) the open || die statement should actually be

    open IN, $infile or die "Can't open input file: $!";
    For reasons of precedence.

    2) Here are some faster algorithms. One way is to use a hash to record which characters you've already placed into your substitution string. Here's the main loop:

    my %set; my $substit = ""; for (1..26) { my $randchar; do { $randchar = chr((int rand 26) + 65) } while $set{$randchar}++; $substit .= $randchar; }
    This way you don't have to do a search through the string each time.

    And here's an even better way of doing it: build a random permutation of the alphabet string. To do this, we'll actually need an array:

    my @alpha = split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Now we set another array equal to this one, then call the random shuffle algorithm on it:
    my @crypt = @alpha; fisher_yates_shuffle(\@crypt);
    which shuffles the array in place. Now all you need to do is get back the substitution string:
    my $substit = join '', @crypt;
    Here's the definition of the fisher_yates_shuffle sub:
    sub fisher_yates_shuffle { my $array = shift; for (my $i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i, $j] = @$array[$j, $i]; } return join '', @$array; }
    (Taken directly from perlfaq4.)

    I did some benchmarking on these, and here's what I got:

    Benchmark: timing 5000 iterations of orig, f_yates, hash... orig: 33 secs (23.60 usr 0.00 sys = 23.60 cpu) f_yates: 5 secs ( 2.88 usr 0.00 sys = 2.88 cpu) hash: 13 secs ( 5.68 usr 0.00 sys = 5.68 cpu)
    where "orig" is the one you posted, "f_yates" is the one using fisher_yates_shuffle, and "hash" is the one using a hash to record seen characters.

    If I've messed anything up, let me know.

      Wow. Thanks for the new ideas. I've been playing with the different algorithms a bit. I've seen somewhere how you do benchmarking, I'll try my own just to see for myself how it's done and if I get comparable numbers.

      I was pretty sure there was a better way to do that. :)

        I've been working on a cryptogram solver and discovered this post and found the code useful - I snagged the code for the fisher_yates_shuffle to grab one of my solved cryptograms to recreate one to work on solving and I found that in some instances one or two of the letters in $substit remained the same as it was originally. The line that checks for $i == $j does a next BUT $i keeps on counting down and effectively leaves the unchanged letter alone. I fixed it by incrementing $i before doing the next.

        I realize in some cases the shuffle results are okay if this happens, but in a cryptogram, it won't fly... ;-) Just thought I'd throw this info back out there just in case you or anyone else may have a need for the fix.

        Here's the modified sub:

        ## Taken from perlfaq4 (thanks btrott) sub fisher_yates_shuffle { my $array = shift; for (my $i = @$array; --$i; ) { my $j = int rand ($i+1); # next if $i == $j; # original if ($i == $j) { # this means the letter will be the same as it wa +s before $i++; # put $i back where it was and get another one next; } @$array[$i, $j] = @$array[$j, $i]; } return join '', @$array; }

        thanks for the original post! I'm learning a little here and there about using perl to solve cryptos... it's gonna take a while, but it's fun!!!

        Life is short, but it's wide -- Chuck Pyle

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found