Robbie Hatley's Solutions To The Weekly Challenge #265

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

This week (2024-04-14 through 2024-04-20) is weekly challenge #265. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 265-1: 33% Appearance
Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints. Write a script to
find an integer in the given array that appeared 33% or more.
If more than one found, return the smallest. If none found,
then return undef.

Example 1:
Input: @ints = (1,2,3,3,3,3,4,2)
Output: 3
1 appeared 1 times.
2 appeared 2 times.
3 appeared 4 times.
3 appeared 50% (>33%) in the given array.

Example 2:
Input: @ints = (1,1)
Output: 1
1 appeared 2 times.
1 appeared 100% (>33%) in the given array.

Example 3:
Input: @ints = (1,2,3)
Output: 1
1 appeared 1 times.
2 appeared 1 times.
3 appeared 1 times.
Since all three appeared 33.3% (>33%) in the given array.
We pick the smallest of all.

I'll make a hash %a to keep track of the abundances of the integers in @ints. I'll then start iterating through the forward-numeric-sorted keys of %a; if the value corresponding to any key is at least 0.33 times the size of @ints i'll stop iterating and return that value; otherwise I'll return undef.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 265-2: Completing Word
Submitted by: Mohammad Sajid Anwar
You are given a string, $str, containing alphnumeric characters,
and array of strings, @str (alphabetic characters only). Write
a script to find the shortest completing word. If none found
return empty string. A "completing word" is a word that contains
all the letters in the given string, ignoring space and number.
If a letter appeared more than once in the given string then it
must appear the same number or more in the word.

Example 1:
Input: $str = 'aBc 11c'
       @str = ('accbbb', 'abc', 'abbc')
Output: 'accbbb'
The given string contains following, ignoring case and number:
a 1 times
b 1 times
c 2 times
The only string in the given array that satisfies the condition
is 'accbbb'.

Example 2:
Input: $str = 'Da2 abc'
       @str = ('abcm', 'baacd', 'abaadc')
Output: 'baacd'
The given string contains following, ignoring case and number:
a 2 times
b 1 times
c 1 times
d 1 times
There are 2 strings that satisfies the condition:
'baacd' and 'abaadc'. Shortest of the two is 'baacd'

Example 3:
Input: $str = 'JB 007'
       @str = ('jj', 'bb', 'bjb')
Output: 'bjb'
The given string contains following, ignoring case and number:
j 1 times
b 1 times
The only string that satisfies the condition is 'bjb'.

I solve this problem by making a list of all words (if any) from @str which contain at least as many of each letter of $str as $str does. If that list has words, I return the shortest; else I return an empty string.

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

That's it for challenge 265; see you on challenge 266!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262