aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Ferrone <zapwai@gmail.com>2024-04-23 00:08:11 -0400
committerDavid Ferrone <zapwai@gmail.com>2024-04-23 00:08:11 -0400
commit81d6fff3c2e69e0435005908cb716deb24c70c02 (patch)
tree320fb9713e3aeb2bf8f7007b68e48600a520ad6b
parent2f1584c7fd61a21b0ea011763a8561ea64b716c9 (diff)
downloadperlweeklychallenge-club-81d6fff3c2e69e0435005908cb716deb24c70c02.tar.gz
perlweeklychallenge-club-81d6fff3c2e69e0435005908cb716deb24c70c02.tar.bz2
perlweeklychallenge-club-81d6fff3c2e69e0435005908cb716deb24c70c02.zip
C and Rust
-rw-r--r--challenge-265/zapwai/c/ch-2.c113
-rw-r--r--challenge-265/zapwai/rust/ch-2.rs66
2 files changed, 179 insertions, 0 deletions
diff --git a/challenge-265/zapwai/c/ch-2.c b/challenge-265/zapwai/c/ch-2.c
new file mode 100644
index 0000000000..a1e74ec231
--- /dev/null
+++ b/challenge-265/zapwai/c/ch-2.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdbool.h>
+
+typedef struct {
+ char* key;
+ int value;
+} item;
+
+item* val(item* items, size_t size, char* key) {
+ for (size_t i = 0; i < size; i++)
+ if (strcmp(items[i].key, key) == 0)
+ return &items[i];
+ return NULL;
+}
+
+void insert(item* items, size_t size, char* key, int val) {
+ int cnt = 0;
+ for (size_t i = 0; i < size; i++) {
+ if (strcmp(items[i].key, key) == 0) {
+ items[i].value = val;
+ break;
+ }
+ cnt++;
+ }
+ if (cnt == size) {
+ items[size].key = key;
+ items[size].value = val;
+ }
+}
+
+item* freq(char* stringy, int *size) {
+ item* f = malloc(100 * sizeof(item));
+ size_t f_size = 0;
+ for (int i = 0; i < strlen(stringy); i++) {
+ char letter = stringy[i];
+ if (isalpha(letter)) {
+ char let = tolower(letter);
+ item* cell = val(f, f_size, &let);
+ if (!cell) {
+ insert(f, f_size, &let, 1);
+ f_size++;
+ } else {
+ insert(f, f_size, &let, cell->value + 1);
+ }
+ }
+ }
+ *size = f_size;
+ return f;
+}
+
+// Return true if g contains f
+bool hash_contains(item* g, item* f, int g_size, int f_size) {
+ int cnt = 0;
+ for (int i = 0; i < f_size; i++) {
+ char* key = f[i].key;
+ item* gcell = val(g, g_size, key);
+ if (!gcell) {
+ return false;
+ } else {
+ item* fcell = val(f, f_size, key);
+ if (fcell->value <= gcell->value)
+ cnt++;
+ }
+ }
+ return (cnt == f_size);
+}
+
+void proc(char* stringy, char* strlist[], int slen) {
+ printf("Input: str = %s\n \tlist: ", stringy);
+ char* ans[slen];
+ int len = 0;
+ for (int i = 0; i < slen; i++) {
+ ans[i] = NULL;
+ int f_size = 0;
+ int g_size = 0;
+ if (hash_contains(freq(strlist[i], &g_size), freq(stringy, &f_size), g_size, f_size)) {
+ ans[len] = malloc(30 * sizeof(char));
+ strcpy(ans[len], strlist[i]);
+ len++;
+ }
+ printf("%s ", strlist[i]);
+ }
+ printf("\n");
+ int min = strlen(ans[0]);
+ char* answer = malloc(sizeof(char) * 30);
+ for (int i = 0; i < len; i++) {
+ if (strlen(ans[i]) < min) {
+ min = strlen(ans[i]);
+ strcpy(answer, ans[i]);
+ }
+ free(ans[i]);
+ }
+ if (len > 0)
+ printf("Output: %s\n", answer);
+ free(answer);
+}
+
+int main() {
+ char* stringy = "aBc 11c";
+ char* strlist[] = {"accbbb", "abc", "abbc"};
+ proc(stringy, strlist, 3);
+
+ stringy = "La1 abc";
+ char* strlist2[] = {"abcl", "baacl", "abaalc"};
+ proc(stringy, strlist2, 3);
+
+ stringy = "JB 007";
+ char* strlist3[] = {"jj", "bb", "bjb"};
+ proc(stringy, strlist3, 3);
+}
diff --git a/challenge-265/zapwai/rust/ch-2.rs b/challenge-265/zapwai/rust/ch-2.rs
new file mode 100644
index 0000000000..fe66eb6285
--- /dev/null
+++ b/challenge-265/zapwai/rust/ch-2.rs
@@ -0,0 +1,66 @@
+use std::collections::HashMap;
+
+fn main() {
+ let mut stringy = "aBc 11c";
+ let mut strlist = vec!["accbbb", "abc", "abbc"];
+ proc(stringy, strlist);
+
+ stringy = "La1 abc";
+ strlist = vec!["abcl", "baacl", "abaalc"];
+ proc(stringy, strlist);
+
+ stringy = "JB 007";
+ strlist = vec!["jj", "bb", "bjb"];
+ proc(stringy, strlist);
+}
+
+fn freq(stringy : &str) -> HashMap<char, i32> {
+ let mut f = HashMap::new();
+ for m in stringy.chars() {
+ if m.is_alphabetic() {
+ let _m = m.to_ascii_lowercase();
+ if f.contains_key(&_m) {
+ f.insert(_m, 1 + f.get(&_m).unwrap());
+ } else {
+ f.insert(_m, 1);
+ }
+ }
+ }
+ println!("f: {:?}", f);
+ return f;
+}
+
+// Return true if g contains f
+fn hash_contains(g :HashMap<char, i32>, f :HashMap<char, i32>) -> bool {
+ let mut cnt = 0;
+ for k in f.keys() {
+ if !g.contains_key(&k) {
+ continue;
+ }
+ if f[k] <= g[k] {
+ cnt += 1;
+ }
+ }
+ return cnt == f.keys().len();
+}
+
+fn proc(stringy : &str, strlist : Vec<&str>) {
+ println!("Input: string = {stringy}");
+ println!("\tlist = {:?}", strlist);
+ let mut ans : Vec<&str> = Vec::new();
+ for s in strlist {
+ if hash_contains(freq(s), freq(stringy)) {
+ ans.push(s);
+ }
+ }
+
+ let mut min = ans[0].len();
+ let mut answer = ans[0];
+ for i in 0 .. ans.len() {
+ if ans[i].len() < min {
+ min = ans[i].len();
+ answer = ans[i];
+ }
+ }
+ println!("Output: {answer}");
+}