aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-201/solathian/perl/ch-1.pl27
-rw-r--r--challenge-201/solathian/perl/ch-2.pl81
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