aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-238/peter-meszaros/perl/ch-1.pl55
-rwxr-xr-xchallenge-238/peter-meszaros/perl/ch-2.pl73
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-238/peter-meszaros/perl/ch-1.pl b/challenge-238/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..b80ae4211a
--- /dev/null
+++ b/challenge-238/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+#
+# You are given an array of integers.
+#
+# Write a script to return the running sum of the given array. The running sum
+# can be calculated as sum[i] = num[0] + num[1] + …. + num[i].
+# Example 1
+#
+# Input: @int = (1, 2, 3, 4, 5)
+# Output: (1, 3, 6, 10, 15)
+#
+# Example 2
+#
+# Input: @int = (1, 1, 1, 1, 1)
+# Output: (1, 2, 3, 4, 5)
+#
+# Example 3
+#
+# Input: @int = (0, -1, 1, 2)
+# Output: (0, -1, 0, 2)
+#
+
+use strict;
+use warnings;
+use Test::More;
+use Data::Dumper;
+
+my $cases = [
+ [1, 2, 3, 4, 5],
+ [1, 1, 1, 1, 1],
+ [0, -1, 1, 2],
+];
+
+sub running_sum
+{
+ my $l = shift;
+
+ my @ret;
+ my $sum = 0;
+ for my $v (@$l) {
+ $sum += $v;
+ push @ret, $sum;
+ }
+
+ return \@ret;
+}
+
+is_deeply(running_sum($cases->[0]), [1, 3, 6, 10, 15], '[1, 2, 3, 4, 5]');
+is_deeply(running_sum($cases->[1]), [1, 2, 3, 4, 5], '[1, 1, 1, 1, 1]');
+is_deeply(running_sum($cases->[2]), [0, -1, 0, 2], '[0, -1, 1, 2]');
+done_testing();
+
+exit 0;
+
+
diff --git a/challenge-238/peter-meszaros/perl/ch-2.pl b/challenge-238/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..0236251275
--- /dev/null
+++ b/challenge-238/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+#
+# You are given an array of positive integers.
+#
+# Write a script to sort the given array in increasing order with respect to
+# the count of steps required to obtain a single-digit number by multiplying its
+# digits recursively for each array element. If any two numbers have the same
+# count of steps, then print the smaller number first.
+# Example 1
+#
+# Input: @int = (15, 99, 1, 34)
+# Output: (1, 15, 34, 99)
+#
+# 15 => 1 x 5 => 5 (1 step)
+# 99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+# 1 => 0 step
+# 34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+#
+# Example 2
+#
+# Input: @int = (50, 25, 33, 22)
+# Output: (22, 33, 50, 25)
+#
+# 50 => 5 x 0 => 0 (1 step)
+# 25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+# 33 => 3 x 3 => 6 (1 step)
+# 22 => 2 x 2 => 4 (1 step)
+#
+
+use strict;
+use warnings;
+use Test::More;
+use Data::Dumper;
+
+my $cases = [
+ [15, 99, 1, 34],
+ [50, 25, 33, 22],
+];
+
+sub count_steps
+{
+ my $n = shift;
+
+ my $steps = 0;
+ while(length($n)> 1) {
+ ++$steps;
+ my @n = split('', $n);
+ $n = 1;
+ $n *= $_ for @n;
+ }
+ return $steps;
+}
+
+sub persistence_sort
+{
+ my $l = shift;
+
+ my @steps = map { count_steps($_) } @$l;
+
+ my @ord = sort { if ($steps[$a] == $steps[$b]) {
+ return $l->[$a] <=> $l->[$b];
+ }
+ return $steps[$a] <=> $steps[$b]
+ } (0 .. $#$l);
+
+ return [$l->@[@ord]];
+}
+
+is_deeply(persistence_sort($cases->[0]), [1, 15, 34, 99], '[15, 99, 1, 34]');
+is_deeply(persistence_sort($cases->[1]), [22, 33, 50, 25], '[50, 25, 33, 22]');
+done_testing();
+
+exit 0;