aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-07-29 12:12:24 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-07-29 12:12:24 -0400
commitf384236257071f69f71d3480ff423d039cee5598 (patch)
tree075c3956a18e60dacd19d17695ad2cfb86429742
parenta2a0393bed0a645143ff47e7c2ef85c67fa16528 (diff)
downloadperlweeklychallenge-club-f384236257071f69f71d3480ff423d039cee5598.tar.gz
perlweeklychallenge-club-f384236257071f69f71d3480ff423d039cee5598.tar.bz2
perlweeklychallenge-club-f384236257071f69f71d3480ff423d039cee5598.zip
Week 280
-rw-r--r--challenge-280/zapwai/c/ch-1.c34
-rw-r--r--challenge-280/zapwai/c/ch-2.c29
-rw-r--r--challenge-280/zapwai/c/vec.h293
-rw-r--r--challenge-280/zapwai/javascript/ch-1.js24
-rw-r--r--challenge-280/zapwai/javascript/ch-2.js21
-rw-r--r--challenge-280/zapwai/perl/ch-1.pl26
-rw-r--r--challenge-280/zapwai/perl/ch-2.pl19
-rw-r--r--challenge-280/zapwai/python/ch-1.py19
-rw-r--r--challenge-280/zapwai/python/ch-2.py17
-rw-r--r--challenge-280/zapwai/rust/ch-1.rs27
-rw-r--r--challenge-280/zapwai/rust/ch-2.rs25
11 files changed, 534 insertions, 0 deletions
diff --git a/challenge-280/zapwai/c/ch-1.c b/challenge-280/zapwai/c/ch-1.c
new file mode 100644
index 0000000000..0d0ba9efb3
--- /dev/null
+++ b/challenge-280/zapwai/c/ch-1.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include "vec.h"
+
+void proc(char* mystr) {
+ printf( "Input: %s\n", mystr);
+ char letter = '0';
+ vec *gotten = new_vec('c');
+ for (int i = 0; i < strlen(mystr); i++) {
+ char l = mystr[i];
+ if (gotten->size > 0) {
+ for (int j = 0; j < gotten->size; j++) {
+ char g = gotten->datac[j];
+ if (l == g)
+ letter = l;
+ }
+ }
+ if (letter != '0') {
+ break;
+ } else {
+ push(gotten, (char[]){l});
+ }
+ }
+ printf( "Output: %c\n", letter);
+}
+
+int main() {
+ char* mystr = "abcddbca";
+ proc(mystr);
+ char* mystr2 = "abcd";
+ proc(mystr2);
+ char* mystr3 = "abcdabbb";
+ proc(mystr3);
+}
+
diff --git a/challenge-280/zapwai/c/ch-2.c b/challenge-280/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..558fbed41f
--- /dev/null
+++ b/challenge-280/zapwai/c/ch-2.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <string.h>
+
+void proc(char mystr[]) {
+ printf("Input: %s\n", mystr);
+ int cnt = 0;
+ char* word;
+ word = strtok(mystr, "|");
+ int j = 0;
+ while (word != NULL) {
+ if (j % 2 == 0)
+ for (int i = 0; i < strlen(word); i++)
+ if (word[i] == '*')
+ cnt++;
+ word = strtok(NULL, "|");
+ j++;
+ }
+ printf("Output: %d\n", cnt);
+}
+
+int main() {
+ char mystr[] = "p|*e*rl|w**e|*ekly|";
+ proc(mystr);
+ char mystr2[] = "perl";
+ proc(mystr2);
+ char mystr3[] = "th|ewe|e**|k|l***ych|alleng|e";
+ proc(mystr3);
+}
+
diff --git a/challenge-280/zapwai/c/vec.h b/challenge-280/zapwai/c/vec.h
new file mode 100644
index 0000000000..ead044082e
--- /dev/null
+++ b/challenge-280/zapwai/c/vec.h
@@ -0,0 +1,293 @@
+#ifndef VEC_H
+#define VEC_H
+#include <stdlib.h>
+#include <string.h>
+#define VECMAXSTRLEN 50
+#define VECCAP 10
+typedef struct vector {
+ size_t cap; /* actual size allocated (reduces calls to realloc)*/
+ size_t size; /* size of data */
+ int* data; /* integer data */
+ char* datac; /* char data */
+ char** datas; /* string data */
+ char type; /* i, c, or s */
+} vec;
+
+vec* new_vec(char t) {
+ struct vector* v = malloc(sizeof(vec));
+ if ( t != 'c' && t != 'i' && t != 's') {
+ exit(1);
+ }
+ v->type = t;
+ v->size = 0;
+ v->cap = VECCAP;
+ v->data = malloc(v->cap * sizeof(int));
+ v->datac = malloc(v->cap * sizeof(char));
+ v->datas = malloc(v->cap * sizeof(char*));
+ for (int i = 0; i < v->cap; i++)
+ v->datas[i] = malloc(VECMAXSTRLEN);
+ return v;
+}
+
+void destroy_vec(struct vector* v) {
+ free (v->data); /* good habit */
+ free (v->datac);
+ for (int i = 0; i < v->cap; i++)
+ free(v->datas[i]);
+ free (v->datas);
+ free(v);
+}
+
+/* If a vector needs more memory, add VECCAP (e.g. 10) slots */
+void stretch_vec(struct vector* v) {
+ if (v->size == v->cap) {
+ v->cap += VECCAP;
+ switch(v->type) {
+ case 'i':
+ v->data = realloc(v->data, sizeof(int) * v->cap);
+ break;
+ case 'c':
+ v->datac = realloc(v->datac, sizeof(char) * v->cap);
+ break;
+ case 's':
+ v->datas = realloc(v->datas, sizeof(char*) * v->cap);
+ for (int i = v->size; i < v->cap; i++)
+ v->datas[i] = malloc(VECMAXSTRLEN);
+ break;
+ }
+ }
+}
+
+void push(struct vector* v, void* value) {
+ v->size++;
+ stretch_vec(v);
+ switch(v->type) {
+ case 'i':
+ v->data[v->size - 1] = *(int *) value;
+ break;
+ case 'c':
+ v->datac[v->size - 1] = *(char *) value;
+ break;
+ case 's':
+ strcpy(v->datas[v->size - 1], (char *) value);
+ break;
+ }
+}
+
+
+void unshift(struct vector* v, void* value) {
+ v->size++;
+ stretch_vec(v);
+ switch(v->type) {
+ case 'i':
+ for (int i = v->size - 1; i > 0; i--)
+ v->data[i] = v->data[i - 1];
+ v->data[0] = * (int*) value;
+ break;
+ case 'c':
+ for (int i = v->size - 1; i > 0; i--)
+ v->datac[i] = v->datac[i - 1];
+ v->datac[0] = * (char*) value;
+ break;
+ case 's':
+ for (int i = v->size - 1; i > 0; i--)
+ strcpy(v->datas[i], v->datas[i - 1]);
+ strcpy(v->datas[0], (char*) value);
+ break;
+ }
+}
+
+void* shift(struct vector* v) {
+ void *x = NULL;
+ if (v->size == 0) return NULL;
+ v->size--;
+ switch(v->type) {
+ case 'i':
+ x = malloc(sizeof(int));
+ if (x == NULL) return NULL;
+ *(int*)x = v->data[0];
+ for (int i = 0; i < v->size; i++)
+ v->data[i] = v->data[i + 1];
+ v->data[v->size] = 0;
+ break;
+ case 'c':
+ x = malloc(sizeof(char));
+ if (x == NULL) return NULL;
+ *(char *)x = v->datac[0];
+ for (int i = 0; i < v->size; i++)
+ v->datac[i] = v->datac[i + 1];
+ v->datac[v->size] = '\0';
+ break;
+ case 's':
+ x = malloc(VECMAXSTRLEN);
+ if (x == NULL) return NULL;
+ strcpy(x, v->datas[0]);
+ for (int i = 0; i < v->size; i++)
+ strcpy(v->datas[i], v->datas[i + 1]);
+ strcpy(v->datas[v->size], "");
+ break;
+ }
+ return x;
+}
+
+void* pop(struct vector* v) {
+ void *x = NULL;
+ if (v->size == 0) return NULL;
+ v->size--;
+ switch(v->type) {
+ case 'i':
+ x = malloc(sizeof(int));
+ *(int*)x = v->data[v->size];
+ v->data[v->size] = 0;
+ break;
+ case 'c':
+ x = malloc(sizeof(char));
+ *(char*)x = v->datac[v->size];
+ v->datac[v->size] = '\0';
+ break;
+ case 's':
+ x = malloc(VECMAXSTRLEN);
+ strcpy(x, v->datas[v->size]);
+ strcpy(v->datas[v->size], " ");
+ break;
+ }
+ return x;
+}
+
+void push_list(struct vector* v, void* list, int listlen) {
+ for (int i = 0; i < listlen; i++)
+ switch(v->type) {
+ case 'i':
+ push(v, &((int*)list)[i]);
+ break;
+ case 'c':
+ push(v, &((char*)list)[i]);
+ break;
+ case 's':
+ push(v, ((char**)list)[i]);
+ break;
+ }
+}
+
+void sort(struct vector* v) {
+ int cnt;
+ do {
+ cnt = 0;
+ for (int i = 0; i < v->size - 1; i++) {
+ switch(v->type) {
+ case 'i':
+ if (v->data[i] > v->data[i + 1]) {
+ cnt++;
+ int tmp = v->data[i];
+ v->data[i] = v->data[i+1];
+ v->data[i+1] = tmp;
+ }
+ break;
+ case 'c':
+ if (v->datac[i] > v->datac[i + 1]) {
+ cnt++;
+ char tmp = v->datac[i];
+ v->datac[i] = v->datac[i+1];
+ v->datac[i+1] = tmp;
+ }
+ break;
+ case 's':
+ if (strcmp(v->datas[i], v->datas[i + 1]) > 0) {
+ cnt++;
+ char* tmp = v->datas[i];
+ v->datas[i] = v->datas[i+1];
+ v->datas[i+1] = tmp;
+ }
+ break;
+ }
+ }
+ } while(cnt > 0);
+}
+
+void reverse(struct vector* v) {
+ if (v->size == 1) return;
+ int k = v->size / 2;
+ for (int i = 0; i < k; i++) {
+ int idx = (v->size % 2 == 0) ? k+i : k+1+i;
+ int idy = k - i - 1;
+ switch(v->type) {
+ case 'i':
+ {
+ int x = v->data[idy];
+ v->data[idy] = v->data[idx];
+ v->data[idx] = x;
+ }
+ break;
+ case 'c':
+ {
+ char x = v->datac[k-1-i];
+ v->datac[idy] = v->datac[idx];
+ v->datac[idx] = x;
+ }
+ break;
+ case 's':
+ {
+ char* x = v->datas[k-1-i];
+ v->datas[idy] = v->datas[idx];
+ v->datas[idx] = x;
+ }
+ break;
+ }
+ }
+}
+
+void display(struct vector* v) {
+ printf("[");
+ if (v->size > 0){
+ switch(v->type) {
+ case 'i':
+ for (int i = 0; i < v->size - 1; i++) printf("%d, ", v->data[i]);
+ printf("%d", v->data[v->size-1]);
+ break;
+ case 'c':
+ for (int i = 0; i < v->size - 1; i++) printf("%c, ", v->datac[i]);
+ printf("%c", v->datac[v->size-1]);
+ break;
+ case 's':
+ for (int i = 0; i < v->size - 1; i++) printf("%s, ", v->datas[i]);
+ printf("%s", v->datas[v->size-1]);
+ break;
+ }
+ }
+ printf("]");
+}
+
+void* get(struct vector* v, int k) {
+ void* x = NULL;
+ switch(v->type) {
+ case 'i':
+ x = malloc(sizeof(int));
+ *(int*) x = v->data[k];
+ break;
+ case 'c':
+ x = malloc(sizeof(char));
+ *(char*) x = v->datac[k];
+ break;
+ case 's':
+ x = malloc(VECMAXSTRLEN);
+ strcpy(x, v->datas[k]);
+ break;
+ }
+ return x;
+}
+
+void set(struct vector* v, int k, void* value) {
+ switch(v->type) {
+ case 'i':
+ v->data[k] = *(int*) value;
+ break;
+ case 'c':
+ v->datac[k] = *(char*) value;
+ break;
+ case 's':
+ strcpy(v->datas[k], (char*) value);
+ break;
+ }
+
+}
+#endif
diff --git a/challenge-280/zapwai/javascript/ch-1.js b/challenge-280/zapwai/javascript/ch-1.js
new file mode 100644
index 0000000000..a4eeaf501b
--- /dev/null
+++ b/challenge-280/zapwai/javascript/ch-1.js
@@ -0,0 +1,24 @@
+let mystr = "abcddbca";
+proc(mystr);
+mystr = "abcd";
+proc(mystr);
+mystr = "abcdabbb";
+proc(mystr);
+function proc(mystr) {
+ console.log("Input: ", mystr);
+ let letter = "";
+ let gotten = [];
+ for (let l of mystr.split("")) {
+ for (let g of gotten) {
+ if (l == g) {
+ letter = l;
+ }
+ }
+ if (letter != "") {
+ break;
+ } else {
+ gotten.push(l);
+ }
+ }
+ console.log("Output:", letter);
+}
diff --git a/challenge-280/zapwai/javascript/ch-2.js b/challenge-280/zapwai/javascript/ch-2.js
new file mode 100644
index 0000000000..e3a17ec1f1
--- /dev/null
+++ b/challenge-280/zapwai/javascript/ch-2.js
@@ -0,0 +1,21 @@
+let mystr = "p|*e*rl|w**e|*ekly|";
+proc(mystr);
+mystr = "perl";
+proc(mystr);
+mystr = "th|ewe|e**|k|l***ych|alleng|e";
+proc(mystr);
+function proc(mystr) {
+ console.log("Input:", mystr);
+ let cnt = 0;
+ let words = mystr.split('|');
+ for (let i = 0; i < words.length; i++) {
+ if (i % 2 == 0) {
+ for (let l of words[i].split("")) {
+ if (l == "*") {
+ cnt += 1;
+ }
+ }
+ }
+ }
+ console.log("Output:", cnt);
+}
diff --git a/challenge-280/zapwai/perl/ch-1.pl b/challenge-280/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..45b6cd74c9
--- /dev/null
+++ b/challenge-280/zapwai/perl/ch-1.pl
@@ -0,0 +1,26 @@
+use v5.38;
+my $str = "abcddbca";
+proc($str);
+$str = "abcd";
+proc($str);
+$str = "abcdabbb";
+proc($str);
+sub proc($str) {
+ say "Input: $str";
+ my $letter = "";
+ my @letters = split("", $str);
+ my @gotten;
+ for my $l (@letters) {
+ for my $g (@gotten) {
+ if ($l eq $g) {
+ $letter = $l;
+ }
+ }
+ if ($letter ne "") {
+ last;
+ } else {
+ push @gotten, $l;
+ }
+ }
+ say "Output: $letter";
+}
diff --git a/challenge-280/zapwai/perl/ch-2.pl b/challenge-280/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..eeefd23d4a
--- /dev/null
+++ b/challenge-280/zapwai/perl/ch-2.pl
@@ -0,0 +1,19 @@
+use v5.38;
+my $str = "p|*e*rl|w**e|*ekly|";
+proc($str);
+$str = "perl";
+proc($str);
+$str = "th|ewe|e**|k|l***ych|alleng|e";
+proc($str);
+sub proc($str) {
+ say "Input: $str";
+ my $cnt = 0;
+ my @words = split('\|', $str);
+ for my $i (0 .. $#words) {
+ if ($i % 2 == 0) {
+ my @letters = split("", $words[$i]);
+ $cnt += grep { $_ eq "*" } @letters;
+ }
+ }
+ say "Output: $cnt";
+}
diff --git a/challenge-280/zapwai/python/ch-1.py b/challenge-280/zapwai/python/ch-1.py
new file mode 100644
index 0000000000..4969fe9c49
--- /dev/null
+++ b/challenge-280/zapwai/python/ch-1.py
@@ -0,0 +1,19 @@
+def proc(mystr):
+ print("Input:", mystr)
+ letter = ""
+ gotten = []
+ for l in list(mystr):
+ for g in gotten:
+ if l == g:
+ letter = l
+ if letter != "":
+ break
+ else:
+ gotten.append(l)
+ print("Output:", letter)
+mystr = "abcddbca"
+proc(mystr)
+mystr = "abcd"
+proc(mystr)
+mystr = "abcdabbb"
+proc(mystr)
diff --git a/challenge-280/zapwai/python/ch-2.py b/challenge-280/zapwai/python/ch-2.py
new file mode 100644
index 0000000000..615cd89919
--- /dev/null
+++ b/challenge-280/zapwai/python/ch-2.py
@@ -0,0 +1,17 @@
+def proc(mystr):
+ print("Input:", mystr)
+ cnt = 0
+ words = mystr.split('|')
+ for i in range(len(words)):
+ if i % 2 == 0:
+ for l in list(words[i]):
+ if l == "*":
+ cnt += 1
+ print("Output:", cnt)
+mystr = "p|*e*rl|w**e|*ekly|"
+proc(mystr)
+mystr = "perl"
+proc(mystr)
+mystr = "th|ewe|e**|k|l***ych|alleng|e"
+proc(mystr)
+
diff --git a/challenge-280/zapwai/rust/ch-1.rs b/challenge-280/zapwai/rust/ch-1.rs
new file mode 100644
index 0000000000..ab82a4d94f
--- /dev/null
+++ b/challenge-280/zapwai/rust/ch-1.rs
@@ -0,0 +1,27 @@
+fn main() {
+ let mystr = "abcddbca";
+ proc(mystr);
+ let mystr2 = "abcd";
+ proc(mystr2);
+ let mystr3 = "abcdabbb";
+ proc(mystr3);
+}
+
+fn proc(mystr : &str) {
+ println!( "Input: mystr");
+ let mut letter : char = '0';
+ let mut gotten : Vec<char> = Vec::new();
+ for l in mystr.chars() {
+ for g in &gotten {
+ if l == *g {
+ letter = l;
+ }
+ }
+ if letter != '0' {
+ break;
+ } else {
+ gotten.push(l);
+ }
+ }
+ println!( "Output: {letter}");
+}
diff --git a/challenge-280/zapwai/rust/ch-2.rs b/challenge-280/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..90d8680b98
--- /dev/null
+++ b/challenge-280/zapwai/rust/ch-2.rs
@@ -0,0 +1,25 @@
+fn main() {
+ let mystr = "p|*e*rl|w**e|*ekly|";
+ proc(mystr);
+ let mystr2 = "perl";
+ proc(mystr2);
+ let mystr3 = "th|ewe|e**|k|l***ych|alleng|e";
+ proc(mystr3);
+}
+
+fn proc(mystr : &str) {
+ println!("Input: {mystr}");
+ let mut cnt = 0;
+ let words : Vec<&str>= mystr.split('|').collect();
+ for i in 0 .. words.len() {
+ if i % 2 == 0 {
+ let letters = words[i].chars();
+ for l in letters {
+ if l == '*' {
+ cnt += 1;
+ }
+ }
+ }
+ }
+ println!("Output: {cnt}");
+}