diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-19 10:33:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-19 10:33:45 +0100 |
| commit | 66e40605e5f81e5f18fc43317771bc4736e5c91b (patch) | |
| tree | c9fdbbde5b3cd496c888c9f9028fe15285ef3d6c | |
| parent | 1ec5389949cde3e7cae71c1c6298759adc5b9b1b (diff) | |
| parent | 2a7cc0992db22905fe9ea6def8bbd7514b089612 (diff) | |
| download | perlweeklychallenge-club-66e40605e5f81e5f18fc43317771bc4736e5c91b.tar.gz perlweeklychallenge-club-66e40605e5f81e5f18fc43317771bc4736e5c91b.tar.bz2 perlweeklychallenge-club-66e40605e5f81e5f18fc43317771bc4736e5c91b.zip | |
Merge pull request #10647 from boblied/w282
Week 282 solutions from Bob Lied
| -rw-r--r-- | challenge-282/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-282/bob-lied/perl/ch-1.pl | 80 | ||||
| -rw-r--r-- | challenge-282/bob-lied/perl/ch-2.pl | 89 |
3 files changed, 172 insertions, 3 deletions
diff --git a/challenge-282/bob-lied/README b/challenge-282/bob-lied/README index 5d935bad60..808e99c97f 100644 --- a/challenge-282/bob-lied/README +++ b/challenge-282/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 281 by Bob Lied +Solutions to weekly challenge 282 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-281/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-281/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-282/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-282/bob-lied diff --git a/challenge-282/bob-lied/perl/ch-1.pl b/challenge-282/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..325aeefc8b --- /dev/null +++ b/challenge-282/bob-lied/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 282 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" +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say goodIint($_) for @ARGV; + +sub goodInt($int) +{ + my @digit = split(//, "$int"); + my $d = shift @digit; + while ( defined $d ) + { + my $consecutive = 1; + my $n; + ++$consecutive while defined($n = shift @digit) && $n == $d; + return ($d x 3) if $consecutive == 3; + $d = $n; + } + return -1; +} + +sub gi2($int) +{ + ( grep {length($_) == 3 } $int =~ m/((.)\2\2+)/g )[0] // -1 ; +} + +sub runTest +{ + use Test2::V0; + + is( goodInt(12344456), "444", "Example 1"); + is( goodInt(1233334), -1, "Example 2"); + is( goodInt(10020003), "000", "Example 3"); + is( goodInt(66612345), "666", "At front"); + is( goodInt(12345666), "666", "At end"); + is( goodInt(17775666), "777", "Two possibilities"); + + is( gi2(12344456), "444", "gi2 Example 1"); + is( gi2(1233334), -1, "gi2 Example 2"); + is( gi2(10020003), "000", "gi2 Example 3"); + is( gi2(66612345), "666", "gi2 At front"); + is( gi2(12345666), "666", "gi2 At end"); + is( gi2(17775666), "777", "gi2 Two possibilities"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + my @input = ( 12344456, 1233334, 10020003, 66612345, 12345666, 17775666 ); + + cmpthese($repeat, { + gi_while => sub { goodInt($_) for @input }, + gi_re => sub { gi2($_) for @input }, + }); +} diff --git a/challenge-282/bob-lied/perl/ch-2.pl b/challenge-282/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..dafda113a2 --- /dev/null +++ b/challenge-282/bob-lied/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2024, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 282 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' +# Output: 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' +# Output: 0 +# Example 3 Input: $str = 'GoO' +# Output: 1 +#============================================================================= + +use v5.40; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +# My keyboard on a MacBook +my %Keyboard = ( + US => { LC => q(1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./ ), + UC => q(!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>? ), + }, + ES => { LC => q(1234567890'¿qwertyuiop´+}asdfghjklñ{zxcvbnm,.- ), + UC => q(!"#$%&/()=?¡QWERTYUIOP¨*]ASDFGHJKLÑ[ZXCVBNM;:_ ), + } +); +my %KEY; +foreach ( 0 .. length($Keyboard{US}{LC})-1 ) +{ + $KEY{ substr($Keyboard{US}{LC}, $_, 1) } = $KEY{substr($Keyboard{US}{UC}, $_, 1)} = $_; +} + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say change($_) for @ARGV; + +sub change($str) +{ + my $count = 0; + my @char = split(//, lc $str); + my $first = shift @char; + while ( defined(my $second = shift @char) ) + { + $count++ if $KEY{$first} != $KEY{$second}; + $first = $second; + } + return $count; +} + +sub runTest +{ + use Test2::V0; + + is( change('pPeErLl'), 3, "Example 1"); + is( change('rRr'), 0, "Example 2"); + is( change('GoO'), 1, "Example 3"); + is( change('(90)>.3'), 3, "Punctuation and numbers"); + is( change($Keyboard{US}{LC}), 46, "All the things"); + is( change($Keyboard{US}{UC}), 46, "All the things, but shifted"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
