aboutsummaryrefslogtreecommitdiff
path: root/challenge-113/stuart-little
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-05-18 22:19:52 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-05-18 22:19:52 +0100
commitd8828a6cfbadb2f453508a4390777534ab4e5dca (patch)
tree777df3bd09f6988996d50aeefe21bd757cf57041 /challenge-113/stuart-little
parentacf7efe3c26276e986c039273f2df7179de5ab24 (diff)
parent28e8ae0a51e27c2435ca1e8fc16f4eea32a78b05 (diff)
downloadperlweeklychallenge-club-d8828a6cfbadb2f453508a4390777534ab4e5dca.tar.gz
perlweeklychallenge-club-d8828a6cfbadb2f453508a4390777534ab4e5dca.tar.bz2
perlweeklychallenge-club-d8828a6cfbadb2f453508a4390777534ab4e5dca.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-113/stuart-little')
-rwxr-xr-xchallenge-113/stuart-little/haskell/ch-1.hs20
-rwxr-xr-xchallenge-113/stuart-little/haskell/ch-2.hs42
-rwxr-xr-xchallenge-113/stuart-little/node/ch-1.js20
-rwxr-xr-xchallenge-113/stuart-little/node/ch-2.js49
-rwxr-xr-xchallenge-113/stuart-little/perl/ch-1.pl23
-rwxr-xr-xchallenge-113/stuart-little/perl/ch-2.pl68
-rwxr-xr-xchallenge-113/stuart-little/python/ch-1.py18
-rwxr-xr-xchallenge-113/stuart-little/python/ch-2.py51
-rwxr-xr-xchallenge-113/stuart-little/raku/ch-1.p621
-rwxr-xr-xchallenge-113/stuart-little/raku/ch-2.p643
10 files changed, 355 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/haskell/ch-1.hs b/challenge-113/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..2de27e6a9b
--- /dev/null
+++ b/challenge-113/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+#!/usr/bin/env runghc
+
+-- run <script> <digit>
+
+import System.Environment (getArgs)
+
+lastDigSumm :: Int -> Int -> Int -> Bool
+lastDigSumm nr dig nrSummands = (mod (nr - nrSummands * dig) 10 == 0) && (nrSummands * dig <= nr) && (nrSummands * ((dig -1) * 10 + dig) >= nr)
+
+lastDig :: Int -> Int -> Bool
+lastDig nr dig = not $ null $ filter (lastDigSumm nr dig) [1..9]
+
+sol :: Int -> Int -> Bool
+sol nr dig
+ |dig == 0 = (nr >= 101) || (mod nr 10 == 0)
+ |otherwise = (nr >= dig*11) || lastDig nr dig
+
+main = do
+ [nr,dig] <- getArgs >>= return . map (read::String->Int) . take 2
+ putStrLn . show . fromEnum $ sol nr dig
diff --git a/challenge-113/stuart-little/haskell/ch-2.hs b/challenge-113/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..62e39d6f99
--- /dev/null
+++ b/challenge-113/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,42 @@
+#!/usr/bin/env runghc
+
+import Data.Char (isDigit)
+import Data.List ((\\), inits)
+import Data.Tree (Tree, drawTree, unfoldTree)
+import System.Environment (getArgs)
+
+sumMinNrs :: [String] -> [String]
+sumMinNrs xs = map (\x -> if (all isDigit x) then show (sm - ((read::String->Int)) x) else x) xs where
+ sm = sum $ map (read::String->Int) $ filter (all isDigit) xs
+
+split2trees :: [String] -> ([String],[String])
+split2trees xs = (lhalf, xs \\ lhalf) where
+ lhalf = head $ dropWhile (\ys -> 2*(length $ filter (\s -> s==".") ys) <= length ys ) $ inits xs
+
+nodeIter :: [String] -> (String, [[String]])
+nodeIter xs = if (head xs == ".") then ("",[]) else (head xs, [lchild,rchild]) where
+ (lchild,rchild) = split2trees $ tail xs
+
+mkTree :: [String] -> Tree String
+mkTree = unfoldTree nodeIter
+
+main = do
+ outTreeList <- getArgs >>= return . sumMinNrs . (\x -> if (not $ null x) then x else ["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."])
+ putStrLn $ drawTree $ mkTree outTreeList
+{--
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2
+--}
diff --git a/challenge-113/stuart-little/node/ch-1.js b/challenge-113/stuart-little/node/ch-1.js
new file mode 100755
index 0000000000..319423ece6
--- /dev/null
+++ b/challenge-113/stuart-little/node/ch-1.js
@@ -0,0 +1,20 @@
+#!/usr/bin/env node
+
+// run <script> <digit>
+
+function lastDigSumm(nr, dig, nrSummands) {
+ return ((nr - nrSummands * dig) % 10 == 0) && (nrSummands * dig <= nr) && (nrSummands * ((dig -1) * 10 + dig) >= nr)
+}
+
+function lastDig(nr,dig) {
+ return [...Array(9)].map((_,i) => i+1).filter(x => lastDigSumm(nr,dig,x)).length >= 1
+}
+
+function sol(nr,dig) {
+ if (dig == 0) {
+ return (nr >= 101 || (nr % 10 == 0))
+ }
+ return ((nr >= dig * 11) || lastDig(nr,dig))
+}
+
+console.log(sol(...process.argv.slice(2).map(x => parseInt(x))) ? 1 : 0)
diff --git a/challenge-113/stuart-little/node/ch-2.js b/challenge-113/stuart-little/node/ch-2.js
new file mode 100755
index 0000000000..2364b0a3a8
--- /dev/null
+++ b/challenge-113/stuart-little/node/ch-2.js
@@ -0,0 +1,49 @@
+#!/usr/bin/env node
+
+const printTree = require('print-tree');
+
+function split2trees(lst) {
+ ix = [...lst.keys()].find(i => 2*lst.slice(0,i+1).filter(x => x === ".").length > i+1 );
+ return [lst.slice(0,ix+1), lst.slice(ix+1)]
+}
+
+function mkTree(lst) {
+ if (lst[0] === ".") {
+ return { name: "" };
+ };
+ const nm = lst[0];
+ const [lft,rght] = split2trees(lst.slice(1));
+ return {
+ name: nm,
+ children: [mkTree(lft),mkTree(rght)],
+ };
+}
+
+const inTreeList = (process.argv.length > 2) ? (process.argv.slice(2)) : (["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."]);
+const sm = inTreeList.reduce((acc,el) => {return acc + (parseInt(el) || 0)}, 0);
+const outTreeList = inTreeList.map(x => (parseInt(x)) ? (sm - parseInt(x)) : x);
+
+printTree(
+ mkTree(outTreeList),
+ node => node.name.toString(),
+ node => node.children,
+);
+
+/*
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2
+*/
+
diff --git a/challenge-113/stuart-little/perl/ch-1.pl b/challenge-113/stuart-little/perl/ch-1.pl
new file mode 100755
index 0000000000..da3e751345
--- /dev/null
+++ b/challenge-113/stuart-little/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+# run <script> <number> <digit>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+sub lastDigSumm($nr,$dig,$nrSummands) {
+ return (($nr - $nrSummands * $dig) % 10 == 0) && ($nrSummands * $dig <= $nr) && ($nrSummands * (($dig -1) * 10 + $dig) >= $nr);
+}
+
+sub lastDig($nr,$dig) {
+ return !!(grep {lastDigSumm($nr,$dig,$_)} (1..9));
+}
+
+sub sol($nr,$dig) {
+ $dig == 0 && return ($nr >= 101 || ($nr % 10 == 0));
+ return (($nr >= $dig * 11) || lastDig($nr,$dig));
+}
+
+say 0+sol(@ARGV);
diff --git a/challenge-113/stuart-little/perl/ch-2.pl b/challenge-113/stuart-little/perl/ch-2.pl
new file mode 100755
index 0000000000..9793a9a623
--- /dev/null
+++ b/challenge-113/stuart-little/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+use List::AllUtils qw(first sum);
+use Tree::DAG_Node;
+
+sub diffIfNum($nr,$str) {
+ return ($str =~ /^\d+$/) ? ($nr - int($str)) : ($str);
+}
+
+sub moreDots($a) {
+ return 2*(scalar grep {$_ eq "."} @{$a}) > (scalar @{$a});
+}
+
+sub ixSplit($a) {
+ return first { my @ar = $a->@[0..$_]; moreDots(\@ar) } (keys @{$a});
+}
+
+sub treeList2Hash($t) {
+ (! scalar @{$t} || $t->[0] eq '.') && return {};
+ my @rest = @{$t}[1..scalar @{$t}-1];
+ my $ix = ixSplit(\@rest);
+ my @left = @rest[0..$ix];
+ my @right = @rest[$ix+1..$#rest];
+ return {
+ name => $t->[0],
+ left => treeList2Hash(\@left),
+ right => treeList2Hash(\@right),
+ };
+}
+
+sub mkDAG($h) {
+ (! scalar keys %{$h}) && return Tree::DAG_Node->new({ name => "" });
+ my $root = Tree::DAG_Node->new({ name => $h->{name} });
+ my %left = %{$h->{left}};
+ $root->add_daughter(mkDAG(\%left));
+ my %right = %{$h->{right}};
+ $root->add_daughter(mkDAG(\%right));
+ return $root;
+}
+
+my @INPUT = (scalar @ARGV) ? (@ARGV) : (qw(1 2 4 . 7 . . . 3 5 . . 6 . .));
+my $sum = sum grep {$_ =~ /^\d+$/} @INPUT;
+my @outTreeList = map {diffIfNum($sum,$_)} @INPUT;
+my $tree = mkDAG(treeList2Hash(\@outTreeList));
+
+print map("$_\n", @{$tree->tree2string({no_attributes => 1})});
+
+__END__
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2
diff --git a/challenge-113/stuart-little/python/ch-1.py b/challenge-113/stuart-little/python/ch-1.py
new file mode 100755
index 0000000000..bbaaf80337
--- /dev/null
+++ b/challenge-113/stuart-little/python/ch-1.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+
+# run <script> <number> <digit>
+
+import sys
+
+def lastDigSumm(nr, dig, nrSummands):
+ return ((nr - nrSummands * dig) % 10 == 0) and (nrSummands * dig <= nr) and (nrSummands * ((dig -1) * 10 + dig) >= nr)
+
+def lastDig(nr,dig):
+ return bool(list(filter(lambda x: lastDigSumm(nr,dig,x), range(1,10))))
+
+def sol(nr,dig):
+ if (dig == 0):
+ return (nr >= 101 or (nr % 10 == 0))
+ return ((nr >= dig * 11) or lastDig(nr,dig));
+
+print(int(sol(*map(int,sys.argv[1:]))))
diff --git a/challenge-113/stuart-little/python/ch-2.py b/challenge-113/stuart-little/python/ch-2.py
new file mode 100755
index 0000000000..593c4cc9a7
--- /dev/null
+++ b/challenge-113/stuart-little/python/ch-2.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+import sys
+from print_tree import print_tree
+
+class Node(object):
+ def __init__(self, value):
+ self.value = value
+ self.children = []
+
+class print_custom_tree(print_tree):
+ def get_children(self, node):
+ return node.children
+ def get_node_str(self, node):
+ return str(node.value)
+
+def moreDots(lst):
+ return lambda i: 2*len(list(filter(lambda x: x == ".",lst[1:i+1]))) > i
+
+def list2node(lst):
+ if len(lst) == 0 or lst[0] == '.':
+ return Node("")
+ ix = next(filter(moreDots(lst), range(0,len(lst))))
+ root=Node(lst[0])
+ lft=list2node(lst[1:ix+1])
+ rght=list2node(lst[ix+1:])
+ root.children=[lft,rght]
+ return root
+
+INPUT = sys.argv[1:] if len(sys.argv) >= 2 else ["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."]
+sm = sum(map(int, filter(lambda x: x.isdigit(), INPUT)))
+outTreeList=list(map(lambda x: str(sm - int(x)) if x.isdigit() else x, INPUT))
+print_custom_tree(list2node(outTreeList))
+
+"""
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2
+"""
diff --git a/challenge-113/stuart-little/raku/ch-1.p6 b/challenge-113/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..2df0b1ccfb
--- /dev/null
+++ b/challenge-113/stuart-little/raku/ch-1.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+# run <script> <number> <digit>
+
+sub lastDigSumm($nr,$dig,$nrSummands) {
+ return (($nr - $nrSummands * $dig) %% 10) && ($nrSummands * $dig <= $nr) && ($nrSummands * (($dig -1) * 10 + $dig) >= $nr);
+}
+
+sub lastDig($nr,$dig) {
+ return (1..9).grep({ lastDigSumm($nr,$dig,$_) }).Bool;
+}
+
+sub sol($nr,$dig) {
+ $dig == 0 && return ($nr >= 101 || ($nr %% 10));
+ return (($nr >= $dig * 11) || lastDig($nr,$dig));
+}
+
+say sol(|@*ARGS.map(*.Int)).Int;
+
+
diff --git a/challenge-113/stuart-little/raku/ch-2.p6 b/challenge-113/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..81d47b2aaf
--- /dev/null
+++ b/challenge-113/stuart-little/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl6
+use v6;
+
+use MONKEY-SEE-NO-EVAL;
+
+sub diffIfNum($nr,$str) {
+ return ($str.Int.defined) ?? (($nr - $str.Int).Str) !! ($str);
+}
+
+sub list2lol(@l) {
+ (@l.elems == 0 || @l[0] eq '.') && return [""];
+ my $ix = @l[1..*].keys.first( -> $i { 2*@l[1..$i+1].grep({ $_ eq '.' }).elems > $i+1});
+ return [list2lol(@l[1..$ix+1]),list2lol(@l[$ix+2..*]),@l[0].Str];
+}
+
+my @INPUT = (@*ARGS.elems) ?? (@*ARGS) !! (<1 2 4 . 7 . . . 3 5 . . 6 . .>);
+my $sum = @INPUT.grep({ $_.Int.defined }).map(*.Int).sum;
+my @outTreeList = @INPUT.map({ diffIfNum($sum,$_) });
+
+my $str=list2lol(@outTreeList).raku;
+EVAL qq!
+use Tree::DAG_Node;
+my \$lol = $str;
+my \$tree=Tree::DAG_Node->lol_to_tree(\$lol);
+print map(\"\$_\\n\", \@\{\$tree->tree2string(\{ no_attributes => 1 \})\});
+!, :lang<Perl5>;
+
+=finish
+run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / / \
+ 4 5 6
+ \
+ 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2