diff options
| author | dms061 <dms7225@psu.edu> | 2021-05-16 18:47:40 -0400 |
|---|---|---|
| committer | dms061 <dms7225@psu.edu> | 2021-05-16 18:47:40 -0400 |
| commit | 0602d0d635835efc141bf1a89f604cd3156ecd3e (patch) | |
| tree | a3cf4fbbe6f6378b1e7f0180ab722010493d6c7d /challenge-112/stuart-little | |
| parent | 111673b82066733c69a62c8f1030da605767aaf8 (diff) | |
| parent | fa969a62c402d6220e260e0f302c80e9b6133c90 (diff) | |
| download | perlweeklychallenge-club-0602d0d635835efc141bf1a89f604cd3156ecd3e.tar.gz perlweeklychallenge-club-0602d0d635835efc141bf1a89f604cd3156ecd3e.tar.bz2 perlweeklychallenge-club-0602d0d635835efc141bf1a89f604cd3156ecd3e.zip | |
Merge branch 'manwar:master' into challenge112
Diffstat (limited to 'challenge-112/stuart-little')
| -rwxr-xr-x | challenge-112/stuart-little/haskell/ch-1.hs | 18 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/haskell/ch-2.hs | 18 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/node/ch-1.js | 7 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/node/ch-2.js | 20 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/perl/ch-1.pl | 9 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/python/ch-1.py | 8 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/python/ch-2.py | 21 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/raku/ch-1.p6 | 6 | ||||
| -rwxr-xr-x | challenge-112/stuart-little/raku/ch-2.p6 | 23 |
10 files changed, 158 insertions, 0 deletions
diff --git a/challenge-112/stuart-little/haskell/ch-1.hs b/challenge-112/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..fcb3175e7c --- /dev/null +++ b/challenge-112/stuart-little/haskell/ch-1.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env runghc + +-- run <script> <path> + +import System.Environment (getArgs) +import System.FilePath (normalise) +import Text.RegexPR (subRegexPR) + +normDdots :: String -> String +normDdots pth = head $ dropWhile (\x -> norm1dd x /= x) $ iterate norm1dd pth where + norm1dd = normalise . (subRegexPR "(?<=/)[^/.]+/+\\.\\." "") + +stripTrail :: String -> String +stripTrail s = if l=='/' then init s else s where + l = last s + +main :: IO () +main = getArgs >>= putStrLn . stripTrail . normDdots . normalise . head diff --git a/challenge-112/stuart-little/haskell/ch-2.hs b/challenge-112/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..ef4416b79d --- /dev/null +++ b/challenge-112/stuart-little/haskell/ch-2.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env runghc + +-- run <script> <number> + +import Data.List (intercalate) +import System.Environment (getArgs) + +memoSteps :: Int -> [[Int]] +memoSteps = (map memo [0 ..] !!) + where memo 0 = [[1]] + memo 1 = [[1,1],[2]] + memo n = (map (1:) $ memoSteps (n-1)) ++ (map (2:) $ memoSteps (n-2)) + +main = do + nr <- getArgs >>= return . (read::String->Int) . head + let res = memoSteps (nr-1) + putStrLn $ (show $ length res) ++ "\n" ++ (replicate 12 '-') + putStrLn $ intercalate "\n" $ map unwords $ (map.map) show res diff --git a/challenge-112/stuart-little/node/ch-1.js b/challenge-112/stuart-little/node/ch-1.js new file mode 100755 index 0000000000..2feea0c4b4 --- /dev/null +++ b/challenge-112/stuart-little/node/ch-1.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node + +// run <script> <path> + +const path = require('path'); + +console.log(path.resolve(process.argv[2])) diff --git a/challenge-112/stuart-little/node/ch-2.js b/challenge-112/stuart-little/node/ch-2.js new file mode 100755 index 0000000000..d29b016779 --- /dev/null +++ b/challenge-112/stuart-little/node/ch-2.js @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +// run <script> <number> + +let memo = [ + [[1,],], + [[1,1],[2,]], +]; + +function memoSteps(n) { + if (typeof(memo[n]) === 'undefined') { + memo[n] = [...(memoSteps(n-1).map(x => [1,...x])) , ...(memoSteps(n-2).map(x => [2,...x]))]; + } + return memo[n] +} + +const res = memoSteps(parseInt(process.argv[2])-1); +console.log(`${res.length} +${"-".repeat(12)}`); +res.forEach(x => console.log(x)); diff --git a/challenge-112/stuart-little/perl/ch-1.pl b/challenge-112/stuart-little/perl/ch-1.pl new file mode 100755 index 0000000000..d43271e650 --- /dev/null +++ b/challenge-112/stuart-little/perl/ch-1.pl @@ -0,0 +1,9 @@ +#!/usr/bin/perl +use warnings; +use v5.12; + +# run <script> <path> + +use Path::Resolve; + +say Path::Resolve->new()->resolve($ARGV[0]); diff --git a/challenge-112/stuart-little/perl/ch-2.pl b/challenge-112/stuart-little/perl/ch-2.pl new file mode 100755 index 0000000000..78e986f4c3 --- /dev/null +++ b/challenge-112/stuart-little/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use v5.12; + +# run <script> <number> + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +my %memo=( + 1 => [[1,],], + 2 => [[1,1],[2,]] +); + +sub memoSteps($n) { + (! exists $memo{$n}) && do { + my @prevTot = ((map {my @ar = (1,@{$_}); \@ar} @{memoSteps($n-1)}), (map {my @ar = (2,@{$_}); \@ar} @{memoSteps($n-2)})); + $memo{$n} = \@prevTot; + }; + return $memo{$n}; +} + +my @res = @{memoSteps($ARGV[0])}; +say scalar @res, "\n", "-" x 10; + +for (@res) { + say "@{$_}"; +} diff --git a/challenge-112/stuart-little/python/ch-1.py b/challenge-112/stuart-little/python/ch-1.py new file mode 100755 index 0000000000..64f5a02c1d --- /dev/null +++ b/challenge-112/stuart-little/python/ch-1.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +# run <script> <path> + +import os +import sys + +print(os.path.normpath(sys.argv[1])) diff --git a/challenge-112/stuart-little/python/ch-2.py b/challenge-112/stuart-little/python/ch-2.py new file mode 100755 index 0000000000..041baec0a0 --- /dev/null +++ b/challenge-112/stuart-little/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# run <script> <number> + +import sys + +memo = [ + [[1,],], + [[1,1],[2,]], +] + +def memoSteps(n): + if (n > len(memo)-1): + memo.append([*(map(lambda a: [1,*a], memoSteps(n-1))),*(map(lambda a: [2,*a], memoSteps(n-2)))]) + return memo[n] + +res = memoSteps(int(sys.argv[1])-1) +print(f"""{len(res)} +{'-' * 12}""") +for ar in res: + print(ar) diff --git a/challenge-112/stuart-little/raku/ch-1.p6 b/challenge-112/stuart-little/raku/ch-1.p6 new file mode 100755 index 0000000000..aa5f7f9434 --- /dev/null +++ b/challenge-112/stuart-little/raku/ch-1.p6 @@ -0,0 +1,6 @@ +#!/usr/bin/env perl6 +use v6; + +# run <script> <path> + +IO::Spec::Unix.canonpath(@*ARGS[0], :parent).say; diff --git a/challenge-112/stuart-little/raku/ch-2.p6 b/challenge-112/stuart-little/raku/ch-2.p6 new file mode 100755 index 0000000000..c4a93ddde8 --- /dev/null +++ b/challenge-112/stuart-little/raku/ch-2.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 +use v6; + +# run <script> <number> + +my %memo=( + 1 => [[1,],], + 2 => [[1,1],[2,]] +); + +sub memoSteps($n) { + (%memo{$n}:!exists) && do { + %memo{$n} = (|memoSteps($n-1).map({ (1,|$_).Array }),|memoSteps($n-2).map({ (2,|$_).Array })).Array; + }; + return %memo{$n}.Array; +} + +my @res = memoSteps(@*ARGS[0].Int); +say @res.elems; + +for (@res) { + .say; +} |
