Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #363 (“String Lie Detector” and “Subnet Sheriff”)
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 2026-03-02 through 2026-03-08 is #363. The tasks for challenge #363 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 363-1: String Lie Detector Submitted by: Mohammad Sajid Anwar You are given a string. Write a script that parses a self-referential string and determines whether its claims about itself are true. The string will make statements about its own composition, specifically the number of vowels and consonants it contains. ( # Example #1 input: "aa — two vowels and zero consonants", # Expected output: true # Example #2 input: "iv — one vowel and one consonant", # Expected output: true # Example #3 input: "hello - three vowels and two consonants", # Expected output: false # Example #4 input: "aeiou — five vowels and zero consonants", # Expected output: true input: # Example #5 input: "aei — three vowels and zero consonants", # Expected output: true );
I'll assume that the input stream will always match this regexp:
^\pL+[\pP\pZ]+\pL+\pZ+\pL+\pZ+\pL+\pZ+\pL+\pZ+\pL+$
So I'll split on /[\pP\pZ]+/ and store the result to an array:
my $fields = split /[\pP\pZ]+/, $string;
Then I'll return "true" if ALL of the following are true, else return "false":
- Number of fields is 6
- Second field is number.
- Third field is "vowel(s)".
- Fourth field is "and"
- Fifth field is number.
- Sixth field is "consonant(s)".
- Second field accurately describes number of vowels in first field.
- Fifth field accurately describes number of consonants in first field.
That approach will work for numbers up to 20. Larger numbers would require more-elaborate programming, which I don't have time for, so this approach will have to do.
Robbie Hatley's Perl Solution to The Weekly Challenge 363-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 363-2: Subnet Sheriff
Submitted by: Peter Campbell Smith
You are given an IPv4 address and an IPv4 network (in CIDR
format). Write a script to determine whether both are valid and
the address falls within the network. For more information, see
this Wikipedia article:
https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
Examples:
(
# Example #1:
[
"192.168.1.45",
"192.168.1.0/24",
],
# Expected output: true
# Example #2:
[
"10.0.0.256",
"10.0.0.0/24",
],
# Expected output: false
# Example #3:
[
"172.16.8.9",
"172.16.8.9/32",
],
# Expected output: true
# Example #4:
[
"172.16.4.5",
"172.16.0.0/14",
],
# Expected output: true
# Example #5:
[
"192.0.2.0",
"192.0.2.0/25",
],
# Expected output: true
);
I'll first check that the two IP addresses are valid (4 dot-separated positive decimal integers in the 0-255 range), then check that the subnet is valid (a positive decimal integer in the 0-32 range). Then check that the "$ip_addr" is in the range of the "$domain" by converting the two IP addresses to integer, then making the subnet mask correspoding to the bitcount, then bitwise-anding the mask against the two addresses and seeing if the results match.
Robbie Hatley's Perl Solution to The Weekly Challenge 363-2
That's it for challenge 363; see you on challenge 364!
Comments
Post a Comment