aboutsummaryrefslogtreecommitdiff
path: root/challenge-061
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-05-21 20:51:25 +0800
committerGitHub <noreply@github.com>2020-05-21 20:51:25 +0800
commit296107a5ad9e98066dbc091814d0afcd4d4a745f (patch)
tree36d4cb845758896af0370b50e9cb9a61624f6209 /challenge-061
parent13c1c57de459227ae5914c68b1696c9dafe7ed0b (diff)
downloadperlweeklychallenge-club-296107a5ad9e98066dbc091814d0afcd4d4a745f.tar.gz
perlweeklychallenge-club-296107a5ad9e98066dbc091814d0afcd4d4a745f.tar.bz2
perlweeklychallenge-club-296107a5ad9e98066dbc091814d0afcd4d4a745f.zip
Rename ch-2.pl to challenge-061/cheok-yin-fung/perl/ch-2.pl
Diffstat (limited to 'challenge-061')
-rw-r--r--challenge-061/cheok-yin-fung/perl/ch-2.pl62
1 files changed, 62 insertions, 0 deletions
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]")};