diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-01 11:24:18 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-01 11:24:18 +1000 |
| commit | a987f049bd2cea92b7e8bd7ca05d732068b69f67 (patch) | |
| tree | f8f8941e6ce447f221ab8b49bcdeb852b11428a7 /challenge-080/jeongoon/perl | |
| parent | a2e2740210f3b090c817e2e06801e36307290cef (diff) | |
| download | perlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.tar.gz perlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.tar.bz2 perlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.zip | |
[ch-080/jeongoon] Perl, Raku, Haskell Solution added.
Diffstat (limited to 'challenge-080/jeongoon/perl')
| -rw-r--r-- | challenge-080/jeongoon/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-080/jeongoon/perl/ch-2.pl | 53 |
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-080/jeongoon/perl/ch-1.pl b/challenge-080/jeongoon/perl/ch-1.pl new file mode 100644 index 0000000000..787384ff8d --- /dev/null +++ b/challenge-080/jeongoon/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use strict; use warnings; +use v5.26; + +# tested with: perl perl/ch-1.pl --debug 5 2 -2 0 + +our $d = 0; + +sub smallestPositiveNumber { + my @unsignedOnly = + sort grep { $_ >= 0 } 0, @_; # add zero(0) to better indexing + say join (", ", @unsignedOnly ) if$d; + + my @set; + $set[$_] = 'set' for @unsignedOnly; + push @set, undef; + if ($d) { + say join(", ", map { "$_ => ".($set[$_] // 'gap') } 0..$#set ); + } + + defined $set[$_] or return $_ for 1..@set; +} + +my @filteredARGV; + +for (@ARGV) { + /^-(d|v|-*debug|-*verbose)$/ and ( $d = 1, next ); + + ( /^(-h|--*help)$/ or ($_+0) ne $_ ) + and say("perl ch-1.pl <interger> ..."), exit 0; + + push @filteredARGV, $_; +} + +say( $d?"therefore: ":"", smallestPositiveNumber( @filteredARGV ) ); diff --git a/challenge-080/jeongoon/perl/ch-2.pl b/challenge-080/jeongoon/perl/ch-2.pl new file mode 100644 index 0000000000..583671ceb0 --- /dev/null +++ b/challenge-080/jeongoon/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +use strict; use warnings; +use v5.26; + +use List::Util qw(sum); + +# tested with: perl perl/ch-2.pl --debug 1 4 3 2 + +our $d = 0; + +sub candies { + my @rank = @_; + say "default candies: ".scalar(@_) if$d; + my @ruleA = @_; + if ( $d ) { + @ruleA + + scalar + ( map { + my @ruleB; + if ( 0 < $_ and $rank[$_-1] < $rank[$_] ) { + say "add 1 more candy". + " as $rank[$_] has higher rank than $rank[$_-1]" if $d; + push @ruleB, 1; + } + if ( ($rank[$_+1]//$rank[$_] ) < $rank[$_] ) { + say "add 1 more candy". + " as $rank[$_] has higher rank than $rank[$_+1]" if $d; + push @ruleB, 1; + } + @ruleB + } 1 .. $#rank ) + } + else { + # https://dev.to/jeongoon/weekly-challenge-080-2n1n + @ruleA + scalar ( grep { $rank[$_-1] != $rank[$_] } 1..$#rank ); + } +} + +my @filteredARGV; +for (@ARGV) { + /^-(d|v|-*debug|-*verbose)$/ and ( $d = 1, next ); + + ( /^(-h|--*help)$/ or ($_+0) ne $_ or $_ < 0 ) + and say("perl ch-1.pl <interger> ..."), exit 0; + + push @filteredARGV, $_; +} + +@filteredARGV < 1 and + ( warn("using default input: '1, 4, 3, 2' ... "), + @filteredARGV = ( 1, 4, 3, 2 )); + +say( ($d?"therefore: ":""), candies @filteredARGV ); |
