aboutsummaryrefslogtreecommitdiff
path: root/challenge-282
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2024-08-12 19:06:38 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2024-08-12 19:06:38 +0200
commit50caaa250341b117b58397d992bc69e7a7febfcf (patch)
tree5c20c47ba4e4644df90fdc16a129644a401804ad /challenge-282
parent51834a29c71cd0c6af0120d0f57583b6cab94ee6 (diff)
downloadperlweeklychallenge-club-50caaa250341b117b58397d992bc69e7a7febfcf.tar.gz
perlweeklychallenge-club-50caaa250341b117b58397d992bc69e7a7febfcf.tar.bz2
perlweeklychallenge-club-50caaa250341b117b58397d992bc69e7a7febfcf.zip
Add solution 282.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-282')
-rw-r--r--challenge-282/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-282/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-282/jeanluc2020/perl/ch-1.pl67
-rwxr-xr-xchallenge-282/jeanluc2020/perl/ch-2.pl69
4 files changed, 138 insertions, 0 deletions
diff --git a/challenge-282/jeanluc2020/blog-1.txt b/challenge-282/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..fd6fc9c56b
--- /dev/null
+++ b/challenge-282/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-282-1.html
diff --git a/challenge-282/jeanluc2020/blog-2.txt b/challenge-282/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..89f91c7fdf
--- /dev/null
+++ b/challenge-282/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-282-2.html
diff --git a/challenge-282/jeanluc2020/perl/ch-1.pl b/challenge-282/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..eb93f6c513
--- /dev/null
+++ b/challenge-282/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-282/#TASK1
+#
+# Task 1: Good Integer
+# ====================
+#
+# You are given a positive integer, $int, having 3 or more digits.
+#
+# Write a script to return the Good Integer in the given integer or -1 if none found.
+#
+### A good integer is exactly three consecutive matching digits.
+#
+## Example 1
+##
+## Input: $int = 12344456
+## Output: "444"
+#
+## Example 2
+##
+## Input: $int = 1233334
+## Output: -1
+#
+## Example 3
+##
+## Input: $int = 10020003
+## Output: "000"
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just walk the digits, taking note of the previous digit
+# and how often it occured already. If the current digit is
+# different from the previous one and we have seen the
+# previous digit exactly 3 times, we return that digit
+# 3 times. If we reached the end of $int, we just have to check
+# whether the last digit was seen 3 times as well.
+
+use strict;
+use warnings;
+
+good_integer(12344456);
+good_integer(1233334);
+good_integer(10020003);
+
+sub good_integer {
+ my $int = shift;
+ print "Input: $int\n";
+ my $prev_digit = "";
+ my $seen = 0;
+ foreach my $digit (split //, $int) {
+ if($prev_digit eq $digit) {
+ $seen++;
+ } elsif ($seen == 3) {
+ return print "Output: $prev_digit$prev_digit$prev_digit\n";
+ } else {
+ $prev_digit = $digit;
+ $seen = 1;
+ }
+ }
+ if($seen == 3) {
+ return print "Output: $prev_digit$prev_digit$prev_digit\n";
+ }
+ print "Output: -1\n";
+}
diff --git a/challenge-282/jeanluc2020/perl/ch-2.pl b/challenge-282/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..a73e163aa7
--- /dev/null
+++ b/challenge-282/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-282/#TASK2
+#
+# Task 2: Changing Keys
+# =====================
+#
+# You are given an alphabetic string, $str, as typed by user.
+#
+# Write a script to find the number of times user had to change the key to type
+# the given string. Changing key is defined as using a key different from the
+# last used key. The shift and caps lock keys won’t be counted.
+#
+## Example 1
+##
+## Input: $str = 'pPeERrLl'
+## Ouput: 3
+##
+## p -> P : 0 key change
+## P -> e : 1 key change
+## e -> E : 0 key change
+## E -> R : 1 key change
+## R -> r : 0 key change
+## r -> L : 1 key change
+## L -> l : 0 key change
+#
+## Example 2
+##
+## Input: $str = 'rRr'
+## Ouput: 0
+#
+## Example 3
+##
+## Input: $str = 'GoO'
+## Ouput: 1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We lowercase the whole string first. Then we just need to count
+# the number of changes that happen.
+
+use strict;
+use warnings;
+
+changing_keys('pPeERrLl');
+changing_keys('rRr');
+changing_keys('GoO');
+
+sub changing_keys {
+ my $str = shift;
+ print "Input: '$str'\n";
+ $str = lc($str);
+ my $last = undef;
+ my $changes = 0;
+ foreach my $char (split //, $str) {
+ if(defined($last)) {
+ if($last ne $char) {
+ $changes++;
+ $last = $char;
+ }
+ } else {
+ $last = $char;
+ }
+ }
+ print "Output: $changes\n";
+}