diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-30 00:05:58 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-30 00:05:58 +0000 |
| commit | cfa5d21fd3463ae83e0818c82cd7b87573d6422d (patch) | |
| tree | 994586e4569f58381146426f2efa7e31fc19fb92 /challenge-201 | |
| parent | 674738936ef381162b4c274889ff06146c4e4b6c (diff) | |
| parent | 18423300c2e14878785b58bffe9aeaa473b13c05 (diff) | |
| download | perlweeklychallenge-club-cfa5d21fd3463ae83e0818c82cd7b87573d6422d.tar.gz perlweeklychallenge-club-cfa5d21fd3463ae83e0818c82cd7b87573d6422d.tar.bz2 perlweeklychallenge-club-cfa5d21fd3463ae83e0818c82cd7b87573d6422d.zip | |
Merge pull request #7496 from Solathian/branch-for-challenge-201
adding files
Diffstat (limited to 'challenge-201')
| -rw-r--r-- | challenge-201/solathian/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-201/solathian/perl/ch-2.pl | 81 |
2 files changed, 108 insertions, 0 deletions
diff --git a/challenge-201/solathian/perl/ch-1.pl b/challenge-201/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..d6f0aa0b15 --- /dev/null +++ b/challenge-201/solathian/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!usr/bin/perl +use v5.36; + +no warnings "experimental::smartmatch"; + +# Challange 201 - 1 - Missing Numbers +# 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. + + +# missingNumbers((0,1,3,5)); # Output: 2,4 +# missingNumbers((0,1,3)); # Output: 2 +# missingNumbers((0,1)); # Output: 2 + + +sub missingNumbers(@array) +{ + my @missing; + + for(my $i = 0; $i <= @array; $i++) + { + push(@missing, $i) if( not ($i ~~ @array)); + } + + say "Missing:".join(',', @missing); +}
\ No newline at end of file diff --git a/challenge-201/solathian/perl/ch-2.pl b/challenge-201/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..9345788629 --- /dev/null +++ b/challenge-201/solathian/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!usr/bin/perl +use v5.36; + +use Storable qw(dclone); + +# Challange 201 - 2 - Penny Piles + +# 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. + + + + + + +# pennyPiles(5); +# pennyPiles(7); + +sub pennyPiles($n) +{ + my @pennyRows; + + + # create initial array of 1s + my @arr = (1) x $n; + + push(@pennyRows, dclone(\@arr)); + + + + MAIN:while() + { + + + # try to merge 1s from the right 1 1 1 1 1 -> 1 1 1 2, or 1 1 3 -> 2 3 + for(my $i = $#arr; $i > 0; $i--) + { + if(($arr[$i] == 1) && ($arr[$i-1] == 1)) + { + splice(@arr, $i, 1); + $arr[$i-1] = 2; + push(@pennyRows, dclone(\@arr)); + next MAIN; + } + } + + # try to move 1s from the left to right 2 3 -> 1 4 + for(my $i = 0; $i < $#arr; $i++) + { + if($arr[$i] > 1 ) + { + $arr[$i]--; + $arr[$#arr]++; + push(@pennyRows, dclone(\@arr)); + next MAIN; + } + } + + + # last step 1 4 -> 5 + if(@arr == 2) + { + splice(@arr, 0, 1); + $arr[0]++; + push(@pennyRows, dclone(\@arr)); + } + + last if(@arr == 1) + + } + + say "\$n is $n, There are ". scalar(@pennyRows). " ways of stacking $n pennies in ascending piles:"; + foreach my $row (@pennyRows) + { + say @$row; + } + + +}
\ No newline at end of file |
