diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-08-25 18:40:35 -0400 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2019-08-25 18:40:35 -0400 |
| commit | 3eddc652fb3a10e5d44f6dac5679bd5f0852885a (patch) | |
| tree | f8a3615e0c64863604a79e707bb81d02c2ca44ec /challenge-022 | |
| parent | 5fc80ca35a61b7e84529f422ba4101ed3aae9e70 (diff) | |
| download | perlweeklychallenge-club-3eddc652fb3a10e5d44f6dac5679bd5f0852885a.tar.gz perlweeklychallenge-club-3eddc652fb3a10e5d44f6dac5679bd5f0852885a.tar.bz2 perlweeklychallenge-club-3eddc652fb3a10e5d44f6dac5679bd5f0852885a.zip | |
Challenge 022 by Jaldhar H. Vyas
Diffstat (limited to 'challenge-022')
| -rw-r--r-- | challenge-022/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-022/jaldhar-h-vyas/perl5/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-022/jaldhar-h-vyas/perl5/ch-2.pl | 66 | ||||
| -rwxr-xr-x | challenge-022/jaldhar-h-vyas/perl6/ch-1.sh | 1 | ||||
| -rwxr-xr-x | challenge-022/jaldhar-h-vyas/perl6/ch-2.p6 | 59 |
5 files changed, 174 insertions, 0 deletions
diff --git a/challenge-022/jaldhar-h-vyas/blog.txt b/challenge-022/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..859f5ac2c4 --- /dev/null +++ b/challenge-022/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2019/08/perl_weekly_challenge_week_22.html
\ No newline at end of file diff --git a/challenge-022/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-022/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..7adb83f3ca --- /dev/null +++ b/challenge-022/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub isPrime { + my ($n) = @_; + + if ($n < 2) { + return undef; + } + + if ($n == 2) { + return 1; + } + + for my $i (2 .. sqrt($n)) { + if ($n % $i == 0) { + return undef; + } + } + + return 1; +} + +sub nextPrime { + state $i = 1; + + while ($i++) { + if (isPrime($i)) { + return $i; + } + } +} + +my @sexyPrimes; + +while (scalar @sexyPrimes < 10) { + my $p = nextPrime(); + if (isPrime($p + 6)) { + push @sexyPrimes, [$p, $p + 6]; + } +} + +for my $sp (@sexyPrimes) { + say "$sp->[0], $sp->[1]"; +}
\ No newline at end of file diff --git a/challenge-022/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-022/jaldhar-h-vyas/perl5/ch-2.pl new file mode 100755 index 0000000000..65eb255eaa --- /dev/null +++ b/challenge-022/jaldhar-h-vyas/perl5/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.018; +use English qw/ -no_match_vars /; + +sub init_dict { + my %dictionary = map { chr $_ => chr $_ } 0 .. 255; + return \%dictionary; +} + +sub uncompress { + my ($data) = @_; + my @output; + my $dict = init_dict(); + my $i = scalar keys %{$dict}; + my $l = 0; + my $p = $data->[$l]; + my $s = $dict->{$p}; + my $c = substr $s, 0, 1; + + push @output, $s; + + while ($l < scalar @{$data} - 1) { + my $n = $data->[++$l]; + if (!exists $dict->{$n}) { + $s = "$dict->{$p}$c"; + } else { + $s = $dict->{$n}; + } + push @output, $s; + $c = substr $s, 0, 1; + $dict->{++$i} = "$dict->{$p}$c"; + $p = $n; + } + + return join q{}, @output; +} + +sub compress { + my ($data) = @_; + my @output; + my $dict = init_dict(); + my $i = scalar keys %{$dict}; + my $l = 0; + my $p = substr $data, $l, 1; + + while ($l < length $data) { + my $c = substr $data, ++$l, 1; + if (exists $dict->{"$p$c"}) { + $p = "$p$c"; + } else { + push @output, $dict->{$p}; + $dict->{"$p$c"} = ++$i; + $p = $c; + } + } + + push @output, $dict->{$p}; + + return \@output; +} + +my $phrase = 'TOBEORNOTTOBEORTOBEORNOT'; +# There and back again... +say (($phrase eq uncompress(compress($phrase))) ? 'OK' : 'NOK');
\ No newline at end of file diff --git a/challenge-022/jaldhar-h-vyas/perl6/ch-1.sh b/challenge-022/jaldhar-h-vyas/perl6/ch-1.sh new file mode 100755 index 0000000000..dff39374bc --- /dev/null +++ b/challenge-022/jaldhar-h-vyas/perl6/ch-1.sh @@ -0,0 +1 @@ +perl6 -e '(1..∞).grep({.is-prime}).map({($_,$_+6) if ($_+6).is-prime})[^10].map({.join(q{, }).say});' diff --git a/challenge-022/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-022/jaldhar-h-vyas/perl6/ch-2.p6 new file mode 100755 index 0000000000..9d553fff00 --- /dev/null +++ b/challenge-022/jaldhar-h-vyas/perl6/ch-2.p6 @@ -0,0 +1,59 @@ +#!/usr/bin/perl6 + +sub init_dict { + return (0 .. 255).map({ $_.chr => $_.chr}); +} + +sub uncompress(@data) { + my @output; + my %dict = init_dict(); + my $i = %dict.elems; + my $l = 0; + my $p = @data[$l]; + my $s = %dict{$p}; + my $c = $s.substr(0, 1); + + @output.push($s); + + while $l < @data.elems - 1 { + my $n = @data[++$l]; + if %dict{$n}:!exists { + $s = "%dict{$p}$c"; + } else { + $s = %dict{$n}; + } + @output.push($s); + $c = $s.substr(0, 1); + %dict{++$i} = "%dict{$p}$c"; + $p = $n; + } + + return @output.join(q{}); +} + +sub compress(Str $data) { + my @output; + my %dict = init_dict(); + my $i = %dict.elems; + my $l = 0; + my $p = $data.substr($l, 1); + + while $l < $data.chars { + my $c = $data.substr( ++$l, 1); + if %dict{"$p$c"}:exists { + $p = "$p$c"; + } else { + @output.push(%dict{$p}); + %dict{"$p$c"} = ++$i; + $p = $c; + } + } + + @output.push(%dict{$p}); + + return @output; +} + +my $phrase = 'TOBEORNOTTOBEORTOBEORNOT'; +# There and back again... +say (($phrase eq uncompress(compress($phrase))) ?? 'OK' !! 'NOK');
\ No newline at end of file |
