Robbie Hatley's Solutions To The Weekly Challenge #275

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2024-06-23 through 2024-06-29 is #275. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 275-1: Broken Keys
Submitted by: Mohammad Sajid Anwar
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

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 fully.

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

A simple regular expression will handle this: ^[^$keys]+$

Robbie Hatley's Perl Solution to The Weekly Challenge 275-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 275-2: Replace Digits
Submitted by: Mohammad Sajid Anwar
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'

I find it interesting that the examples show that "previous digit" means "nearest digit to the left in original strinug, if any", rather than "digit immediately to the left". This causes two ambiguities:

  1. What if there is no previous letter?
  2. What if we try to shift 'z' by 3?

I think I'll fix #1 by returning 'invalid_string' unless the input is well-formed (m/[a-z][a-z0-9]*/).

And I'll fix #2 by looping back to the beginning of the alphabet as in ROT13 (so that z3 is c).

Robbie Hatley's Perl Solution to The Weekly Challenge 275-2

That's it for challenge 275; see you on challenge 276!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273