diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-05 21:50:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-05 21:50:13 +0100 |
| commit | b903d4f642c74b1052c2aeb4ddc2684c405b1e18 (patch) | |
| tree | 0fb7f978fbbbb8686319460d038b49628364bc33 | |
| parent | fa77b28520ab385145cace74ac5b82642e58b0ef (diff) | |
| parent | 454b616b1d087ad5f9aea815091d771a4a28b6b9 (diff) | |
| download | perlweeklychallenge-club-b903d4f642c74b1052c2aeb4ddc2684c405b1e18.tar.gz perlweeklychallenge-club-b903d4f642c74b1052c2aeb4ddc2684c405b1e18.tar.bz2 perlweeklychallenge-club-b903d4f642c74b1052c2aeb4ddc2684c405b1e18.zip | |
Merge pull request #10216 from robbie-hatley/rh272
Robbie Hatley's Perl solutions for The Weekly Challenge #272.
| -rwxr-xr-x | challenge-272/robbie-hatley/perl/ch-1.pl | 67 | ||||
| -rwxr-xr-x | challenge-272/robbie-hatley/perl/ch-2.pl | 84 |
2 files changed, 151 insertions, 0 deletions
diff --git a/challenge-272/robbie-hatley/perl/ch-1.pl b/challenge-272/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..dffc2b9110 --- /dev/null +++ b/challenge-272/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 272-1. +Written by Robbie Hatley on Mon Jun 03, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 272-1: Defang IP Address +Submitted by: Mohammad Sajid Anwar +You are given a valid IPv4 address. Write a script to return the +defanged version of the given IP address. A "defanged" IP address +replaces every period “.” with “[.]". + +Example 1: +Input: $ip = "1.1.1.1" +Output: "1[.]1[.]1[.]1" + +Example 2: +Input: $ip = "255.101.1.0" +Output: "255[.]101[.]1[.]0" + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +The "s" operator will make quick work of this: + + sub defang ($x) {$x =~ s/\./[.]/gr} + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of double-quoted IP4 strings, in proper Perl syntax, like so: +./ch-1.pl '("3.87.253.6", "8.8.8.8", "2.4.4.2", "192.168.67.84")' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + sub defang ($x) {$x =~ s/\./[.]/gr} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @strings = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + "1.1.1.1", + # Expected output: "1[.]1[.]1[.]1" + + # Example 2 input: + "255.101.1.0", + # Output: "255[.]101[.]1[.]0" +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $string (@strings) { + say ''; + say 'Original: ', $string; + say 'Defanged: ', defang($string); +} diff --git a/challenge-272/robbie-hatley/perl/ch-2.pl b/challenge-272/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..f18239cf0d --- /dev/null +++ b/challenge-272/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 272-2. +Written by Robbie Hatley on Mon Jun 03, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 272-2: String Score +Submitted by: Mohammad Sajid Anwar +You are given an ASCII string, $str. Write a script to return +the "score" of $str, where "score" is defined as the sum of +the absolute difference between the ASCII values of adjacent +characters. + +Example 1: Input: "hello" Output: 13 + +Example 2: Input: "perl" Output: 30 + +Example 3: Input: "raku" Output: 37 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This is just a matter of using ord() and abs(): + + sub score ($x) { + my @chars = split //, $x; + my $score = 0; + for ( my $idx = 0 ; $idx <= $#chars - 1 ; ++$idx ) { + $score += abs(ord($chars[$idx])-ord($chars[$idx+1])); + } + return $score; + } + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of double-quoted ASCII strings, in proper Perl syntax, like so: +./ch-2.pl '("Azathoth", "one two three", "zavazavabu")' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + sub score ($string) { + my @chars = split //, $string; + my $score = 0; + for ( my $idx = 0 ; $idx <= $#chars - 1 ; ++$idx ) { + $score += abs(ord($chars[$idx])-ord($chars[$idx+1])); + } + return $score; + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @strings = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + "hello", + # Expected output: 13 + + # Example 2 input: + "perl", + # Expected output: 30 + + # Example 3 input: + "raku", + # Expected output: 37 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $string (@strings) { + say ''; + say 'String: ', $string; + say 'Score : ', score($string); +} |
