aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-15 09:37:58 +0100
committerGitHub <noreply@github.com>2023-10-15 09:37:58 +0100
commit30c224e9017864bfa1b5d81fd63a78a10f769eb6 (patch)
tree9b67faa81869ddf26b7149fab674e28a5f3e96b0
parent2fe2b9e7f05465e7a88a1f352e71fa14e47574d9 (diff)
parent30f8af4d485603440b88a5859103e0fbd9712959 (diff)
downloadperlweeklychallenge-club-30c224e9017864bfa1b5d81fd63a78a10f769eb6.tar.gz
perlweeklychallenge-club-30c224e9017864bfa1b5d81fd63a78a10f769eb6.tar.bz2
perlweeklychallenge-club-30c224e9017864bfa1b5d81fd63a78a10f769eb6.zip
Merge pull request #8867 from augiedb/new-branch
New branch
-rw-r--r--challenge-238/augiedb/README1
-rw-r--r--challenge-238/augiedb/blog1.txt1
-rw-r--r--challenge-238/augiedb/blog2.txt1
-rw-r--r--challenge-238/augiedb/blog3.txt1
-rw-r--r--challenge-238/augiedb/elixir/ch-1.exs30
-rw-r--r--challenge-238/augiedb/perl/ch-1.pl68
-rw-r--r--challenge-238/augiedb/perl/ch-2.pl108
7 files changed, 210 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/blog1.txt b/challenge-238/augiedb/blog1.txt
new file mode 100644
index 0000000000..3f50afd0cc
--- /dev/null
+++ b/challenge-238/augiedb/blog1.txt
@@ -0,0 +1 @@
+https://variousandsundry.com/running-sum-or-i-love-recursion/
diff --git a/challenge-238/augiedb/blog2.txt b/challenge-238/augiedb/blog2.txt
new file mode 100644
index 0000000000..db95b7db99
--- /dev/null
+++ b/challenge-238/augiedb/blog2.txt
@@ -0,0 +1 @@
+https://variousandsundry.com/persistence-sort-in-which-i-give-up-and-use-a-global/
diff --git a/challenge-238/augiedb/blog3.txt b/challenge-238/augiedb/blog3.txt
new file mode 100644
index 0000000000..526c7a2095
--- /dev/null
+++ b/challenge-238/augiedb/blog3.txt
@@ -0,0 +1 @@
+https://variousandsundry.com/recursive-running-sum-redux-in-elixir/
diff --git a/challenge-238/augiedb/elixir/ch-1.exs b/challenge-238/augiedb/elixir/ch-1.exs
new file mode 100644
index 0000000000..ccdbf7d544
--- /dev/null
+++ b/challenge-238/augiedb/elixir/ch-1.exs
@@ -0,0 +1,30 @@
+# Given an array, output an array of the running sum.
+
+defmodule Runner do
+
+ def running_sum([head | tail], new_list, sum) do
+ sum = sum + head
+ running_sum(tail, new_list ++ [sum], sum)
+ end
+
+ def running_sum([], new_list, _) do
+ new_list
+ end
+end
+
+IO.puts 'Exercise 1: '
+list_of_numbers = [1, 2, 3, 4, 5]
+IO.inspect list_of_numbers
+IO.inspect Runner.running_sum(list_of_numbers, [], 0)
+
+IO.puts 'Exercise 2: '
+list_of_numbers = [1, 1, 1, 1, 1]
+IO.inspect list_of_numbers
+IO.inspect Runner.running_sum(list_of_numbers, [], 0)
+
+IO.puts 'Exercise 3: '
+list_of_numbers = [0, -1, 1, 2]
+IO.inspect list_of_numbers
+IO.inspect Runner.running_sum(list_of_numbers, [], 0)
+
+
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..f994839183
--- /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 = persistence_sort( $array_ref );
+
+ print "AFTER: ";
+ pretty_print_array(\@results);
+
+ print "\n";
+
+ ## The price I pay for the global
+ %steps = ();
+
+}
+
+
+## Basically, main():
+sub persistence_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;
+}
+