From b690a2d14d660bdc88bc1e9fde871ddbbbf71f4b Mon Sep 17 00:00:00 2001 From: Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> Date: Sun, 14 Jul 2019 23:02:35 -0400 Subject: replace prior PR my previous PR had some changes from the original code you'd seen, mohammad. you tossed that PR because you thought it was the same. this PR merges those changes back in. --- challenge-015/michael-hamlin/perl5/GetPrime.pm | 4 +- challenge-015/michael-hamlin/perl5/Vigenere.pm | 67 ++++++++++++++++++++++++++ challenge-015/michael-hamlin/perl5/Vignere.pm | 66 ------------------------- challenge-015/michael-hamlin/perl5/ch-1.pl | 29 +---------- challenge-015/michael-hamlin/perl5/ch-2.pl | 24 +-------- challenge-015/michael-hamlin/perl5/task1.txt | 15 ++++++ challenge-015/michael-hamlin/perl5/task2.txt | 2 + challenge-016/michael-hamlin/perl5/ch-1.pl | 33 +++++++++++++ challenge-016/michael-hamlin/perl5/task1.txt | 6 +++ 9 files changed, 128 insertions(+), 118 deletions(-) create mode 100644 challenge-015/michael-hamlin/perl5/Vigenere.pm delete mode 100644 challenge-015/michael-hamlin/perl5/Vignere.pm mode change 100644 => 120000 challenge-015/michael-hamlin/perl5/ch-1.pl mode change 100644 => 120000 challenge-015/michael-hamlin/perl5/ch-2.pl create mode 100644 challenge-015/michael-hamlin/perl5/task1.txt create mode 100644 challenge-015/michael-hamlin/perl5/task2.txt create mode 100644 challenge-016/michael-hamlin/perl5/ch-1.pl create mode 100644 challenge-016/michael-hamlin/perl5/task1.txt diff --git a/challenge-015/michael-hamlin/perl5/GetPrime.pm b/challenge-015/michael-hamlin/perl5/GetPrime.pm index 17559ce4ea..9812b88ed2 100644 --- a/challenge-015/michael-hamlin/perl5/GetPrime.pm +++ b/challenge-015/michael-hamlin/perl5/GetPrime.pm @@ -13,7 +13,7 @@ use integer; my @primes = (2, 3, 5); sub getprime ($) { - my $n = shift || die "must give an ordinal number to getprime"; + my $n = shift || die "must give an ordinal integer to getprime"; push @primes, _nextprime() while @primes < $n; return $primes[$n - 1]; } @@ -41,8 +41,10 @@ sub _hasfactor { 1; __END__ + #foreach (1..20) { # printf "%2u: %u\n", $_, getprime($_); #} + say 'the hundredth prime is ', getprime(100); diff --git a/challenge-015/michael-hamlin/perl5/Vigenere.pm b/challenge-015/michael-hamlin/perl5/Vigenere.pm new file mode 100644 index 0000000000..bc54f2d23b --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/Vigenere.pm @@ -0,0 +1,67 @@ +package Vigenere; + +# implement the cipher of the same name. + +use 5.18.0; +use parent 'Exporter'; +our @EXPORT = qw; + +use integer; +use List::SomeUtils qw; + +my $basis = join '', 'A' .. 'Z'; +my $blength = length $basis; + +our $debug = 0; + +sub _normalize { + return unless my $plaintext = uc(shift); + $plaintext =~ tr/A-Z//cd; + return $plaintext; +} +sub _ordify { + return map {ord($_) - 65} split //, shift; +} +sub _stringify { + return join '', map {chr($_ + 65)} @_; +} +sub _keyshifts { + my ($key, $len) = @_; + my @rots = _ordify($key); + push @rots, @rots while @rots < $len; + splice(@rots, $len); + return @rots; +} + + +sub vencode ($@) { + my $msg = _normalize(shift) || die "cipher can only encode ascii letters"; + while (my $key = _normalize(shift)) { + say "key: [$key]" if $debug; + my @mchars = _ordify($msg); + my @rots = _keyshifts($key, length($msg)); + my @res = pairwise {($a + $b) % $blength} @mchars, @rots; + $msg = _stringify(@res); + if ($debug) { + say "msg @mchars"; + say "rot @rots"; + say "res @res"; + } + } + return $msg; +} + +sub vdecode ($@) { + my $msg = shift; + die "some chars in ciphertext cannot have been encoded" if $msg =~ tr/A-Z/ /c; + while (my $key = _normalize(shift)) { + my @mchars = _ordify($msg); + my @rots = _keyshifts($key, length($msg)); + my @res = pairwise {($blength + $a - $b) % $blength} @mchars, @rots; + $msg = _stringify(@res); + } + return $msg; +} + +1; + diff --git a/challenge-015/michael-hamlin/perl5/Vignere.pm b/challenge-015/michael-hamlin/perl5/Vignere.pm deleted file mode 100644 index 494c322f5a..0000000000 --- a/challenge-015/michael-hamlin/perl5/Vignere.pm +++ /dev/null @@ -1,66 +0,0 @@ -package Vigenere; - -# implement the cipher of the same name. - -use 5.18.0; -use parent 'Exporter'; -our @EXPORT = qw; - -use integer; -use List::SomeUtils qw; - -my $basis = join '', 'A' .. 'Z'; -my $blength = length $basis; - -our $debug = 0; - -sub _normalize { - return unless my $plaintext = uc(shift); - $plaintext =~ tr/A-Z//cd; - return $plaintext; -} -sub _ordify { - return map {ord($_) - 65} split //, shift; -} -sub _stringify { - return join '', map {chr($_ + 65)} @_; -} -sub _keyshifts { - my ($key, $len) = @_; - my @rots = _ordify($key); - push @rots, @rots while @rots < $len; - splice(@rots, $len); - return @rots; -} - - -sub vencode ($@) { - my $msg = _normalize(shift) || die "cipher can only encode ascii letters"; - while (my $key = _normalize(shift)) { - say "key: [$key]" if $debug; - my @mchars = _ordify($msg); - my @rots = _keyshifts($key, length($msg)); - my @res = pairwise {($a + $b) % $blength} @mchars, @rots; - $msg = _stringify(@res); - if ($debug) { - say "msg @mchars"; - say "rot @rots"; - say "res @res"; - } - } - return $msg; -} - -sub vdecode ($@) { - my $msg = shift; - die "some chars in ciphertext cannot have been encoded" if $msg =~ tr/A-Z/ /c; - while (my $key = _normalize(shift)) { - my @mchars = _ordify($msg); - my @rots = _keyshifts($key, length($msg)); - my @res = pairwise {($blength + $a - $b) % $blength} @mchars, @rots; - $msg = _stringify(@res); - } - return $msg; -} -1; - diff --git a/challenge-015/michael-hamlin/perl5/ch-1.pl b/challenge-015/michael-hamlin/perl5/ch-1.pl deleted file mode 100644 index bbbf14099c..0000000000 --- a/challenge-015/michael-hamlin/perl5/ch-1.pl +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/env perl - -use 5.18.0; -use GetPrime; -use integer; - -my $n = shift @ARGV || 10; - -my (@strong, @weak); - -for (my $i = 2; @strong < $n && @weak < $n; $i++) { - my $prime = getprime($i); - # avoiding division for speed - - my $doubled = 2 * $prime; # saves an op later - my $neighbor_sum = getprime($i-1) + getprime($i+1); - - if ($doubled > $neighbor_sum) { - push @strong, $prime; - } elsif ($doubled < $neighbor_sum) { - push @weak, $prime; - } else { - say "zomg prime #$i ($prime) is neither!" -} -} - -say "Strongs are: @strong"; -say "Weaks are: @weak"; diff --git a/challenge-015/michael-hamlin/perl5/ch-1.pl b/challenge-015/michael-hamlin/perl5/ch-1.pl new file mode 120000 index 0000000000..97193b3d16 --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/ch-1.pl @@ -0,0 +1 @@ +t1-prime-types.pl \ No newline at end of file diff --git a/challenge-015/michael-hamlin/perl5/ch-2.pl b/challenge-015/michael-hamlin/perl5/ch-2.pl deleted file mode 100644 index 9d30e5e5a1..0000000000 --- a/challenge-015/michael-hamlin/perl5/ch-2.pl +++ /dev/null @@ -1,23 +0,0 @@ -#! /usr/bin/env perl - -use 5.18.0; -use Vigenere; - -use Getopt::Long; - -my %opt; -GetOptions(\%opt, - 'key|k=s@', - 'msg|m=s', -); - -my $plaintext = $opt{msg} || 'attack at dawn'; -my @keys = $opt{key} ? @{ $opt{key} } : 'lemon'; - -my $cipher = vencode($plaintext, @keys); -my $decoded = vdecode($cipher, reverse @key -s); - -print "ciphertext: $cipher\n"; -print "decoded: $decoded\n"; - diff --git a/challenge-015/michael-hamlin/perl5/ch-2.pl b/challenge-015/michael-hamlin/perl5/ch-2.pl new file mode 120000 index 0000000000..036430d10c --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/ch-2.pl @@ -0,0 +1 @@ +t2-vig-cipher.pl \ No newline at end of file diff --git a/challenge-015/michael-hamlin/perl5/task1.txt b/challenge-015/michael-hamlin/perl5/task1.txt new file mode 100644 index 0000000000..50cc038091 --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/task1.txt @@ -0,0 +1,15 @@ + + + Write a script to generate first 10 strong and weak prime numbers. + + For example, the nth prime number is represented by p(n). + + p(1) = 2 + p(2) = 3 + p(3) = 5 + p(4) = 7 + p(5) = 11 + + Strong Prime number p(n) when p(n) > [ p(n-1) + p(n+1) ] / 2 + Weak Prime number p(n) when p(n) < [ p(n-1) + p(n+1) ] / 2 + diff --git a/challenge-015/michael-hamlin/perl5/task2.txt b/challenge-015/michael-hamlin/perl5/task2.txt new file mode 100644 index 0000000000..1c00f90e2b --- /dev/null +++ b/challenge-015/michael-hamlin/perl5/task2.txt @@ -0,0 +1,2 @@ +Write a script to implement Vigenère cipher. The script should be able encode and decode. Checkout wiki page for more information. +https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher diff --git a/challenge-016/michael-hamlin/perl5/ch-1.pl b/challenge-016/michael-hamlin/perl5/ch-1.pl new file mode 100644 index 0000000000..f172617f68 --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/env perl +# +use 5.18.0; +use List::Util qw; +use Getopt::Std; + +my %opt; +getopts('v', \%opt); + +# each piece is a percent. the sum must equal 100. +my @pieces; +my $biggest; + +# given the conditions, and the nature of the problem, +# we can surmise the function must have a single maximum. +# with this knowledge, we can skip calculating once we +# pass the largest value. + +my $remaining = 100; +for my $p (1..100) { + my $this = $remaining * $p/100; + printf "piece %3u: %.2f\n", $p, $this if $opt{v}; + push @pieces, $this; + $remaining -= $this; + # once we passed the peak, we can stop + last if $biggest && $this < $pieces[$biggest - 1]; + # otherwise we just found a new maximum: + $biggest = $p; +} + +printf "biggest was #%2u, at %.2f of the pie\n", $biggest, $pieces[$biggest - 1]; +# say 'checking our math, sum is ', List::Util::sum @pieces if $opt{v}; + diff --git a/challenge-016/michael-hamlin/perl5/task1.txt b/challenge-016/michael-hamlin/perl5/task1.txt new file mode 100644 index 0000000000..d2b904c7ed --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/task1.txt @@ -0,0 +1,6 @@ + + Pythagoras Pie Puzzle, proposed by Jo Christian Oterhals. + + At a party a pie is to be shared by 100 guest. The first guest gets 1% of the pie, the second guest gets 2% of the remaining pie, the third gets 3% of the remaining pie, the fourth gets 4% and so on. + + Write a script that figures out which guest gets the largest piece of pie. -- cgit