diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-06-08 13:56:41 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-06-08 13:56:41 +0100 |
| commit | 14e4db4d669770384019246cd819e0842cc67a4e (patch) | |
| tree | d37eb54ab0c54bfb26a43f6694285dc822a7f95f /challenge-272 | |
| parent | 551bfbbdae65eea1b691a2749695dec2ad72206c (diff) | |
| download | perlweeklychallenge-club-14e4db4d669770384019246cd819e0842cc67a4e.tar.gz perlweeklychallenge-club-14e4db4d669770384019246cd819e0842cc67a4e.tar.bz2 perlweeklychallenge-club-14e4db4d669770384019246cd819e0842cc67a4e.zip | |
- Added solutions by Robbie Hatley.
- Added solutions by Luca Ferrari.
- Added solutions by PokGoPun.
- Added solutions by Roger Bell_West.
- Added solutions by Jan Krnavek.
- Added solutions by Jorg Sommrey.
- Added solutions by Adam Russell.
- Added solutions by Reinier Maliepaard.
Diffstat (limited to 'challenge-272')
| -rw-r--r-- | challenge-272/reinier-maliepaard/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-272/reinier-maliepaard/perl/ch-1.pl | 53 | ||||
| -rw-r--r-- | challenge-272/reinier-maliepaard/perl/ch-2.pl | 60 | ||||
| -rw-r--r-- | challenge-272/reinier-maliepaard/perl/ch-2a.pl | 16 |
4 files changed, 130 insertions, 0 deletions
diff --git a/challenge-272/reinier-maliepaard/blog.txt b/challenge-272/reinier-maliepaard/blog.txt new file mode 100644 index 0000000000..7d54963077 --- /dev/null +++ b/challenge-272/reinier-maliepaard/blog.txt @@ -0,0 +1 @@ +https://reiniermaliepaard.nl/perl/pwc/index.php?id=pwc272 diff --git a/challenge-272/reinier-maliepaard/perl/ch-1.pl b/challenge-272/reinier-maliepaard/perl/ch-1.pl new file mode 100644 index 0000000000..1c6542cc84 --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl +use strict; +use warnings; + +=begin + Here my solution TASK #1 along with alternatives from Niels van Dijke and James Curtis-Smith. + I did some benchmark (https://metacpan.org/pod/Benchmark) with 'timethese': + James' join+split is the fastest solution, even when the validation in Niels' and my + solution is removed +=cut + +use Data::Validate::IP qw(is_ipv4); + +sub defangIP { + + if (is_ipv4($_[0])) { + $_[0] =~ s/\./[.]/g; + } + else { + print("No valid IP address!"); + } + +# Inspired by Niels van Dijke's oneliner which uses the necessary /r switch +# (Niels uses for validation another module Net::IP) +# (is_ipv4($_[0])) ? $_[0] =~ s/\./[.]/gr : "No valid IP address!"; + +# James Curtis-Smith solution: +# join '[.]', split /\./, $_[0]; +} + +# TESTS +my $ip; + +# Example 1 +$ip = "1.1.1.1"; +print(defangIP($ip), "\n"); # Output: 1[.]1[.]1[.]1 + +# Example 2 +$ip = "255.101.1.0"; +print(defangIP($ip), "\n"); # Output: 255[.]101[.]1[.]0 + +# Example 3 +$ip = "123.234.345.001"; +print(defangIP($ip), "\n"); # Output: "No valid IP address! + +# Example 4 +# From: https://metacpan.org/pod/Data::Validate::IP +# There are security implications to this around certain oddly formed addresses. +# Notably, an address like "010.0.0.1" is technically valid, but the operating +# system will treat '010' as an octal number: '010.0.0.1' is interpreted as '8.0.0.1' +# James' solution and Niels' original do not take that into account. +$ip = "010.0.0.1"; +print(defangIP($ip), "\n"); # Output: "No valid IP address! diff --git a/challenge-272/reinier-maliepaard/perl/ch-2.pl b/challenge-272/reinier-maliepaard/perl/ch-2.pl new file mode 100644 index 0000000000..e67813f50c --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Benchmark qw(:all); + +=begin + Here is my solution for TASK #2, along with alternatives from + Niels van Dijke and James Curtis-Smith. I shared the benchmark results, + which show some variability when run multiple times. Nonetheless, James's + solution is the fastest. +=cut + +# Reinier Maliepaard: function to calculate total sum using split and for +sub ascii_differences_sum_split_1 { + my @chars = split(//, $_[0]); + my $total_sum = 0; + + for my $i (0 .. $#chars - 1) { + $total_sum += abs(ord($chars[$i]) - ord($chars[$i + 1])); + } + + return($total_sum); +} + +# James Curtis-Smith: function to calculate total sum using split and for +sub ascii_differences_sum_split_2 { + my ($t, @l) = (0, split //, $_[0]); + + my $f = ord shift @l; + + ($t += abs($f - ord)), $f=ord for @l; + + $t; +} + +# Niels van Dijke: function to calculate total sum using sum0 and slide +use List::Util qw(sum0); +use List::MoreUtils qw(slide); + +sub ascii_differences_sum_slide { + sum0 slide {abs(ord($b) - ord($a))} split '',$_[0] +} + +# A fanciful creation by fans, combining various Elvish elements from Tolkien's work. +my $input_string = "Aearenuialinorosinaiantirnoitisirsiensinoit"; + +# Benchmark all three methods +timethese(1000000, { + 'Using split_1' => sub { ascii_differences_sum_split_1($input_string) }, + 'Using split_2' => sub { ascii_differences_sum_split_2($input_string) }, + 'Using slide' => sub { ascii_differences_sum_slide($input_string) }, +}); + +=begin +Results: + Benchmark: timing 1000000 iterations of Using slide, Using split_1, Using split_2... + Using slide: 7 wallclock secs ( 6.55 usr + 0.00 sys = 6.55 CPU) @ 152671.76/s (n=1000000) + Using split_1: 7 wallclock secs ( 6.87 usr + 0.00 sys = 6.87 CPU) @ 145560.41/s (n=1000000) + Using split_2: 6 wallclock secs ( 5.76 usr + 0.00 sys = 5.76 CPU) @ 173611.11/s (n=1000000) +=cut diff --git a/challenge-272/reinier-maliepaard/perl/ch-2a.pl b/challenge-272/reinier-maliepaard/perl/ch-2a.pl new file mode 100644 index 0000000000..5c2b34ddce --- /dev/null +++ b/challenge-272/reinier-maliepaard/perl/ch-2a.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub ascii_differences_sum_split_2_alt +{ + my ( $t, @list ) = ( 0, split(//, $_[0]) ); + + my $f = ord( shift(@list) ); + + for (@list) { + $t += abs( $f - ord($_) ); + $f = ord($_); + } + return($t); +} |
