Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #307 ("Check Order" and "Find Anagrams")

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 2025-02-03 through 2025-02-09 is #307.

The tasks for challenge #307 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 307-1: "Check Order"
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to
re-arrange the given array in an increasing order and return
the indices where it differs from the original array.

Example #1:
Input: @ints = (5, 2, 4, 3, 1)
Output: (0, 2, 3, 4)
Before: (5, 2, 4, 3, 1)
After : (1, 2, 3, 4, 5)
Difference at indices: (0, 2, 3, 4)

Example #2:
Input: @ints = (1, 2, 1, 1, 3)
Output: (1, 3)
Before: (1, 2, 1, 1, 3)
After : (1, 1, 1, 2, 3)
Difference at indices: (1, 3)

Example #3:
Input: @ints = (3, 1, 3, 2, 3)
Output: (0, 1, 3)
Before: (3, 1, 3, 2, 3)
After : (1, 2, 3, 3, 3)
Difference at indices: (0, 1, 3)

This is just a matter of numeric sort followed by compare.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 307-2: "Find Anagrams"
Submitted by: Mohammad Sajid Anwar
You are given a list of words, @words. Write a script to find
any two consecutive words and if they are anagrams, drop the
first word and keep the second. You continue this until there
is no more anagrams in the given list and return the count of
final list.

Example #1:
Input: @words = ("acca", "dog", "god", "perl", "repl")
Output: 3
Step 1: "dog" and "god" are anagrams, so dropping "dog" and
        keeping "god" => ("acca", "god", "perl", "repl")
Step 2: "perl" and "repl" are anagrams, so dropping "perl"
        and keeping "repl" => ("acca", "god", "repl")

Example #2:
Input: @words = ("abba", "baba", "aabb", "ab", "ab")
Output: 2
Step 1: "abba" and "baba" are anagrams, so dropping "abba"
        and keeping "baba" => ("baba", "aabb", "ab", "ab")
Step 2: "baba" and "aabb" are anagrams, so dropping "baba"
        and keeping "aabb" => ("aabb", "ab", "ab")
Step 3: "ab" and "ab" are anagrams, so dropping "ab" and
        keeping "ab" => ("aabb", "ab")

One simple way to solve this would be to sort the letters of the words, but I'll use a different approach: I'll make a "signature" for each word, using the method Newton used in his famous letter to Liebnitz. For example, sig("parrot") = "a1o1p1r2t1".

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

That's it for challenge 307; see you on challenge 308!

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