diff options
| -rw-r--r-- | challenge-260/zapwai/c/ch-1.c | 84 | ||||
| -rw-r--r-- | challenge-260/zapwai/c/ch-2.c | 81 | ||||
| -rw-r--r-- | challenge-260/zapwai/javascript/ch-1.js | 21 | ||||
| -rw-r--r-- | challenge-260/zapwai/javascript/ch-2.js | 40 | ||||
| -rw-r--r-- | challenge-260/zapwai/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-260/zapwai/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-260/zapwai/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-260/zapwai/python/ch-2.py | 34 | ||||
| -rw-r--r-- | challenge-260/zapwai/rust/ch-1.rs | 30 | ||||
| -rw-r--r-- | challenge-260/zapwai/rust/ch-2.rs | 51 |
10 files changed, 436 insertions, 0 deletions
diff --git a/challenge-260/zapwai/c/ch-1.c b/challenge-260/zapwai/c/ch-1.c new file mode 100644 index 0000000000..ec3b7577b9 --- /dev/null +++ b/challenge-260/zapwai/c/ch-1.c @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <stdbool.h> +#define MAX 100 +#define BORK -111111 /* unusable key/value */ + +struct hash { + int key[MAX]; + int val[MAX]; + int length; +}; + +void init(struct hash *h) { + for (int i = 0; i < MAX; i++) { + h->key[i] = BORK; + h->val[i] = BORK; + } + h->length = 0; +} + +int get(struct hash h, int key) { + for (int i = 0; i < MAX; i++) + if (h.key[i] == key) + return h.val[i]; + return BORK; +} + +void set(struct hash *h, int key, int value) { + for (int i = 0; i < MAX; i++) + if (h->key[i] == key) + h->val[i] = value; +} + +void insert(struct hash *h, int key, int value) { + int index = h->length; + h->key[index] = key; + h->val[index] = value; + h->length++; +} + +bool contains(struct hash h, int key) { + for (int i = 0; i < MAX; i++) + if (h.key[i] == key) + return true; + return false; +} + +int has_uniq_freq(struct hash h) { + for (int i = 1; i < h.length; i++) + for (int j = 0; j < i; j++) + if (h.val[j] == h.val[i]) + return 0; + return 1; +} + +void proc(int len, int l[]) { + printf("Input: { "); + for (int i = 0; i < len; i++) + printf("%d ", l[i]); + printf("}\n"); + + struct hash f; + init(&f); + + for (int i = 0; i < len; i++) + if (contains(f, l[i])) + set(&f, l[i], get(f,l[i]) + 1); + else + insert(&f, l[i], 1); + + printf("Output %d\n", has_uniq_freq(f)); +} + +int main() { + int l1[] = {1,2,2,1,1,3}; + int l2[] = {1,2,3}; + int l3[] = {-2,0,1,-2,1,1,0,1,-2,9}; + int s1 = sizeof(l1) / sizeof(int); + int s2 = sizeof(l2) / sizeof(int); + int s3 = sizeof(l3) / sizeof(int); + proc(s1, l1); + proc(s2, l2); + proc(s3, l3); +} + diff --git a/challenge-260/zapwai/c/ch-2.c b/challenge-260/zapwai/c/ch-2.c new file mode 100644 index 0000000000..a499718f35 --- /dev/null +++ b/challenge-260/zapwai/c/ch-2.c @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#define MAX_WORD_LEN 50 + +void sortme(char** words, int wordlen) { + int cnt; + do { + cnt = 0; + for (int i = 0; i < wordlen - 1; i++) + if (strcmp(words[i+1], words[i]) < 0) { + char* tmp = words[i+1]; + words[i+1] = words[i]; + words[i] = tmp; + cnt++; + } + } while (cnt > 0); +} + +void insert_uniquely(char* word, int wordlen, char** h, int* hlen) { + for (int i = 0; i < *hlen; i++) + if (0 == strcmp(h[i], word)) + return; + h[*hlen] = malloc(MAX_WORD_LEN * sizeof(char)); + strncpy(h[*hlen], word, wordlen); + ++*hlen; +} + +int seek_word(char* word, char** sorted, int sortlen) { + for (int i = 0; i < sortlen; i++) + if (0 == strcmp(sorted[i], word)) + return i; + return -1; +} + +void swap(int i, int j, char list[]) { + char tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +void L(int k, char list[], char** h, int *hlen, int wordlength) { + if (k == 1) + insert_uniquely(list, wordlength, h, hlen); + else { + L(k - 1, list, h, hlen, wordlength); + for (int i = 0; i <= k - 2; i++) { + if (k % 2 == 0) + swap(i, k-1, list); + else + swap(0, k-1, list); + L(k - 1, list, h, hlen, wordlength); + } + } +} + +void proc(char* word) { + printf("Input: %s\n", word); + char** h = malloc(1000 * sizeof(char*)); + int hlen = 0; + + char list[strlen(word)]; + for (int i = 0; i < strlen(word); i++) + list[i] = word[i]; + + L(strlen(word), list, h, &hlen, strlen(word)); + + sortme(h, hlen); + + int ind = seek_word(word, h, hlen); + printf("Output: %d\n", 1 + ind); + for (int i = 0; i < hlen; i++) + free(h[i]); + free(h); +} + +int main() { + proc("CAT"); + proc("GOGGLE"); + proc("SECRET"); +} diff --git a/challenge-260/zapwai/javascript/ch-1.js b/challenge-260/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..3e4fbc4b42 --- /dev/null +++ b/challenge-260/zapwai/javascript/ch-1.js @@ -0,0 +1,21 @@ +let l1 = [1,2,2,1,1,3]; +let l2 = [1,2,3]; +let l3 = [-2,0,1,-2,1,1,0,1,-2,9]; +for (let l of [l1, l2, l3]) + proc(l); + +function proc(l) { + console.log("Input:", l); + let f = {}; + for (let i of l) + (i in f) ? f[i]++ : f[i] = 1; + let freq = []; + let output = 1; + for (let v of Object.values(f)) { + if (freq.includes(v)) + output = 0; + else + freq.push(v); + } + console.log("Output:", output); +} diff --git a/challenge-260/zapwai/javascript/ch-2.js b/challenge-260/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..c7078f49fa --- /dev/null +++ b/challenge-260/zapwai/javascript/ch-2.js @@ -0,0 +1,40 @@ +function proc(word) { + console.log("Input:", word); + let h = {}; + L(word.length, word.split(''), h); + let sorted = Object.keys(h).sort(); + console.log("Output:", 1 + seek_word(word, sorted)); +} + +function L(k, list, h) { + if (k == 1) + h[list.join('')] = 1; + else { + L(k-1, list, h); + for (let i = 0; i < k-1; i++) { + if (k % 2 == 0) + swap(i, k-1, list); + else + swap(0, k-1, list); + L(k-1, list, h); + } + } +} + +function swap(i, j, list) { + let tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +function seek_word(word, sorted) { + for (let i = 0; i < sorted.length; i++) + if (sorted[i] == word) + return i; + + return -1; +} + +proc("CAT"); +proc("GOGGLE"); +proc("SECRET"); diff --git a/challenge-260/zapwai/perl/ch-1.pl b/challenge-260/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..b4c19a9415 --- /dev/null +++ b/challenge-260/zapwai/perl/ch-1.pl @@ -0,0 +1,25 @@ +use v5.36; +my @ints = (1,2,2,1,1,3); +proc(@ints); +my @ints2 = (1,2,3); +proc(@ints2); +my @ints3 = (-2,0,1,-2,1,1,0,1,-2,9); +proc(@ints3); + +sub proc(@ints) { + say "Input: \@ints = (", join(", ", @ints).")"; + say "Output: ", has_uniq_freq(@ints); +} + +sub has_uniq_freq(@ints) { + my %f = map { $_, 0 } @ints; + $f{$_}++ foreach (@ints); + my %g = map { $_, 0 } (values %f); + $g{$_}++ foreach (values %f); + for ( values %g ) { + if ($_ > 1) { + return 0; + } + } + return 1; +} diff --git a/challenge-260/zapwai/perl/ch-2.pl b/challenge-260/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..d186c35bc3 --- /dev/null +++ b/challenge-260/zapwai/perl/ch-2.pl @@ -0,0 +1,43 @@ +use v5.36; + +sub proc($word) { + my %words; + say "Input: $word"; + my @let = split "", $word; + L(length $word, \@let, \%words); + my @sorted = sort keys %words; + say "Output: ", 1 + seek_word($word, @sorted); +} + +sub L($k, $list, $r) { + if ($k == 1) { + $$r{join("",@$list)} = 1; + } else { + L($k-1, $list, $r); + for my $i (0 .. $k-2) { + if ($k % 2 == 0) { + swap($i, $k-1, $list); + } else { + swap(0, $k-1, $list); + } + L($k-1, $list, $r); + } + } +} + +sub swap($i, $j, $list) { + my $hold = $$list[$i]; + $$list[$i] = $$list[$j]; + $$list[$j] = $hold; +} + +sub seek_word ($word, @sorted) { + for my $i (0 .. $#sorted) { + return $i if ($sorted[$i] eq $word); + } + return -1; +} + +proc("CAT"); +proc("GOOGLE"); +proc("SECRET"); diff --git a/challenge-260/zapwai/python/ch-1.py b/challenge-260/zapwai/python/ch-1.py new file mode 100644 index 0000000000..4de8664aea --- /dev/null +++ b/challenge-260/zapwai/python/ch-1.py @@ -0,0 +1,27 @@ +def has_uniq_freq(l): + freq = {} + for item in l: + if item in freq.keys(): + freq[item] += 1 + else: + freq[item] = 1 + gq = {} + for v in freq.values(): + if v in gq.keys(): + gq[v] += 1 + else: + gq[v] = 0 + for v in gq.values(): + if v > 1: + return 0 + return 1 + +def proc(l): + print("Input:", l); + print("Output:", has_uniq_freq(l)) + +l1 = [1,2,2,1,1,3] +l2 = [1,2,3] +l3 = [-2,0,1,-2,1,1,0,1,-2,9] +for l in [l1, l2, l3]: + proc(l) diff --git a/challenge-260/zapwai/python/ch-2.py b/challenge-260/zapwai/python/ch-2.py new file mode 100644 index 0000000000..bdfe08eca0 --- /dev/null +++ b/challenge-260/zapwai/python/ch-2.py @@ -0,0 +1,34 @@ +def seek_word(word, sorts): + for i in range(len(sorts)): + if sorts[i] == word: + return i + return -1 + +def L(k, mylist, h): + if k == 1: + h[''.join(mylist)] = 1 + else: + L(k-1, mylist, h) + for i in range(k-1): + if k % 2 == 0: + swap(i, k-1, mylist) + else: + swap(0, k-1, mylist) + L(k-1, mylist, h) + +def swap(i, j, mylist): + tmp = mylist[i] + mylist[i] = mylist[j] + mylist[j] = tmp + +def proc(word): + perms = {} + print("Input:", word) + let = list(word) + L(len(word), let, perms) + sorts = sorted(perms.keys()) + print("Output", 1 + seek_word(word, sorts)) + +proc("CAT") +proc("GOGGLE") +proc("SECRET") diff --git a/challenge-260/zapwai/rust/ch-1.rs b/challenge-260/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..7e0cf5afe2 --- /dev/null +++ b/challenge-260/zapwai/rust/ch-1.rs @@ -0,0 +1,30 @@ +use std::collections::HashMap; + +fn main() { + let l1 = [1,2,2,1,1,3]; + let l2 = [1,2,3]; + let l3 = [-2,0,1,-2,1,1,0,1,-2,9]; + let lists : Vec<&[i32]> = vec![&l1, &l2, &l3]; + for l in lists { + proc(&l); + } +} + +fn proc(l : &[i32]) { + println!("Input: {:?}", l); + let mut f = HashMap::new(); + for k in l { + let entry = f.entry(k).or_insert(0); + *entry += 1; + } + let mut freqs = vec![]; + let mut out = 1; + for v in f.values() { + if freqs.contains(v) { + out = 0; + } else { + freqs.push(*v); + } + } + println!("Output: {out}"); +} diff --git a/challenge-260/zapwai/rust/ch-2.rs b/challenge-260/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..62322619c3 --- /dev/null +++ b/challenge-260/zapwai/rust/ch-2.rs @@ -0,0 +1,51 @@ +use std::collections::HashMap; + +fn proc(word : &str) { + let mut h = HashMap::new(); + println!("Input: {}", word); + let mut letters = word.chars().collect::<Vec<char>>(); + l(word.len(), &mut letters, &mut h); + let mut sorted :Vec<String> = Vec::new(); + for k in h.keys() { + sorted.push(k.to_string()); + } + sorted.sort(); + println!("Output {}", 1 + seek_word(word, sorted)); +} + +fn l(k :usize, list : &mut Vec<char>, h : &mut HashMap<String, i32>) { + if k == 1 { + h.insert(list.iter().collect(), 1); + } else { + l(k - 1, list, h); + for i in 0 ..= k - 2 { + if k % 2 == 0 { + swap(i, k-1, list); + } else { + swap(0, k-1, list); + } + l(k-1, list, h); + } + } +} + +fn swap(i :usize, j :usize, list :&mut Vec<char>) { + let tmp = list[i]; + list[i] = list[j]; + list[j] = tmp; +} + +fn seek_word(word :&str, sorted : Vec<String>) -> usize{ + for i in 0 .. sorted.len() { + if sorted[i] == word.to_string(){ + return i; + } + } + return 10000; +} + +fn main() { + proc("CAT"); + proc("GOGGLE"); + proc("SECRET"); +} |
