From 296107a5ad9e98066dbc091814d0afcd4d4a745f Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Thu, 21 May 2020 20:51:25 +0800 Subject: Rename ch-2.pl to challenge-061/cheok-yin-fung/perl/ch-2.pl --- challenge-061/cheok-yin-fung/perl/ch-2.pl | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 challenge-061/cheok-yin-fung/perl/ch-2.pl (limited to 'challenge-061') diff --git a/challenge-061/cheok-yin-fung/perl/ch-2.pl b/challenge-061/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..38ebcfd528 --- /dev/null +++ b/challenge-061/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +use strict; +#use Array::Compare, List::Compare; +#use Test::Simple tests => 7; + +sub threesum { + my $len = $_[0]; + my $remain = $len - $_[1] - $_[2] - $_[3]; + return (($remain >= 1) && ($remain <= 3)); +} + +sub possible_octet { + my $item = $_[0]; + if ($item =~ /^[0-9]$/ or $item =~ /^[1-9][0-9]$/ ) { + return 1; } + elsif ($item =~ /^[1-9][0-9][0-9]$/) { + return ( $item <= 255) ; + } + else { + return 0; + } + +} + + +sub myjoinsplice { + my $word = $_[0]; + my $offset = $_[1]; + my $section_len = $_[2]; + my @letters = split //, $word; + return ( join '', splice(@letters,$offset,$section_len) ) ; +} + + +sub ipv4 { + my @answers = (); + my $iplength = length $_[0]; + my $ip = $_[0]; + for my $i (1..3) { + for my $j (1..3) { + for my $k (1..3) { + if (threesum($iplength,$i,$j,$k) ) { + my $a = myjoinsplice($ip,0,$i); + my $b = myjoinsplice($ip,$i,$j); + my $c = myjoinsplice($ip,$i+$j,$k); + my $d = myjoinsplice($ip,$i+$j+$k, $iplength-$i-$j-$k); + if (possible_octet($a) and possible_octet($b) + and possible_octet($c) and possible_octet($d)) { + push @answers, (join '.' , $a,$b,$c,$d); + #print "$a $b $c $d","\n"; + # print join '.', $a,$b,$c,$d; print "\n"; + } + } + } + } + } + # print join " " , @answers; print "\n"; + return \@answers; +} + + +print join "\n", @{ipv4("$ARGV[0]")}; -- cgit