aboutsummaryrefslogtreecommitdiff
path: root/challenge-113
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-18 16:36:14 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-18 16:36:14 +0100
commit28e8ae0a51e27c2435ca1e8fc16f4eea32a78b05 (patch)
treee95e8f535b4fdc5fae55613c1910fb80bb01c3ae /challenge-113
parent9a948f1a8f9bbe8d5c66dc3730732b492454f8fa (diff)
downloadperlweeklychallenge-club-28e8ae0a51e27c2435ca1e8fc16f4eea32a78b05.tar.gz
perlweeklychallenge-club-28e8ae0a51e27c2435ca1e8fc16f4eea32a78b05.tar.bz2
perlweeklychallenge-club-28e8ae0a51e27c2435ca1e8fc16f4eea32a78b05.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-113')
-rw-r--r--challenge-113/ulrich-rieke/perl/ch-1.pl43
-rw-r--r--challenge-113/ulrich-rieke/raku/ch-1.raku39
-rw-r--r--challenge-113/ulrich-rieke/raku/ch-2.raku43
3 files changed, 125 insertions, 0 deletions
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 ;