From 2ca314cd4cfa147739729b900d2b254834eb6689 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Mon, 23 Jan 2023 16:09:47 +0100 Subject: PWC 201 --- challenge-201/spadacciniweb/perl/ch-1.pl | 33 ++++++++++++++++++++++++++ challenge-201/spadacciniweb/perl/ch-2.pl | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 challenge-201/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-201/spadacciniweb/perl/ch-2.pl 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 = ; +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; -- cgit