From 07be6207a1898d4cc16779a6eaac5457271edf68 Mon Sep 17 00:00:00 2001 From: Augie De Blieck Jr Date: Sat, 14 Oct 2023 01:02:20 -0400 Subject: Perl solutions for both tasks of Week 238 by Augie De Blieck Jr. --- challenge-238/augiedb/README | 1 + challenge-238/augiedb/perl/ch-1.pl | 68 ++++++++++++++++++++++ challenge-238/augiedb/perl/ch-2.pl | 113 +++++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 challenge-238/augiedb/README create mode 100644 challenge-238/augiedb/perl/ch-1.pl create mode 100644 challenge-238/augiedb/perl/ch-2.pl 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..fe0ec15a46 --- /dev/null +++ b/challenge-238/augiedb/perl/ch-2.pl @@ -0,0 +1,113 @@ +#!/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 = (); + + foreach my $result (sort compare keys %steps) { + push(@final_results, $result); + } + + 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; +} + -- cgit From 36e7646c7d379da6be38d9b54b672e73e74c3510 Mon Sep 17 00:00:00 2001 From: Augie De Blieck Jr Date: Sat, 14 Oct 2023 01:08:41 -0400 Subject: Updated Perl Task 2 solution to eliminate redundant foreach loop --- challenge-238/augiedb/perl/ch-2.pl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/challenge-238/augiedb/perl/ch-2.pl b/challenge-238/augiedb/perl/ch-2.pl index fe0ec15a46..f2626eadb7 100644 --- a/challenge-238/augiedb/perl/ch-2.pl +++ b/challenge-238/augiedb/perl/ch-2.pl @@ -40,16 +40,11 @@ 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 = (); - - foreach my $result (sort compare keys %steps) { - push(@final_results, $result); } + + my @final_results = sort compare keys %steps; return @final_results; -- cgit