aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-272/zapwai/c/ch-1.c29
-rw-r--r--challenge-272/zapwai/c/ch-2.c24
-rw-r--r--challenge-272/zapwai/javascript/ch-1.js11
-rw-r--r--challenge-272/zapwai/javascript/ch-2.js19
-rw-r--r--challenge-272/zapwai/perl/ch-1.pl12
-rw-r--r--challenge-272/zapwai/perl/ch-2.pl15
-rw-r--r--challenge-272/zapwai/python/ch-1.py15
-rw-r--r--challenge-272/zapwai/python/ch-2.py18
-rw-r--r--challenge-272/zapwai/rust/ch-1.rs18
-rw-r--r--challenge-272/zapwai/rust/ch-2.rs21
10 files changed, 182 insertions, 0 deletions
diff --git a/challenge-272/zapwai/c/ch-1.c b/challenge-272/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..5e1fad79bf
--- /dev/null
+++ b/challenge-272/zapwai/c/ch-1.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void proc(char* ip) {
+ char* out = malloc(100 * sizeof(char));
+ int j = 0;
+ for (int i = 0; i < strlen(ip); i++) {
+ if (ip[i] == '.') {
+ out[j++] = '[';
+ out[j++] = '.';
+ out[j] = ']';
+ } else {
+ out[j] = ip[i];
+ }
+ j++;
+ }
+ out[j] = '\0';
+ printf( "Input: %s\n", ip);
+ printf( "Output: %s\n", out);
+ free(out);
+}
+
+int main() {
+ char* ip = "1.1.1.1";
+ proc(ip);
+ char* ip2 = "255.101.1.0";
+ proc(ip2);
+}
diff --git a/challenge-272/zapwai/c/ch-2.c b/challenge-272/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..8f93ad2cd6
--- /dev/null
+++ b/challenge-272/zapwai/c/ch-2.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+void proc(char* str) {
+ int sum = 0;
+ int prev = str[0];
+ for (int i = 1; i < strlen(str); i++) {
+ int diff = abs(str[i] - prev);
+ sum += diff;
+ prev = str[i];
+ }
+ printf("Input: %s\n", str);
+ printf("Output: %d\n", sum);
+}
+
+int main() {
+ char* str1 = "hello";
+ proc(str1);
+ char* str2 = "perl";
+ proc(str2);
+ char* str3 = "raku";
+ proc(str3);
+}
diff --git a/challenge-272/zapwai/javascript/ch-1.js b/challenge-272/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..024387a534
--- /dev/null
+++ b/challenge-272/zapwai/javascript/ch-1.js
@@ -0,0 +1,11 @@
+let ip = "1.1.1.1";
+proc(ip);
+ip = "255.101.1.0";
+proc(ip);
+
+function proc(ip) {
+ let nums = ip.split('\.');
+ let out = nums.join("[.]");
+ console.log( "Input:", ip);
+ console.log( "Output:", out);
+}
diff --git a/challenge-272/zapwai/javascript/ch-2.js b/challenge-272/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..7d944f651c
--- /dev/null
+++ b/challenge-272/zapwai/javascript/ch-2.js
@@ -0,0 +1,19 @@
+let str = "hello";
+proc(str);
+str = "perl";
+proc(str);
+str = "raku";
+proc(str);
+
+function proc(str) {
+ let ords = [];
+ for (let i = 0; i < str.length; i++) {
+ ords.push(str.charCodeAt(i));
+ }
+ let sum = 0;
+ for (let i = 0; i < ords.length - 1; i++) {
+ sum += Math.abs(ords[i] - ords[i + 1]);
+ }
+ console.log( "Input:", str);
+ console.log( "Output:", sum);
+}
diff --git a/challenge-272/zapwai/perl/ch-1.pl b/challenge-272/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..ef46d3822f
--- /dev/null
+++ b/challenge-272/zapwai/perl/ch-1.pl
@@ -0,0 +1,12 @@
+use v5.38;
+my $ip = "1.1.1.1";
+proc($ip);
+$ip = "255.101.1.0";
+proc($ip);
+
+sub proc($ip) {
+ my @nums = split '\.', $ip;
+ my $out = join "[.]", @nums;
+ say "Input: $ip";
+ say "Output: $out";
+}
diff --git a/challenge-272/zapwai/perl/ch-2.pl b/challenge-272/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..4676c3ee0a
--- /dev/null
+++ b/challenge-272/zapwai/perl/ch-2.pl
@@ -0,0 +1,15 @@
+use v5.38;
+my $str = "hello";
+proc($str);
+$str = "perl";
+proc($str);
+$str = "raku";
+proc($str);
+
+sub proc($str) {
+ my @ord = map { ord $_ } split "", $str;
+ my $sum = 0;
+ $sum += abs($ord[$_] - $ord[$_ + 1]) for (0 .. $#ord - 1);
+ say "Input: $str";
+ say "Output: $sum";
+}
diff --git a/challenge-272/zapwai/python/ch-1.py b/challenge-272/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..d254cbd0ac
--- /dev/null
+++ b/challenge-272/zapwai/python/ch-1.py
@@ -0,0 +1,15 @@
+def proc(ip):
+ nums = ip.split('.')
+ out = ''
+ for i in range(len(nums)):
+ str = nums[i]
+ if i < len(nums) - 1:
+ str += "[.]"
+ out += str
+ print("Input:", ip)
+ print("Output:", out)
+
+ip = "1.1.1.1"
+proc(ip)
+ip = "255.101.1.0"
+proc(ip)
diff --git a/challenge-272/zapwai/python/ch-2.py b/challenge-272/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..4d488a2ab5
--- /dev/null
+++ b/challenge-272/zapwai/python/ch-2.py
@@ -0,0 +1,18 @@
+def proc(str):
+ chars = list(str)
+ num = []
+ for c in chars:
+ num.append(ord(c))
+ sum = 0
+ for i in range(len(num) - 1):
+ sum += abs(num[i] - num[i + 1])
+ print("Input:", str)
+ print("Output:", sum)
+
+str = "hello"
+proc(str)
+str = "perl"
+proc(str)
+str = "raku"
+proc(str)
+
diff --git a/challenge-272/zapwai/rust/ch-1.rs b/challenge-272/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..fdea6f414f
--- /dev/null
+++ b/challenge-272/zapwai/rust/ch-1.rs
@@ -0,0 +1,18 @@
+fn main() {
+ let ip = "1.1.1.1";
+ proc(ip);
+ let ip2 = "255.101.1.0";
+ proc(ip2);
+}
+
+fn proc(ip : &str) {
+ let nums : Vec<&str> = ip.split(".").collect();
+ let mut pie : String = String::new();
+ for p in &nums {
+ pie += p;
+ pie += "[.]";
+ }
+ let out = &pie[..pie.len()-3];
+ println!( "Input: {ip}");
+ println!( "Output: {out}");
+}
diff --git a/challenge-272/zapwai/rust/ch-2.rs b/challenge-272/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..c78c8b1e14
--- /dev/null
+++ b/challenge-272/zapwai/rust/ch-2.rs
@@ -0,0 +1,21 @@
+fn main() {
+ let str = "hello";
+ proc(str);
+ let str2 = "perl";
+ proc(str2);
+ let str3 = "raku";
+ proc(str3);
+}
+
+fn proc(stri : &str) {
+ let mut sum = 0;
+ let strin : Vec<char> = stri.chars().collect();
+ let mut prev = strin[0];
+ for i in 0 .. strin.len() {
+ let diff = strin[i] as i32 - prev as i32;
+ prev = strin[i];
+ sum += diff.abs();
+ }
+ println!( "Input: {stri}");
+ println!( "Output: {sum}");
+}