aboutsummaryrefslogtreecommitdiff
path: root/challenge-061
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-19 05:29:00 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-05-19 05:29:00 +0100
commite0eedc0b969d5c2a40ce01edeb6839c1fe540b91 (patch)
tree1d5c4ce84fdcb41190de86edb373c8d6d4ba5bc0 /challenge-061
parent74bed36e5eafeb25e8cbfbaa52bdb5bf08ab98be (diff)
downloadperlweeklychallenge-club-e0eedc0b969d5c2a40ce01edeb6839c1fe540b91.tar.gz
perlweeklychallenge-club-e0eedc0b969d5c2a40ce01edeb6839c1fe540b91.tar.bz2
perlweeklychallenge-club-e0eedc0b969d5c2a40ce01edeb6839c1fe540b91.zip
- Added Raku solution to the "IPv4 Partition" task.
Diffstat (limited to 'challenge-061')
-rw-r--r--challenge-061/mohammad-anwar/raku/ch-2.p647
-rw-r--r--challenge-061/mohammad-anwar/raku/ch-2a.p648
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-061/mohammad-anwar/raku/ch-2.p6 b/challenge-061/mohammad-anwar/raku/ch-2.p6
new file mode 100644
index 0000000000..f42cbbabc5
--- /dev/null
+++ b/challenge-061/mohammad-anwar/raku/ch-2.p6
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl6
+
+use v6.d;
+
+sub MAIN(Str $S = "25525511135") {
+ my Str @V = ipv4-partition($S);
+ say sprintf("%s = %s", $S, @V.join(", "));
+}
+
+sub ipv4-partition($ipv4_string) {
+
+ my @valid = Empty;
+ my $ipv4 = '';
+ my $size = $ipv4_string.chars;
+ for 1 .. $size-3 -> $i {
+ for $i+1 .. $size-2 -> $j {
+ for $j+1 .. $size-1 -> $k {
+ $ipv4 = $ipv4.substr(0, $k) ~ "." ~ $ipv4.substr($k)
+ if $ipv4.chars >= $k;
+ $ipv4 = $ipv4.substr(0, $j) ~ "." ~ $ipv4.substr($j)
+ if $ipv4.chars >= $j;
+ $ipv4 = $ipv4.substr(0, $i) ~ "." ~ $ipv4.substr($i)
+ if $ipv4.chars >= $i;
+
+ @valid.push: $ipv4 if is-valid($ipv4);
+ $ipv4 = $ipv4_string;
+ }
+ }
+ }
+
+ return @valid;
+}
+
+sub is-valid(Str $ipv4) {
+
+ return False if $ipv4.chars == 0;
+
+ for $ipv4.split(".") -> $octet {
+ return False if ($octet.chars > 3)
+ ||
+ ($octet.Int > 255)
+ ||
+ ($octet.chars > 1 && $octet ~~ /^0/);
+ }
+
+ return True;
+}
diff --git a/challenge-061/mohammad-anwar/raku/ch-2a.p6 b/challenge-061/mohammad-anwar/raku/ch-2a.p6
new file mode 100644
index 0000000000..a3fde1b5be
--- /dev/null
+++ b/challenge-061/mohammad-anwar/raku/ch-2a.p6
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl6
+
+use Test;
+
+my Str @V = ipv4-partition("25525511135");
+
+is @V.join(", "), "255.255.11.135, 255.255.111.35";
+
+done-testing;
+
+sub ipv4-partition($ipv4_string) {
+
+ my @valid = Empty;
+ my $ipv4 = '';
+ my $size = $ipv4_string.chars;
+ for 1 .. $size-3 -> $i {
+ for $i+1 .. $size-2 -> $j {
+ for $j+1 .. $size-1 -> $k {
+ $ipv4 = $ipv4.substr(0, $k) ~ "." ~ $ipv4.substr($k)
+ if $ipv4.chars >= $k;
+ $ipv4 = $ipv4.substr(0, $j) ~ "." ~ $ipv4.substr($j)
+ if $ipv4.chars >= $j;
+ $ipv4 = $ipv4.substr(0, $i) ~ "." ~ $ipv4.substr($i)
+ if $ipv4.chars >= $i;
+
+ @valid.push: $ipv4 if is-valid($ipv4);
+ $ipv4 = $ipv4_string;
+ }
+ }
+ }
+
+ return @valid;
+}
+
+sub is-valid(Str $ipv4) {
+
+ return False if $ipv4.chars == 0;
+
+ for $ipv4.split(".") -> $octet {
+ return False if ($octet.chars > 3)
+ ||
+ ($octet.Int > 255)
+ ||
+ ($octet.chars > 1 && $octet ~~ /^0/);
+ }
+
+ return True;
+}