Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #352 (“Match String” and “Binary Prefix”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge

The Weekly Challenge for the week of 2025-12-15 through 2025-12-21 is #352. The tasks for challenge #352 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 352-1: Match String
Submitted by: Mohammad Sajid Anwar
You are given an array of strings. Write a script to return all
strings that are a substring of another word in the given array
in the order they occur.

Example #1:
Input:  ("cat", "cats", "dog", "dogcat", "dogcat", "rat", "ratcatdogcat")
Output: ("cat", "dog", "dogcat", "rat")

Example #2:
Input:  ("hello", "hell", "world", "wor", "ellow", "elloworld")
Output: ("hell", "world", "wor", "ellow")

Example #3:
Input:  ("a", "aa", "aaa", "aaaa")
Output: ("a", "aa", "aaa")

Example #4:
Input:  ("flower", "flow", "flight", "fl", "fli", "ig", "ght")
Output: ("flow", "fl", "fli", "ig", "ght")

Example #5:
Input:  ("car", "carpet", "carpenter", "pet", "enter", "pen", "pent")
Output: ("car", "pet", "enter", "pen", "pent")

I'll start by making hash array @out (for output) and hash "%used" (to avoid duplicates in @out). Then for each input word $word1, I'll skip it if it's used, or set $used{$word} = 1 if it isn't. I'll then compare each input word $word2 other than itself to pattern $word1 ($word2 =~m/$word1/); if a match occurs, I'll push $word1 to @out and skip to next $word1. Then just output @out.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 352-2: Binary Prefix
Submitted by: Mohammad Sajid Anwar
You are given an array, @nums, where each element is 0 or 1.
Define x[i] as the number formed by taking the first i+1 bits of
@nums (from $nums[0] through $nums[i]) and interpreting them as
a binary number, with $nums[0] being the most significant bit.
For example, if @nums = (1, 0, 1), then:
x0 = 1 (binary 1)
x1 = 2 (binary 10)
x2 = 5 (binary 101)
Write a script to return an array @answer where $answer[i] is
true if x[i] is divisible by 5, otherwise false.

Example #1:
Input: @nums = (0,1,1,0,0,1,0,1,1,1)
Output: (true, false, false, false, false, true, true, false, false, false)
Binary numbers formed (decimal values):
0: 0
01: 1
011: 3
0110: 6
01100: 12
011001: 25
0110010: 50
01100101: 101
011001011: 203
0110010111: 407

Example #2:
Input: @num = (1,0,1,0,1,0)
Output: (false, false, true, true, false, false)
1: 1
10: 2
101: 5
1010: 10
10101: 21
101010: 42

Example #3:
Input: @num = (0,0,1,0,1)
Output: (true, true, false, false, true)
0: 0
00: 0
001: 1
0010: 2
00101: 5

Example #4:
Input: @num = (1,1,1,1,1)
Output: (false, false, false, true, false)
1: 1
11: 3
111: 7
1111: 15
11111: 31

Example #5:
Input: @num = (1,0,1,1,0,1,0,0,1,1)
Output: (false, false, true, false, false, true, true, true, false, false)
1: 1
10: 2
101: 5
1011: 11
10110: 22
101101: 45
1011010: 90
10110100: 180
101101001: 361
1011010011: 723

To solve this problem, I'll use "join '', @num[0..$i]" to make each "binary prefix", tack a "0b" to its left, use "oct" to convert it to an integer $int, and use "if (0==$int%5) {...}" to check for divisibility by 5. Or combine all those steps in one line: if (0==oct('0b'.join '', @num[0..$i])%5) {push @out, 'true'} else {push @out, 'false'}

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

That's it for challenge 352; see you on challenge 353!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)