DEV Community

oldtechaa
oldtechaa

Posted on

Perl Weekly Challenge #224 - Passing Notes

Hi everybody! Just doing one challenge again this week. Time limitations hold me back once again.

This week we're looking for the letters of a target word in a source word, and we're not allowed to use the same letter twice. Spoiler alert because it's only Wednesday and you still have the rest of the week to submit solutions if desired.

The easiest way to do this is with a dictionary hash initialized like so:

foreach (split //, $source) {$chars{$_}++}
Enter fullscreen mode Exit fullscreen mode

Many people use map() to do this, but I'm not a big fan of map in many cases because I feel it makes code less readable.

This gives us the number of occurrences of each letter in the original word.

Then we iterate through the target word and look for (and remove) the letters in the dictionary:

foreach (split //, $target) {
    if ($chars{$_}) {
        $chars{$_}--;
    } else {
        say 'false' and exit;
    }
}
say 'true';
Enter fullscreen mode Exit fullscreen mode

Obviously if the script did more than just this we'd need a success flag in there instead, but as it is we can just exit if we ever don't find the matching character in the dictionary.

Anyways, that's the solution for this week! I have a definite idea of how I'd solve the second challenge, I just don't have the time to implement it. I look forward to seeing the other solutions, because I'm sure they'll have more efficient ways to do it.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more