Skip to content

Week 275 solutions from Bob Lied #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions challenge-275/bob-lied/README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Solutions to weekly challenge 274 by Bob Lied
Solutions to weekly challenge 275 by Bob Lied

https://perlweeklychallenge.org/blog/perl-weekly-challenge-274/
https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-274/bob-lied
https://perlweeklychallenge.org/blog/perl-weekly-challenge-275/
https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-275/bob-lied
81 changes: 81 additions & 0 deletions challenge-275/bob-lied/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/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 275 Task 1 Broken Keys
#=============================================================================
# You are given a sentence, $sentence and list of broken keys @keys.
# Write a script to find out how many words can be typed fully.
# Example 1 Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a')
# Output: 0
# Every word requires one of the broken keys.
# Example 2 Input: $sentence = "Perl and Raku", @keys = ('a')
# Output: 1
# Only Perl since the other word two words contain 'a' and can't be typed.
# Example 3 Input: $sentence = "Well done Team PWC", @keys = ('l', 'o')
# Output: 2
# Example 4 Input: $sentence = "The joys of polyglottism", @keys = ('T')
# Output: 2
#=============================================================================

use v5.40;

use Getopt::Long;
my $DoTest = false;
my $Benchmark = 0;

GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark);
exit(!runTest()) if $DoTest;
exit( runBenchmark($Benchmark) ) if $Benchmark;

say brokenKeys(@ARGV);

sub brokenKeys($sentence, @keys)
{
my @s = split(/\W+/, $sentence);
for my $broken ( @keys )
{
@s = grep !/$broken/i, @s;
}
return scalar(@s);
}

sub bk($sentence, @keys)
{
# my $re = '^[^' . join("", @keys) . ']*$';
# return scalar grep /$re/i, split(/\W+/, $sentence);
# my $re = '[' . join("", @keys) . ']';
my $re;
{ local $, = ''; $re = qq([@keys]); }
my @s = split(/\W+/, $sentence);
return scalar(@s) - ( grep /$re/i, @s );
}

sub runTest
{
use Test2::V0;

is( brokenKeys("Perl Weekly Challenge", 'l', 'a'), 0, "Example 1");
is( brokenKeys("Perl and Raku", 'a' ), 1, "Example 2");
is( brokenKeys("Well done Team PWC", 'l', 'o'), 2, "Example 3");
is( brokenKeys("The joys of polyglottism", 'T' ), 2, "Example 4");

is( bk("Perl Weekly Challenge", 'l', 'a'), 0, "Example 1 bk");
is( bk("Perl and Raku", 'a' ), 1, "Example 2 bk");
is( bk("Well done Team PWC", 'l', 'o'), 2, "Example 3 bk");
is( bk("The joys of polyglottism", 'T' ), 2, "Example 4 bk");

done_testing;
}

sub runBenchmark($repeat)
{
use Benchmark qw/cmpthese/;
my $sentence = "It's eighty degrees and I'm down on my knees in Brooklyn";

cmpthese($repeat, {
re => sub { bk($sentence, qw(f a w z y) ) },
loop => sub { brokenKeys($sentence, qw(f a w z y) ) },
});
}
62 changes: 62 additions & 0 deletions challenge-275/bob-lied/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/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 275 Task 2 Replace Digits
#=============================================================================
# You are given an alphanumeric string, $str, where each character is
# either a letter or a digit.
# Write a script to replace each digit in the given string with the
# value of the previous letter plus (digit) places.
# Example 1 Input: $str = 'a1c1e1'
# Ouput: 'abcdef'
# shift('a', 1) => 'b' shift('c', 1) => 'd' shift('e', 1) => 'f'
# Example 2 Input: $str = 'a1b2c3d4'
# Output: 'abbdcfdh'
# shift('a', 1) => 'b' shift('b', 2) => 'd'
# shift('c', 3) => 'f' shift('d', 4) => 'h'
# Example 3 Input: $str = 'b2b'
# Output: 'bdb'
# Example 4 Input: $str = 'a16z'
# Output: 'abgz'
#=============================================================================

use v5.40;


use Getopt::Long;
my $DoTest = false;
my $Benchmark = 0;

GetOptions("test" => \$DoTest);
exit(!runTest()) if $DoTest;

# Must not begin with a digit
say replaceDigits($_) for map { s/^[0-9]*//ir } @ARGV;

sub replaceDigits($str)
{
my @s = split("", $str);
my $letter = my $result = shift @s;

while ( defined(my $next = shift @s) )
{
if ( $next =~ m/[^0-9]/i ) { $result .= ($letter = $next) }
else { $result .= chr(ord($letter) + $next); }
}
return $result;
}


sub runTest
{
use Test2::V0;

is( replaceDigits("a1c1e1" ), "abcdef" , "Example 1");
is( replaceDigits("a1b2c3d4"), "abbdcfdh", "Example 2");
is( replaceDigits("b2b" ), "bdb" , "Example 3");
is( replaceDigits("a16z" ), "abgz" , "Example 4");

done_testing;
}