diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-01 01:41:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-01 01:41:32 +0100 |
| commit | e139a8baa5dae90c9ebc26cb711b81fed66674e9 (patch) | |
| tree | aba820a188216301aefc8d348650069b4922b23b | |
| parent | c063067c7d05ec2be0d79774a7ec663f2af38305 (diff) | |
| parent | a987f049bd2cea92b7e8bd7ca05d732068b69f67 (diff) | |
| download | perlweeklychallenge-club-e139a8baa5dae90c9ebc26cb711b81fed66674e9.tar.gz perlweeklychallenge-club-e139a8baa5dae90c9ebc26cb711b81fed66674e9.tar.bz2 perlweeklychallenge-club-e139a8baa5dae90c9ebc26cb711b81fed66674e9.zip | |
Merge pull request #2420 from jeongoon/master
[ch-080/jeongoon] Perl, Raku, Haskell Solution added.
| -rw-r--r-- | challenge-080/jeongoon/haskell/ch-1.hs | 20 | ||||
| -rw-r--r-- | challenge-080/jeongoon/haskell/ch-2.hs | 26 | ||||
| -rw-r--r-- | challenge-080/jeongoon/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-080/jeongoon/perl/ch-2.pl | 53 | ||||
| -rw-r--r-- | challenge-080/jeongoon/raku/ch-1.raku | 4 | ||||
| -rw-r--r-- | challenge-080/jeongoon/raku/ch-2.raku | 4 |
6 files changed, 142 insertions, 0 deletions
diff --git a/challenge-080/jeongoon/haskell/ch-1.hs b/challenge-080/jeongoon/haskell/ch-1.hs new file mode 100644 index 0000000000..7809ab05b8 --- /dev/null +++ b/challenge-080/jeongoon/haskell/ch-1.hs @@ -0,0 +1,20 @@ +import System.Environment +import System.Exit +import Data.Char (isNumber) +import Data.Maybe (isJust, catMaybes) +import qualified Data.Set as Set + +-- https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/#TASK1 +{- tested with: +runhaskell ch-1.hs 5 -2 2 0 +-} + +smallestPostiveNumber ints = -- result "1" with empty list + (head.dropWhile (isJust.((flip Set.lookupIndex) (Set.fromList ints)))) [1..] + +main = do + nums <- (catMaybes.map (\nStr -> + if (all isNumber nStr) then Just(read nStr :: Int) + else Nothing )) `fmap` getArgs; + if length nums == 0 then putStrLn "runhaskell ch-1.hs <integer> ..." + else (putStrLn.show) $ smallestPostiveNumber nums diff --git a/challenge-080/jeongoon/haskell/ch-2.hs b/challenge-080/jeongoon/haskell/ch-2.hs new file mode 100644 index 0000000000..10fd60cec0 --- /dev/null +++ b/challenge-080/jeongoon/haskell/ch-2.hs @@ -0,0 +1,26 @@ +import System.Environment +import System.Exit +import Data.List (zipWith) +import Data.Char (isNumber) +import Data.Maybe (isJust, catMaybes) + +-- https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/#TASK2 +{- tested with: +runhaskell ch-2.hs 1 4 3 2 +-} + +countCandies [] = 0 +countCandies ranks = defaultCandies + extraCandies + where + leftGroup = init ranks + rightGroup = tail ranks + defaultCandies = length ranks + extraCandies = + sum $ zipWith (\l r -> if l /= r then 1 else 0) leftGroup rightGroup + +main = do + nums <- (catMaybes.map (\nStr -> + if (all isNumber nStr) then Just(read nStr :: Int) + else Nothing )) `fmap` getArgs; + if length nums == 0 then putStrLn "runhaskell ch-1.hs <unsigned integer> ..." + else (putStrLn.show) $ countCandies nums 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 ); diff --git a/challenge-080/jeongoon/raku/ch-1.raku b/challenge-080/jeongoon/raku/ch-1.raku new file mode 100644 index 0000000000..009beb0383 --- /dev/null +++ b/challenge-080/jeongoon/raku/ch-1.raku @@ -0,0 +1,4 @@ +# https://dev.to/jeongoon/weekly-challenge-080-2kg9 +# test with: jeongoon/raku/ch-1.raku 5 2 -2 0 +[1..∞].first({@*ARGS.Set∌$_}).say +#sub MAIN{say((|@_,0,Inf).sort.rotor(2=>-1).first({.[0]>-1>[-] $_})[0]+1)} diff --git a/challenge-080/jeongoon/raku/ch-2.raku b/challenge-080/jeongoon/raku/ch-2.raku new file mode 100644 index 0000000000..babea151c7 --- /dev/null +++ b/challenge-080/jeongoon/raku/ch-2.raku @@ -0,0 +1,4 @@ +# https://dev.to/jeongoon/weekly-challenge-080-task-2-kkj +# test with: raku jeongoon/raku/ch-2.raku 1 4 3 2 +#sub MAIN{(|@_,|((|@_,|@_.reverse).rotor(2=>-1).grep({[<] $_}))).elems.say} +sub MAIN{(|@_,|(|@_.rotor(2=>-1).grep({[!=] $_}))).elems.say} |
