diff options
| -rw-r--r-- | challenge-201/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-201/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-201/jeanluc2020/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-201/jeanluc2020/perl/ch-2.pl | 73 |
4 files changed, 139 insertions, 0 deletions
diff --git a/challenge-201/jeanluc2020/blog-1.txt b/challenge-201/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..86dbaaa92f --- /dev/null +++ b/challenge-201/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-201-1.html diff --git a/challenge-201/jeanluc2020/blog-2.txt b/challenge-201/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..f2faf48735 --- /dev/null +++ b/challenge-201/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-201-2.html diff --git a/challenge-201/jeanluc2020/perl/ch-1.pl b/challenge-201/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..d3088aac9d --- /dev/null +++ b/challenge-201/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-201/#TASK1 +# +# You are given an array of unique numbers. +# +# Write a script to find out all missing numbers in the range 0..$n where $n is the array size. +# +## Example 1 +## +## Input: @array = (0,1,3) +## Output: 2 +## +## The array size i.e. total element count is 3, so the range is 0..3. +## The missing number is 2 in the given array. +# +## Example 2 +## +## Input: @array = (0,1) +## Output: 2 +## +## The array size is 2, therefore the range is 0..2. +## The missing number is 2. +# +##################################################################### +## +## discussion +## +##################################################################### +# +# this is basically a loop from 0..$n and printing all the numbers that +# are not in the array. +# Note that the task doesn't state that all numbers in the array have to +# be in the range 0..$n, just the missing numbers are. So we could also +# have an array like (0,2,4,8) where $n = 4, but 8 is clearly not in the +# range 0..4, so we would get 1 and 3 as output + +use strict; +use warnings; + +my @examples = ( + [0, 1, 3], + [0, 1], + [0, 2, 4, 8], + [0, 2, 4, 6, 8, 10] +); + +foreach my $array (@examples) { + # let's print the input array + print "(" . join(", ", @$array) . "):\n"; + # for each element in the array create a corresponding + # entry in the %seen hash + my %seen = map { $_ => 1, } @$array; + my @result; + foreach my $i (0..scalar(@$array)) { + # the missing elements are the ones not in %seen, + # so add them to the result + push @result, $i unless $seen{$i}; + } + # print the result + print "Output: " . join(", ", @result) . "\n"; +} + + diff --git a/challenge-201/jeanluc2020/perl/ch-2.pl b/challenge-201/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..87a56d7203 --- /dev/null +++ b/challenge-201/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-201/#TASK2 +# +# You are given an integer, $n > 0. +# +# Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. +# +## Example +## +## Input: $n = 5 +## Output: 7 +## +## Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +## +## 1 1 1 1 1 +## 1 1 1 2 +## 1 2 2 +## 1 1 3 +## 2 3 +## 1 4 +## 5 +# +###################################################################### +## +## discussion +## +###################################################################### +# +# we can find all ways of stacking $n pennies by creating the right-most +# pile first, going from 1..$n +# then we create all solutions for the remaining pennies but with the constraint +# that the biggest pile can only be as big as the one we already built. To each +# of all these solutions we add our right-most pile and return them. +# + +use strict; +use warnings; +use feature 'say'; + +my @examples = (1, 2, 3, 4, 5, 10, 20); + +foreach my $example (@examples) { + my @solutions = find_solutions($example); + say scalar(@solutions) . " solutions found:"; + foreach my $solution (@solutions) { + say join(" ", @$solution); + } +} + +# our recursive function +sub find_solutions { + my ($n, $max_pile_size) = @_; + # if no maximum pile size was given use $n as fallback + $max_pile_size //= $n; + # limit $max_pile_size to $n + $max_pile_size = $n if $max_pile_size > $n; + my @solutions; + # There is only an empty solution if $n is 0 + return ([]) unless $n > 0; + # now create the last pile + foreach my $last (1..$max_pile_size) { + # create all piles left to the last one + my @sub_solutions = find_solutions($n - $last, $last); + # add the last pile to each of these solutions and add + # the solutions to our return array + foreach my $sub_solution (@sub_solutions) { + push @$sub_solution, $last; + push @solutions, $sub_solution; + } + } + return @solutions; +} |
