diff options
34 files changed, 2337 insertions, 1656 deletions
diff --git a/challenge-113/luca-ferrari/blog-1.txt b/challenge-113/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..334e584517 --- /dev/null +++ b/challenge-113/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/05/18/PerlWeeklyChallenge113.html#task1 diff --git a/challenge-113/luca-ferrari/blog-2.txt b/challenge-113/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..2167806f85 --- /dev/null +++ b/challenge-113/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/05/18/PerlWeeklyChallenge113.html#task2 diff --git a/challenge-113/luca-ferrari/raku/ch-1.p6 b/challenge-113/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..f47a511d54 --- /dev/null +++ b/challenge-113/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,10 @@ +#!raku + +sub MAIN( Int $N where { $N > 0 }, + Int $D where { $D >= 0 && $D.Str.chars == 1 } ) { + given (1 ..^ $N).grep( * ~~ / $D /).sum { + when $N { '1'.say } + default { '0'.say } + } + +} diff --git a/challenge-113/luca-ferrari/raku/ch-2.p6 b/challenge-113/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..e181405599 --- /dev/null +++ b/challenge-113/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,42 @@ +#!raku + +class Node { + has Int $.value; + + has $.left is rw; + has $.right is rw; + + method sum() { + my $sum = $!value; + $sum += $_.sum if ( $_ ) for ( $!left, $!right ); + return $sum; + } + + method map( &block ) { + self.new: value => block( $!value ), + left => $!left.map( &block ), + right => $!right.map( &block ); + } + + method say() { + "{ $!value }".say; + "\t Left = { $!left.value }".say if $!left; + "\t Right = { $!right.value }".say if $!right; + + $!left.say if $!left; + $!right.say if $!right; + } + + +} + +sub MAIN() { + + my $root = Node.new: + value => 1 + , left => Node.new( value => 2, left => Node.new( value => 4, left => Node.new( value => 7 ) ) ) + , right => Node.new( value => 3, left => Node.new( value => 5 ), right => Node.new( value => 6 ) ); + + $root = $root.map: { $root.sum - $_ if $_ }; + $root.say; +} diff --git a/challenge-113/polettix/blog.txt b/challenge-113/polettix/blog.txt new file mode 100644 index 0000000000..aa3dee7d91 --- /dev/null +++ b/challenge-113/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/05/19/pwc113-represent-integer/ diff --git a/challenge-113/polettix/blog1.txt b/challenge-113/polettix/blog1.txt new file mode 100644 index 0000000000..59ad48ecbd --- /dev/null +++ b/challenge-113/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/05/20/pwc113-recreate-binary-tree/ diff --git a/challenge-113/polettix/perl/ch-1.pl b/challenge-113/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..8e2c0058ed --- /dev/null +++ b/challenge-113/polettix/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub represent_integer ($n, $d) { + return 0 if $n < $d; # no point in checking this + return 1 if $n >= 10 * $d; # n * d + (10 * d + i) (i < 9) + return 1 if $n =~ m{$d}mxs; # match one digit + $n -= $d; + while ($n > 0) { + return 1 if represent_integer($n, $d); + $n -= 10; + } + return 0; +} + +my $N = shift || 25; +my $D = shift || 7; +say represent_integer($N, $D) ? 1 : 0; diff --git a/challenge-113/polettix/perl/ch-2.pl b/challenge-113/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..826c5083b8 --- /dev/null +++ b/challenge-113/polettix/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use 5.024; +use warnings; +use experimental qw< postderef signatures >; +no warnings qw< experimental::postderef experimental::signatures >; + +sub recreate_binary_tree ($tree) { + my $sum = 0; + for my $cb ( # do the same loop with different actions inside + sub ($n) { $sum += $n->{value} }, + sub ($n) { $n->{value} = $sum - $n->{value} }, + ) + { + my @queue = ($tree); + while (@queue) { + my $node = shift(@queue) // next; + $cb->($node); + next unless exists $node->{children}; + push @queue, $node->{children}->@*; + } ## end while (@queue) + } ## end for my $cb (sub ($n) { ...}) + return $tree; +} ## end sub recreate_binary_tree ($tree) + +sub node ($value, $left = undef, $right = undef) { + my %retval = (value => $value); + $retval{children} = [$left, $right] + if defined($left) || defined($right); + return \%retval; +} ## end sub node + +sub printout ($root, $indent = 0) { + my $value = defined($root) ? $root->{value} : ''; + say ' ' x $indent, "<$value>"; + printout($_, $indent + 1) for $root->{children}->@*; +} + +# 1 +# / \ +# 2 3 +# / / \ +# 4 5 6 +# \ +# 7 +my $T = + node(1, node(2, node(4, undef, node(7))), node(3, node(5), node(6))); + +printout(recreate_binary_tree($T)); 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 diff --git a/challenge-113/ulrich-rieke/perl/ch-1.pl b/challenge-113/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..347b697412 --- /dev/null +++ b/challenge-113/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( sum ) ; +use Algorithm::Combinatorics qw ( combinations ) ; + +my $N = $ARGV[0] ; +my $D = $ARGV[ 1 ] ; +my @possibleNumbers = grep { $_ =~ /$D/ } (1 .. $N) ; +my $nums = scalar @possibleNumbers ; +if ( $N == $D ) { + say 1 ; + exit( 0 ) ; +} +else { + if ( $nums == 0 or $nums == 1 ) { + say 0 ; + exit( 1 ) ; + } + if ( $nums == 2 ) { + if ( $possibleNumbers[ 0 ] + $possibleNumbers[1] == $N ) { + say 1 ; + exit( 0 ) ; + } + else { + say 0 ; + exit( 1 ) ; + } + } + if ( $nums > 2 ) { + for my $i ( 2 .. $nums ) { + my $iter = combinations( \@possibleNumbers , $i ) ; + while ( my $p = $iter->next ) { + if ( sum( @$p ) == $N ) { + say 1 ; + exit( 0 ) ; + } + } + } + say 0 ; + } +} diff --git a/challenge-113/ulrich-rieke/raku/ch-1.raku b/challenge-113/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..e884adb74a --- /dev/null +++ b/challenge-113/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,39 @@ +use v6 ; + +sub MAIN( Int $N , Int $D ) { + if ( $N == $D ) { + say 1 ; + exit( 1 ) ; + } + else { +#find numbers below $N containing $D + my @possibleNumbers = (1 .. $N ).grep( { ~$_ ~~ /$D/ } ) ; + if (@possibleNumbers.elems == 1 ) { + say 0 ; #no valid sum, only one number contains $D + exit( 0 ) ; + } + if ( @possibleNumbers.elems == 2 ) { + if ( @possibleNumbers[0] + @possibleNumbers[1] == $N ) { + say 1 ; + exit( 0 ) ; + } + else { + say 0 ; + exit( 1 ) ; + } + } + if ( (my $nums = @possibleNumbers.elems) > 2 ) { +#there can be combinations of 2 and more that sum up to $D + for ( 2 .. $nums ) -> $i { + my @combis = @possibleNumbers.combinations($i ) ; + for @combis -> $combi { + if ( ([+] $combi) == $N ) {#valid combination found + say 1 ; + exit( 0 ) ; + } + } + } + } + say 0 ;#nothing found , everything else would have returned already + } +} diff --git a/challenge-113/ulrich-rieke/raku/ch-2.raku b/challenge-113/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..179133b101 --- /dev/null +++ b/challenge-113/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,43 @@ +use v6 ; + +#we enter the binary tree level by level. That means, that at every level +#numbers are ordered from left to right. To denote different parent nodes, +#we introduce subarrays at every level when needed +sub sumElements( $element ) { + my $sum = 0 ; + for (0 .. $element.elems - 1) -> $i { + if ( $element[ $i ] ~~ Int ) { + $sum += $element[ $i ] ; + } + if ( $element[ $i ] ~~ Array ) { + $sum += [+] |$element[ $i ] ; + } + } + return $sum ; +} + +sub calculateElement( $element , $sum ) { + my $output ; + for (0 .. $element.elems - 1 ) -> $i { + if ( $element[ $i ] ~~ Int ) { + $output.push( $sum - $element[ $i ] ) ; + } + if ( $element[ $i ] ~~ Array ) { + my $restsums ; + for (0 .. $element[ $i ].elems - 1) -> $j { + $restsums.push( $sum - $element[$i][$j] ) ; + } + $output.push( $restsums ) ; + } + } + return $output ; +} + +my @binary_input = ( [1] , [2 , 3] , [[4] , [5 , 6 ]] , [7] ) ; +#we substitute every node by the sum of all nodes - the node itself +#due to the rooting and ordering principles of a binary tree this brings +#about a reverse ordering of the tree +my $treesum = 0 ; +@binary_input.map( { $treesum += sumElements( $_ ) } ) ; +my @output_binary = @binary_input.map( { calculateElement( $_ , $treesum) }) ; +say @output_binary ; diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 829c9710c6..098595648f 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -4,25 +4,109 @@ "text" : "Total Solutions" } }, - "tooltip" : { - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", - "followPointer" : 1 + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } + }, + "title" : { + "text" : "Perl Weekly Challenge - 113" }, "chart" : { "type" : "column" }, + "tooltip" : { + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" + }, + "subtitle" : { + "text" : "[Champions: 11] Last updated at 2021-05-18 15:36:02 GMT" + }, + "xAxis" : { + "type" : "category" + }, + "series" : [ + { + "name" : "Perl Weekly Challenge - 113", + "colorByPoint" : 1, + "data" : [ + { + "y" : 2, + "drilldown" : "Ben Davies", + "name" : "Ben Davies" + }, + { + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby", + "y" : 2 + }, + { + "y" : 4, + "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti" + }, + { + "name" : "James Smith", + "y" : 2, + "drilldown" : "James Smith" + }, + { + "name" : "Luca Ferrari", + "y" : 4, + "drilldown" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "y" : 2, + "drilldown" : "Mark Anderson" + }, + { + "y" : 2, + "drilldown" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "y" : 4, + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "drilldown" : "Simon Green", + "y" : 3 + }, + { + "drilldown" : "Stuart Little", + "y" : 4, + "name" : "Stuart Little" + }, + { + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke", + "y" : 3 + } + ] + } + ], + "legend" : { + "enabled" : 0 + }, "drilldown" : { "series" : [ { "name" : "Ben Davies", - "id" : "Ben Davies", "data" : [ [ |
