aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-12-20 10:47:39 +0100
committerLubos Kolouch <lubos@kolouch.net>2020-12-20 10:47:39 +0100
commit4ebd86f136c6cb4b403ec74d8eb8a30cdd282bf4 (patch)
treec29b3b51114ea58dfe68bba0c359a791493d781d
parent2c8320e7653a2fb8335078b07e8e7925f540073b (diff)
downloadperlweeklychallenge-club-4ebd86f136c6cb4b403ec74d8eb8a30cdd282bf4.tar.gz
perlweeklychallenge-club-4ebd86f136c6cb4b403ec74d8eb8a30cdd282bf4.tar.bz2
perlweeklychallenge-club-4ebd86f136c6cb4b403ec74d8eb8a30cdd282bf4.zip
Challenge 091 LK
-rw-r--r--challenge-091/lubos-kolouch/perl/ch_1.pl52
-rw-r--r--challenge-091/lubos-kolouch/perl/ch_2.pl48
2 files changed, 100 insertions, 0 deletions
diff --git a/challenge-091/lubos-kolouch/perl/ch_1.pl b/challenge-091/lubos-kolouch/perl/ch_1.pl
new file mode 100644
index 0000000000..88fd4d34ed
--- /dev/null
+++ b/challenge-091/lubos-kolouch/perl/ch_1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch_1.pl
+#
+# USAGE: ./ch_1.pl
+#
+# DESCRIPTION: Perl Weekly Challenge #091
+# Task 1 - Count Number
+# www.perlweeklychallenge.org
+#
+# AUTHOR: Lubos Kolouch
+# VERSION: 1.0
+# CREATED: 12/20/2020 10:27:07 AM
+#===============================================================================
+
+use strict;
+use warnings;
+
+sub as_we_read {
+ my $what = shift;
+
+ my $solution = '';
+
+ my $pos = 1;
+ my $count = 1;
+ my $last_char = substr($what, 0, 1);
+
+ while ($pos <= length($what)) {
+ if (($pos == length($what)) or (substr($what, $pos, 1) != $last_char)) {
+ $solution .= $count.$last_char;
+ $count = 1;
+ $last_char = substr($what, $pos, 1);
+ } else {
+ $count++;
+ }
+
+
+ $pos += 1;
+ }
+
+ return $solution;
+
+}
+
+use Test::More;
+
+is(as_we_read(1122234), 21321314);
+is(as_we_read(2333445), 12332415);
+is(as_we_read(12345), 1112131415);
+
+done_testing;
diff --git a/challenge-091/lubos-kolouch/perl/ch_2.pl b/challenge-091/lubos-kolouch/perl/ch_2.pl
new file mode 100644
index 0000000000..df4a01ba3f
--- /dev/null
+++ b/challenge-091/lubos-kolouch/perl/ch_2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch_1.pl
+#
+# USAGE: ./ch_1.pl
+#
+# DESCRIPTION: Perl Weekly Challenge #091
+# Task 2 - Jump Game
+# www.perlweeklychallenge.org
+#
+# AUTHOR: Lubos Kolouch
+# VERSION: 1.0
+# CREATED: 12/20/2020 10:27:07 AM
+#===============================================================================
+
+use strict;
+use warnings;
+
+sub jump {
+ my $in_arr = shift;
+
+ # test if each zero can be overjumped
+
+ my $pos = 0;
+ for my $num (@$in_arr) {
+ if ($in_arr->[$pos] == 0) {
+ my $back = 0;
+ # work backwards and stop if we can jump over or at the beginning
+ while ( ($pos >= 0) and ($in_arr->[$pos] + $pos - $back > $pos) ) {
+ $back--;
+ }
+
+ return 0 unless $back;
+ }
+ $pos++;
+ }
+
+ return 1;
+
+}
+
+use Test::More;
+
+is(jump([1, 2, 1, 2]), 1);
+is(jump([2, 1, 1, 0, 2]), 0);
+
+done_testing;