diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-01-23 16:09:47 +0100 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-01-23 16:09:47 +0100 |
| commit | 2ca314cd4cfa147739729b900d2b254834eb6689 (patch) | |
| tree | c46dcc877cd3c5c8bb3729e8bf9be36852a547f2 | |
| parent | 27b88f614b9bb53872ef0da19a56087505836db0 (diff) | |
| download | perlweeklychallenge-club-2ca314cd4cfa147739729b900d2b254834eb6689.tar.gz perlweeklychallenge-club-2ca314cd4cfa147739729b900d2b254834eb6689.tar.bz2 perlweeklychallenge-club-2ca314cd4cfa147739729b900d2b254834eb6689.zip | |
PWC 201
| -rw-r--r-- | challenge-201/spadacciniweb/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-201/spadacciniweb/perl/ch-2.pl | 40 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-201/spadacciniweb/perl/ch-1.pl b/challenge-201/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..a9d1c97263 --- /dev/null +++ b/challenge-201/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental 'smartmatch'; + +# Task 1: Missing Numbers +# Submitted by: Mohammad S Anwar +# +# 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. + + +my @missing = map { $_ ~~ \@ARGV ? () : $_ } + (0..scalar @ARGV); +printf "The missing %s %s\n", + (scalar @missing > 1) ? 'numbers are:' + : 'number is', + (join ', ', @missing); diff --git a/challenge-201/spadacciniweb/perl/ch-2.pl b/challenge-201/spadacciniweb/perl/ch-2.pl new file mode 100644 index 0000000000..81aca678f9 --- /dev/null +++ b/challenge-201/spadacciniweb/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw/sum/; +use Algorithm::Combinatorics qw[ variations_with_repetition ]; + + +print "Please enter a integer number > 0: "; +my $input = <STDIN>; +chomp $input; +die 'input not valid' + unless $input =~ /^\d+$/; + +my $tot_penny = $input; +my $max_piles = $tot_penny; +my @piles; + + +foreach my $max_col (reverse(0..$max_piles-1)) { + my @seed_data = map { 1 } + (0..$max_col); + my $remaining_penny = $tot_penny - sum(@seed_data); + + my @n = (0..$remaining_penny); + + my $iter = variations_with_repetition( \@n, $#seed_data+1 ); + while (my $tupla = $iter->next) { + next if sum(@$tupla) != $remaining_penny + or + scalar @$tupla > 1 and scalar map { $tupla->[$_] > $tupla->[$_+1] ? 1 : ()} + (0..(scalar @$tupla)-2); + my @data = map { int($seed_data[$_] + $tupla->[$_] ) } + (0..$#seed_data); + push @piles, \@data; + } +} + +printf "Since \$n=%s, there are %s ways of stacking %s pennies in ascending piles:", $tot_penny, scalar @piles, $tot_penny; +printf "\n\t%s", join ' ', @$_ + foreach @piles; |
