diff options
| -rw-r--r-- | challenge-022/yet-ebreo/perl5/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-022/yet-ebreo/perl5/ch-2.pl | 54 |
2 files changed, 46 insertions, 24 deletions
diff --git a/challenge-022/yet-ebreo/perl5/ch-1.pl b/challenge-022/yet-ebreo/perl5/ch-1.pl index b0a1bae4a4..fbc68cb5fd 100644 --- a/challenge-022/yet-ebreo/perl5/ch-1.pl +++ b/challenge-022/yet-ebreo/perl5/ch-1.pl @@ -1,7 +1,15 @@ -# Write a script to print first 10 Sexy Prime Pairs. -# Sexy primes are prime numbers that differ from each other by 6. -# For example, the numbers 5 and 11 are both sexy primes, because 11 - 5 = 6. +# Write a script to print first 10 Sexy Prime Pairs. +# Sexy primes are prime numbers that differ from each other by 6. +# For example, the numbers 5 and 11 are both sexy primes, because 11 - 5 = 6. # The term “sexy prime” is a pun stemming from the Latin word for six: sex. -@_=grep{@_[ map $x*$_,//..@_/($x=$_) ] =0 if $_[$_] > 1}@_=0..54; +#This part determines the primes from 0 to 54 using sieve of eratosthenes +#Multiple of each current value (from 0..54) are set to 0 leaving only those +#which are not (Primes) +@_=grep{@_[ map $x*$_,$_..@_/($x=$_) ] =0 if $_[$_] > 1}@_=0..54; + +#The ocde below go through the primes generated above and print combinations +#which have difference of +6 map { ($_[$_]-$_[$'] == 6) && print "$_[$'] $_[$_]\n" for $_+//..$#_ } 0..$#_; + +#My love for obfuscated code strikes again, sorry ;) diff --git a/challenge-022/yet-ebreo/perl5/ch-2.pl b/challenge-022/yet-ebreo/perl5/ch-2.pl index 3d96301c0b..9ad27f72b5 100644 --- a/challenge-022/yet-ebreo/perl5/ch-2.pl +++ b/challenge-022/yet-ebreo/perl5/ch-2.pl @@ -10,21 +10,24 @@ sub encode { my $string = pop; my $len = length $string; + #Initialize the dictionary to hash my $dict_size = INIT_DICT_SIZE; - my %dictionary = map { chr($_) => $_ }0..$dict_size; - - my $bin_buff = ""; + my %dictionary = map { chr $_ => $_ } 0..$dict_size; + + #Initialize variables my @buff_out = (); my $pos = 0; my $output = ""; my $l = ""; my $v = ""; + #Go through the characters and build-up the $dictionary while ($pos<$len) { my $incr = 1; for my $x ($pos+1..$len) { - $l = substr $string, $pos, $x-$pos; - + $l = substr $string, $pos, $x-$pos; + + #Check if current L value is in the $dictionary if (exists $dictionary{$l}) { $v = $dictionary{$l}; @@ -34,7 +37,7 @@ sub encode { } } else { $incr = $x - $pos-1; - + #Store last value to @buff_out and update the $dictionary push @buff_out, $v; $dictionary{$l} = ++$dict_size; last; @@ -47,29 +50,45 @@ sub encode { sub decode { my @buff_in = @{+pop}; + + #Initialize dictionary using numbers as keys + #Using array would work too my %dictionary = map { $_ => chr }0..INIT_DICT_SIZE; - my $old = $buff_in[0]; - my $n = ""; - my $s = $dictionary{$old}; + #Initialize the first value from @buff_in to $o + my $o = $buff_in[0]; + my $n = ""; + my $s = $dictionary{$o}; my $c = substr $s, 0, 1; my $ret = $s; + + my $count = INIT_DICT_SIZE; + #Go through the codes in @buff_in for (my $i =0; $i < $#buff_in;$i++) { + + #Store the $n-ext value $n = $buff_in[$i+1]; - if ( !exists $dictionary{$n} ) { - $s = $dictionary{$old}; - $s.= $c; - } else { + #Check if $n-ext value is in the dictionary + if ( exists $dictionary{$n} ) { $s = $dictionary{$n}; + } else { + $s = $dictionary{$o}; + $s.= $c; } + + #Update return value $ret .= $s; $c = substr $s, 0, 1; - $dictionary{++$count} = $dictionary{$old}.$c; - $old = $n; + #Update dictionary + $dictionary{++$count} = $dictionary{$o}.$c; + + #Update store $n to $o as + #new value becomes old and be replaced on next iteration + $o = $n; } return $ret; } @@ -87,8 +106,3 @@ print ("Uncompressed String has length ". (length $uncompressed)."\n"); print "$uncompressed\n\n"; print "Uncompressed string matches original\n\n" if $uncompressed eq $string; - - - - - |
