aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-226/e-choroba/perl/ch-1.pl44
-rwxr-xr-xchallenge-226/e-choroba/perl/ch-2.pl22
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-226/e-choroba/perl/ch-1.pl b/challenge-226/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..00b8537a63
--- /dev/null
+++ b/challenge-226/e-choroba/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub shuffle_string_substr($string, @indices) {
+ my $r = ' ' x @indices;
+ substr $r, $indices[$_], 1, substr $string, $_, 1 for 0 .. $#indices;
+ return $r
+}
+
+sub shuffle_string_hash($string, @indices) {
+ my %rev = reverse %indices[0 .. $#indices];
+ join "", map { substr $string, $rev{$_}, 1, substr $string, $_, 1 }
+ 0 .. $#indices
+}
+
+use Test::More tests => 2 * 2;
+my %implementation = (substr => *shuffle_string_substr{CODE},
+ hash => *shuffle_string_hash{CODE});
+for my $shuffle_string (keys %implementation) {
+ is $implementation{$shuffle_string}
+ ->(lacelengh => 3, 2, 0, 5, 4, 8, 6, 7, 1),
+ 'challenge',
+ "Example 1 - $shuffle_string";
+
+ is $implementation{$shuffle_string}->(rulepark => 4, 7, 3, 1, 0, 5, 2, 6),
+ 'perlraku',
+ "Example 2 - $shuffle_string";
+}
+
+use Benchmark qw{ cmpthese };
+cmpthese(-3, {
+ substr => sub { shuffle_string_substr(
+ lacelengh => 3, 2, 0, 5, 4, 8, 6, 7, 1) },
+ hash => sub { shuffle_string_hash(
+ lacelengh => 3, 2, 0, 5, 4, 8, 6, 7, 1) },
+});
+
+__END__
+
+ Rate hash substr
+hash 153947/s -- -66%
+substr 455856/s 196% --
diff --git a/challenge-226/e-choroba/perl/ch-2.pl b/challenge-226/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..a3ca2dce9f
--- /dev/null
+++ b/challenge-226/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+# It makes no sense to subtract anything but the minimum (otherwise,
+# we'd have to subtract the remaining quantity in a later step). To
+# get the number of steps, it's enough to count the number of unique
+# positive elements.
+
+sub zero_array(@ints) {
+ my %element;
+ @element{@ints} = ();
+ delete $element{0};
+ return keys %element
+}
+
+use Test::More tests => 3;
+
+is zero_array(1, 5, 0, 3, 5), 3, 'Example 1';
+is zero_array(0), 0, 'Example 2';
+is zero_array(2, 1, 4, 0, 3), 4, 'Example 3';