[Perl] my quiz (easy)

Nadav Popplewell nadav at nirim.co.il
Thu Oct 24 06:33:45 PDT 2002


> Hello.
> 
> Write a sub which receives some integer number 
> greater than zero (lets call the number N), and
> return a string of N characters (over [A-Za-z0-9]).
> 
> Calling the sub within the same run of the program should 
> guarantee no two strings of length N are equal (i.e. all the
> strings of same length are uniqe).
> 
> Good luck.
> 
> Let's see who writes the nicest and perhaps smartest peace of code.
> 

#=========================================================
#!perl

my @info;

my @lists=('0','A','a');

sub unique_str {
    my $len=shift;
    unless (defined $info[$len]) {
       $info[$len]=[0,$lists[0]x$len];
    }
    my $ref=\$info[$len][1];
    my $ret=$$ref;
    if (length(++$$ref)!=$len) {
       my $l=$info[$len][0]=($info[$len][0]+1) % 3;
       $$ref=$lists[$l]x$len;
    }
    return $ret;
}

my %u;

my $s;
do {
    $s=unique_str(2);
    print "$s\n";
    $u{$s}++;
} until $u{$s}>1;






More information about the Perl mailing list