diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-17 10:43:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-17 10:43:49 +0100 |
| commit | 4c82c665be83d12cf9bcd5c0443fa678eb51198c (patch) | |
| tree | 999961fe310a55ed5e7b3b76a133e08f69e0b22e | |
| parent | 2de4c40920246e5f27a85e3d577e57a086d40cb7 (diff) | |
| parent | 5c3cc14a7646b6845778b008aa1cafbd54068790 (diff) | |
| download | perlweeklychallenge-club-4c82c665be83d12cf9bcd5c0443fa678eb51198c.tar.gz perlweeklychallenge-club-4c82c665be83d12cf9bcd5c0443fa678eb51198c.tar.bz2 perlweeklychallenge-club-4c82c665be83d12cf9bcd5c0443fa678eb51198c.zip | |
Merge pull request #10108 from zapwai/branch-for-269
week 269
| -rw-r--r-- | challenge-269/zapwai/c/ch-1.c | 80 | ||||
| -rw-r--r-- | challenge-269/zapwai/c/ch-2.c | 44 | ||||
| -rw-r--r-- | challenge-269/zapwai/c/vec.h | 94 | ||||
| -rw-r--r-- | challenge-269/zapwai/javascript/ch-1.js | 50 | ||||
| -rw-r--r-- | challenge-269/zapwai/javascript/ch-2.js | 21 | ||||
| -rw-r--r-- | challenge-269/zapwai/python/ch-1.py | 37 | ||||
| -rw-r--r-- | challenge-269/zapwai/python/ch-2.py | 20 | ||||
| -rw-r--r-- | challenge-269/zapwai/rust/ch-1.rs | 57 | ||||
| -rw-r--r-- | challenge-269/zapwai/rust/ch-2.rs | 26 |
9 files changed, 429 insertions, 0 deletions
diff --git a/challenge-269/zapwai/c/ch-1.c b/challenge-269/zapwai/c/ch-1.c new file mode 100644 index 0000000000..4bb9e78c72 --- /dev/null +++ b/challenge-269/zapwai/c/ch-1.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <math.h> +#include <stdbool.h> +#include "vec.h" + +void proc(int ints[], int N); +bool truth(int* ints, int N); + +int main() { + int ints[] = {1, 2, 3, 4, 5}; + int ints2[] = {2, 3, 8, 16}; + int ints3[] = {1, 3, 5, 7, 9}; + proc(ints, sizeof(ints) / sizeof(int)); + proc(ints2, sizeof(ints2) / sizeof(int)); + proc(ints3, sizeof(ints3) / sizeof(int)); +} + +int* dec_to_bin(int x, int N) { + int* b = malloc(sizeof(int) * (N+1)); + for (int i = 0; i <= N; i++) { + if (x >= pow(2, N - i)) { + b[i] = 1; + x -= pow(2, N - i); + } else { + b[i] = 0; + } + } + return b; +} + +bool truth(int* ints, int N) { + for (int i = 0; i < pow(2, N); i++) { + int d[80] = {}; + int* b = dec_to_bin(i, N-1); + /* for (int j = 0; j < N; j++) */ + /* printf("%d ", b[j]); */ + /* printf("\n"); */ + + vec current_list; + init_vec(¤t_list); + for (int j = 0; j < N; j++) + if (1 == b[j]) + push(¤t_list, ints[j]); + free(b); + if (1 >= current_list.size) + continue; + int tally = 0; + for (int j = 0; j < current_list.size; j++) + tally = tally | current_list.data[j]; + + int* x = dec_to_bin(tally, N-1); + int last_bin_dig = x[N-1]; + if (last_bin_dig == 0) { + display(¤t_list); + printf(" -> "); + for (int j = 0; j < N; j++) + printf("%d", x[j]); + destroy_vec(¤t_list); + free(x); + return 1; + } + free(x); + destroy_vec(¤t_list); + } + return 0; +} + +void proc(int ints[], int N) { + printf("Input: ints = { "); + for (int i = 0; i < N; i++) + printf("%d ", ints[i]); + printf("}\n"); + printf("Output: "); + if (truth(ints, N)) { + printf(" true\n"); + } else { + printf("false\n"); + } +} + diff --git a/challenge-269/zapwai/c/ch-2.c b/challenge-269/zapwai/c/ch-2.c new file mode 100644 index 0000000000..c1862ff13e --- /dev/null +++ b/challenge-269/zapwai/c/ch-2.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include "vec.h" + +void proc(int ints[], int N) { + printf( "Input: ints = { " ); + for (int i = 0; i < N; i++) + printf("%d ", ints[i]); + printf("}\n"); + vec arr1; init_vec(&arr1); + vec arr2; init_vec(&arr2); + int cnt = 2; + push(&arr1, ints[0]); + push(&arr2, ints[1]); + + while (cnt < N) { + int x = ints[cnt]; + cnt++; + if (arr1.data[arr1.size-1] > arr2.data[arr2.size-1]) { + push(&arr1, x); + } else { + push(&arr2, x); + } + } + printf("Output: arr1 = { "); + for (int i = 0; i < arr1.size; i++) + printf("%d ", arr1.data[i]); + + printf("} arr2 = { "); + for (int i = 0; i < arr2.size; i++) + printf("%d ", arr2.data[i]); + printf("}\n"); + destroy_vec(&arr1); + destroy_vec(&arr2); +} + +int main() { + int ints[] = {2, 1, 3, 4, 5}; + int ints2[] = {3,2,4}; + int ints3[] = {5, 4, 3, 8}; + proc(ints, sizeof(ints)/sizeof(int)); + proc(ints2, sizeof(ints2)/sizeof(int)); + proc(ints3, sizeof(ints3)/sizeof(int)); +} + diff --git a/challenge-269/zapwai/c/vec.h b/challenge-269/zapwai/c/vec.h new file mode 100644 index 0000000000..a769498c92 --- /dev/null +++ b/challenge-269/zapwai/c/vec.h @@ -0,0 +1,94 @@ +#ifndef VEC_H +#define VEC_H +#include <stdlib.h> +typedef struct vector { + size_t cap; /* actual size allocated (reduces calls to realloc)*/ + size_t size; /* size of data */ + int* data; /* integer data */ +} vec; + +void init_vec(struct vector* v) { + v->size = 0; + v->cap = 10; + v->data = malloc(v->cap * sizeof(int)); +} +void destroy_vec(struct vector* v) { + free (v->data); /* good habit */ +} + +/* If a vector needs more memory, add 10 slots */ +void stretch_vec(struct vector* v) { + if (v->size == v->cap) { + v->cap += 10; + v->data = realloc(v->data, sizeof(int) * v->cap); + } +} +void push(struct vector* v, int value) { + v->size++; + stretch_vec(v); + v->data[v->size - 1] = value; +} +void unshift(struct vector* v, int value) { + v->size++; + stretch_vec(v); + for (int i = v->size - 1; i > 0; i--) { + v->data[i] = v->data[i - 1]; + } + v->data[0] = value; +} +int shift(struct vector* v) { + int x = v->data[0]; + v->size--; + for (int i = 0; i < v->size; i++) { + v->data[i] = v->data[i + 1]; + } + v->data[v->size] = 0; + return x; +} +int pop(struct vector* v) { + int x = v->data[v->size-- - 1]; + v->data[v->size] = 0; + return x; +} + +void push_list(struct vector* v, int* list) { + // ... +} + +void sort(struct vector* v) { + int cnt; + do { + cnt = 0; + for (int i = 0; i < v->size - 1; 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; + } + } + } while(cnt > 0); +} + +void reverse(struct vector* v) { + int k = v->size / 2; + for (int i = 0; i < k; i++) { + int x = v->data[k-1-i]; + int idx = (v->size % 2 == 0) ? k+i : k+1+i; + v->data[k-1-i] = v->data[idx]; + v->data[idx] = x; + } +} + +void display(struct vector* v) { + printf("["); + if (v->size > 0){ + for (int i = 0; i < v->size - 1; i++) + printf("%d, ", v->data[i]); + printf("%d", v->data[v->size-1]); + } + printf("]"); +} + +/* to do : allow list of chars, or strings. Maybe a union for data? */ +#endif diff --git a/challenge-269/zapwai/javascript/ch-1.js b/challenge-269/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..f60ece69cb --- /dev/null +++ b/challenge-269/zapwai/javascript/ch-1.js @@ -0,0 +1,50 @@ +let ints = [1, 2, 3, 4, 5]; +let ints2 = [2, 3, 8, 16]; +let ints3 = [1, 3, 5, 7, 9]; +proc(ints); +proc(ints2); +proc(ints3); + +function truth(ints) { + let N = ints.length; + for (let i = 0; i < Math.pow(2,N) - 1; i++) { + let d = i.toString(2); + let D = d.split(""); + while (D.length < N) { + if (D.length < N) { + D.unshift(0); + } + } + let current_list = []; + for (let j = 0; j < D.length; j++) { + if (1 == D[j]) { + current_list.push(ints[j]); + } + } + if (1 >= current_list.length) { + continue; + } + let tally = 0; + for (let c of current_list) { + tally = tally | c; + } + let x = tally.toString(2); + let last_bin_dig = x.slice(-1); + console.log(d, x, last_bin_dig); + if (last_bin_dig == "0") { + console.log(current_list,"->", x); + return 1; + } + } + return 0; +} + +function proc(ints) { + console.log("Input:", ints); + if (truth(ints)) { + console.log( "Output: true" ); + } else { + console.log( "Output: false" ); + } +} + diff --git a/challenge-269/zapwai/javascript/ch-2.js b/challenge-269/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..f7d71c8e34 --- /dev/null +++ b/challenge-269/zapwai/javascript/ch-2.js @@ -0,0 +1,21 @@ +let ints = [2, 1, 3, 4, 5]; +let ints2 = [3,2,4]; +let ints3 = [5, 4, 3, 8]; +proc(ints); +proc(ints2); +proc(ints3); + +function proc(ints) { + console.log( "Input: ints = ", ints ); + let arr1 = [ints.shift()]; + let arr2 = [ints.shift()]; + while (ints.length > 0) { + let x = ints.shift(); + if (arr1[arr1.length-1] > arr2[arr2.length-1]) { + arr1.push(x); + } else { + arr2.push(x); + } + } + console.log("Output: arr1 = ", arr1, "arr2 = ", arr2); +} diff --git a/challenge-269/zapwai/python/ch-1.py b/challenge-269/zapwai/python/ch-1.py new file mode 100644 index 0000000000..835d197c69 --- /dev/null +++ b/challenge-269/zapwai/python/ch-1.py @@ -0,0 +1,37 @@ +def truth(ints): + N = len(ints) + + for i in range(2**N): + d = format(i,'b') + D = list(d) + for i in range(N - len(d)): + D.insert(0, '0') + current_list = [] + for j in range(len(D)): + if '1' == D[j]: + current_list.append(ints[j]) + if 1 >= len(current_list): + continue + tally = 0 + for c in current_list: + tally = tally | c + x = format(tally,'b') + last_dig = x[-1:] + if last_dig == '0': + print("\t-->",current_list, x) + return 1 + return 0 + +def proc(ints): + print("Input:", ints) + if truth(ints): + print( "Output: true" ) + else: + print( "Output: false" ) + +ints = [1, 2, 3, 4, 5] +ints2 = [2, 3, 8, 16] +ints3 = [1, 3, 5, 7, 9] +proc(ints) +proc(ints2) +proc(ints3) diff --git a/challenge-269/zapwai/python/ch-2.py b/challenge-269/zapwai/python/ch-2.py new file mode 100644 index 0000000000..e4d7cebe5f --- /dev/null +++ b/challenge-269/zapwai/python/ch-2.py @@ -0,0 +1,20 @@ +def proc(ints): + print("Input: ints = ", ints) + ints.reverse() + arr1 = [ints.pop()] + arr2 = [ints.pop()] + while len(ints) > 0: + x = ints.pop() + if arr1[len(arr1)-1] > arr2[len(arr2)-1]: + arr1.append(x) + else: + arr2.append(x) + print("Output: arr1 = ", arr1, "arr2 = ", arr2) + +ints = [2, 1, 3, 4, 5] +ints2 = [3,2,4] +ints3 = [5, 4, 3, 8] +proc(ints) +proc(ints2) +proc(ints3) + diff --git a/challenge-269/zapwai/rust/ch-1.rs b/challenge-269/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..5450f570d1 --- /dev/null +++ b/challenge-269/zapwai/rust/ch-1.rs @@ -0,0 +1,57 @@ +fn main() { + let ints = vec![1, 2, 3, 4, 5]; + let ints2 = vec![2, 3, 8, 16]; + let ints3 = vec![1, 3, 5, 7, 9]; + proc(ints); + proc(ints2); + proc(ints3); +} + +fn truth(ints : Vec<i32>) -> bool { + let N :u32 = ints.len() as u32; + for i in 0 .. 2_i32.pow(N) - 1 { + let d = format!("{:b}",i); + let mut D : Vec<&str> = d.split("").collect(); + D.pop(); + D.reverse(); + D.pop(); + D.reverse(); + for _j in 0 .. N - (d.len() as u32) { + D.insert(0, "0"); + } + let mut current_list : Vec<i32> = Vec::new(); + for j in 0 .. D.len() { + if "1" == D[j] { + current_list.push(ints[j]); + } + } + + if 1 >= current_list.len() { + continue; + } + let mut tally = 0; + for c in ¤t_list { + tally = tally | c; + } + + let x = format!("{:b}", tally); + let last_bin_dig = x.chars().last().unwrap(); + + if last_bin_dig == '0' { + print!("\t{} <--> {:?} ", x, current_list); + return true; + } + } + return false; +} + +fn proc(ints : Vec<i32>) { + println!("Input: ints = {:?}", ints); + print!("Output: "); + if truth(ints) { + println!("true"); + } else { + println!("false"); + } +} + diff --git a/challenge-269/zapwai/rust/ch-2.rs b/challenge-269/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..1f25b3f4a1 --- /dev/null +++ b/challenge-269/zapwai/rust/ch-2.rs @@ -0,0 +1,26 @@ +fn main() { + let mut ints = vec![2, 1, 3, 4, 5]; + let mut ints2 = vec![3,2,4]; + let mut ints3 = vec![5, 4, 3, 8]; + proc(ints); + proc(ints2); + proc(ints3); +} + +fn proc(mut ints : Vec<i32>) { + println!("Input: ints = {:?}", ints); + let mut arr1 = vec![ints[0]]; + let mut arr2 = vec![ints[1]]; + ints.reverse(); + ints.pop(); + ints.pop(); + while ints.len() > 0 { + let x = ints.pop().unwrap(); + if arr1[arr1.len()-1] > arr2[arr2.len()-1] { + arr1.push(x); + } else { + arr2.push(x); + } + } + println!("Output: arr1 = {:?}, arr2 = {:?}", arr1, arr2); +} |
