aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAugie De Blieck Jr <augiedb@gmail.com>2023-10-14 01:02:20 -0400
committerAugie De Blieck Jr <augiedb@gmail.com>2023-10-14 01:18:32 -0400
commit97989c7fc6968bf19d7cb49ba3e5605dcb544c3e (patch)
tree7d2b9814c9ef0e6456a239568e0627398e4f589a
parent89c66871b2b57061fd6f12d71e016979e41e23a2 (diff)
downloadperlweeklychallenge-club-97989c7fc6968bf19d7cb49ba3e5605dcb544c3e.tar.gz
perlweeklychallenge-club-97989c7fc6968bf19d7cb49ba3e5605dcb544c3e.tar.bz2
perlweeklychallenge-club-97989c7fc6968bf19d7cb49ba3e5605dcb544c3e.zip
Solutions by Augie De Blieck Jr.
Solutions for both tasks
-rw-r--r--challenge-238/augiedb/README1
-rw-r--r--challenge-238/augiedb/perl/ch-1.pl68
-rw-r--r--challenge-238/augiedb/perl/ch-2.pl108
3 files changed, 177 insertions, 0 deletions
diff --git a/challenge-238/augiedb/README b/challenge-238/augiedb/README
new file mode 100644
index 0000000000..0842edd447
--- /dev/null
+++ b/challenge-238/augiedb/README
@@ -0,0 +1 @@
+Solution by Augie De Blieck Jr.
diff --git a/challenge-238/augiedb/perl/ch-1.pl b/challenge-238/augiedb/perl/ch-1.pl
new file mode 100644
index 0000000000..0b80826dcf
--- /dev/null
+++ b/challenge-238/augiedb/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+# Week 238, Task 1: Running Sum
+# Given an array, output an array of the running sum.
+
+# Test Data:
+my @test1 = (1, 2, 3, 4, 5);
+my @test2 = (1, 1, 1, 1, 1);
+my @test3 = (0, -1, 1, 2);
+my @array_examples = (\@test1, \@test2, \@test3);
+
+
+print "Running Sum Code Results:\n";
+print "=========================\n\n";
+
+foreach my $array_ref (@array_examples) {
+
+ print "Array: " ;
+ pretty_print_array( $array_ref );
+
+ my @results = show_running_sum($array_ref);
+
+ print "Results: ";
+ pretty_print_array(\@results);
+
+ print "\n";
+
+}
+
+##
+## Subroutines
+##
+
+sub show_running_sum {
+
+ ## Ultimately, this is the heart of the solution
+
+ my @array = @{ shift() };
+ my $sum = 0;
+
+ return map{ $sum += $_ } @array;
+
+}
+
+sub pretty_print_array {
+
+ ## Total overkill, but I'm new and over-enthusiastic.
+ ## In reality, I'd find something on CPAN for this.
+
+ my @array = @{ shift() };
+ my $length = scalar @array;
+ my $count = 1;
+
+ print "(";
+
+ foreach my $value(@array) {
+ print $value;
+ print ", " if $count < $length;
+ $count++;
+ }
+
+ print ")\n";
+
+ return;
+}
+
diff --git a/challenge-238/augiedb/perl/ch-2.pl b/challenge-238/augiedb/perl/ch-2.pl
new file mode 100644
index 0000000000..f2626eadb7
--- /dev/null
+++ b/challenge-238/augiedb/perl/ch-2.pl
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# Week 238, Task 2: Persistence Sort
+# I can't explain it in one line. Go to the website.
+
+## Test Values:
+my @first = (15, 99, 1, 34);
+my @second = (50, 25, 33, 22);
+my @tests = ( \@first, \@second );
+
+## This is global because I'm a bad person.
+## Also, I ran out of time to fix it.
+my %steps;
+
+## Runner:
+foreach my $array_ref( @tests ) {
+
+ print "BEFORE: ";
+ pretty_print_array( $array_ref);
+
+ my @results = persistance_sort( $array_ref );
+
+ print "AFTER: ";
+ pretty_print_array(\@results);
+
+ print "\n";
+
+ ## The price I pay for the global
+ %steps = ();
+
+}
+
+
+## Basically, main():
+sub persistance_sort {
+
+ my @array = @{ shift() };
+
+ foreach my $value( @array ) {
+ my @array_of_numbers = split(//, $value);
+ $steps{$value} = multiply_numbers_recursively($value, 0);
+ }
+
+ my @final_results = sort compare keys %steps;
+
+ return @final_results;
+
+}
+
+sub compare {
+
+ ## The two level sort function
+ ## The reason I have a global variable.
+
+ if($steps{$a} < $steps{$b}) {
+ return -1;
+ } elsif ($steps{$a} == $steps{$b}) {
+ return 1 if $a > $b;
+ return -1;
+ } else {
+ return 1
+ }
+
+}
+
+
+sub multiply_numbers_recursively {
+
+ # Recursion Rocks
+
+ my $number = shift();
+ my $count = shift();
+
+ my @list_of_numbers = split(//, $number);
+ return $count if scalar(@list_of_numbers) == 1;
+
+ my $running_total = 1;
+ map{ $running_total *= $_ } @list_of_numbers;
+
+ return multiply_numbers_recursively($running_total, ++$count);
+}
+
+sub pretty_print_array {
+
+ ## I copied-and-pasted this from my solution for Task 1.
+ ## If I copy-and-paste it again, by law I need to turn
+ ## it into a library. ;-)
+
+ my @array = @{ shift() };
+ my $length = scalar @array;
+ my $count = 1;
+
+ print "(";
+
+ foreach my $value(@array) {
+ print $value;
+ print ", " if $count < $length;
+ $count++;
+ }
+
+ print ")\n";
+
+ return;
+}
+