diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-09 10:52:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 10:52:51 +0100 |
| commit | b208be7e37d87e9f6e8edfeb3e7a220da920ff81 (patch) | |
| tree | 5af9223aed73706fa3d7888c8e8734ba4326f41f | |
| parent | 895d77950307a2c67c19d895e2fcf58ee3d1bffc (diff) | |
| parent | fee0039c3385527d72e681ecee3bc59a6ce3439f (diff) | |
| download | perlweeklychallenge-club-b208be7e37d87e9f6e8edfeb3e7a220da920ff81.tar.gz perlweeklychallenge-club-b208be7e37d87e9f6e8edfeb3e7a220da920ff81.tar.bz2 perlweeklychallenge-club-b208be7e37d87e9f6e8edfeb3e7a220da920ff81.zip | |
Merge pull request #9899 from zapwai/branch-for-264
Week 264
| -rw-r--r-- | challenge-264/zapwai/c/ch-1.c | 65 | ||||
| -rw-r--r-- | challenge-264/zapwai/c/ch-2.c | 58 | ||||
| -rw-r--r-- | challenge-264/zapwai/javascript/ch-1.js | 26 | ||||
| -rw-r--r-- | challenge-264/zapwai/javascript/ch-2.js | 26 | ||||
| -rw-r--r-- | challenge-264/zapwai/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-264/zapwai/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-264/zapwai/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-264/zapwai/python/ch-2.py | 26 | ||||
| -rw-r--r-- | challenge-264/zapwai/rust/ch-1.rs | 26 | ||||
| -rw-r--r-- | challenge-264/zapwai/rust/ch-2.rs | 33 |
10 files changed, 326 insertions, 0 deletions
diff --git a/challenge-264/zapwai/c/ch-1.c b/challenge-264/zapwai/c/ch-1.c new file mode 100644 index 0000000000..de874a98b6 --- /dev/null +++ b/challenge-264/zapwai/c/ch-1.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <string.h> +#include <stdbool.h> + +char to_lower(char u) { + return (u >= 65 && u <= 90) ? u+32 : u; +} + +void sort(char c[], int clen) { + bool flag = false; + do { + flag = false; + for (int i = 0; i < clen - 1; i++) { + if (c[i] > c[i+1]) { + char tmp = c[i]; + c[i] = c[i+1]; + c[i+1] = tmp; + flag = true; + } + } + } while (flag); +} + +bool is_upper(char l) { + return (to_lower(l) != l); +} + +char proc(char* str) { + char* letters = str; + char upper[26]; + char lower[26]; + int i1 = 0; + int i2 = 0; + for (int i = 0; i < strlen(str); i++) { + char l = str[i]; + if (is_upper(l)) { + upper[i1] = l; + i1++; + } + else { + lower[i2] = l; + i2++; + } + } + char common[26]; + int i3 = 0; + for (int i = 0; i < i1; i++) { + char u = upper[i]; + for (int j = 0; j < i2; j++) { + char l = lower[j]; + if (to_lower(u) == l) { + common[i3] = u; + i3++; + } + } + } + sort(common, i3); + return common[i3 - 1]; +} + +int main() { + char* str = "PeRlwEKLy"; + printf("Input: %s\n", str); + printf("Output: %c\n", proc(str)); +} diff --git a/challenge-264/zapwai/c/ch-2.c b/challenge-264/zapwai/c/ch-2.c new file mode 100644 index 0000000000..86096faa72 --- /dev/null +++ b/challenge-264/zapwai/c/ch-2.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#define NIL -11111 +void proc(int source[], int indices[], int slen) { + printf("Input: source = { "); + for (int i = 0; i < slen; i++) + printf("%d ", source[i]); + printf("}, indices = { "); + for (int i = 0; i < slen; i++) + printf("%d ", indices[i]); + printf("}\n"); + + int target[slen]; + for (int i = 0; i < slen; i++) + target[i] = NIL; + + int current_length = 0; + for (int i = 0; i < slen; i++) { + /* target[current_length] = ; */ + + int *pre = malloc(current_length * sizeof(int)); + for (int j = 0; j < current_length; j++) + pre[j] = target[j]; + + int *post = malloc((slen - indices[i]) * sizeof(int)); + for (int j = 0; j < slen - indices[i]; j++) + post[j] = target[j]; + + /* Work in progress... */ + + free(pre); + free(post); + current_length++; + + /* chunk = splice @target, indices[i]; */ + /* target[indices[i]] = source[i]; */ + /* push @target, @chunk; */ + } + printf("Output: { "); + for (int i = 0; i < slen; i++) + printf("%d ", target[i]); + printf("}\n"); + +} + +int main() { + int source[] = {0, 1, 2, 3, 4}; + int indices[] = {0, 1, 2, 2, 1}; + proc(source, indices, sizeof(source) / sizeof(int)); + + int source2[] = {1, 2, 3, 4, 0}; + int indices2[] = {0, 1, 2, 3, 0}; + proc(source2, indices2, sizeof(source2) / sizeof(int)); + + int source3[] = {1}; + int indices3[] = {0}; + proc(source3, indices3, sizeof(source3) / sizeof(int)); +} diff --git a/challenge-264/zapwai/javascript/ch-1.js b/challenge-264/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..65f7c084e2 --- /dev/null +++ b/challenge-264/zapwai/javascript/ch-1.js @@ -0,0 +1,26 @@ +let str = 'PeRlwEKLy'; +console.log("Input:", str); +console.log("Output:", proc(str)); + +function proc(str) { + letters = str.split(""); + let upper = []; + let lower = []; + for (let l of letters) { + if (l.toUpperCase() == l) { + upper.push(l); + } else { + lower.push(l); + } + } + common = []; + for (let u of upper) { + for (let l of lower) { + if (u.toLowerCase() == l) { + common.push(u); + } + } + } + common = common.sort(); + return common[common.length-1]; +} diff --git a/challenge-264/zapwai/javascript/ch-2.js b/challenge-264/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..d0cee7a842 --- /dev/null +++ b/challenge-264/zapwai/javascript/ch-2.js @@ -0,0 +1,26 @@ +let source = [0, 1, 2, 3, 4]; +let indices = [0, 1, 2, 2, 1]; +proc(source, indices); + +source = [1, 2, 3, 4, 0]; +indices = [0, 1, 2, 3, 0]; +proc(source, indices); + +source = [1]; +indices = [0]; +proc(source, indices); + +function proc(s, i) { + let source = s; + let indices = i; + console.log("Input:", source, indices); + let target = []; + for (let i = 0; i < source.length; i++) { + let chunk = target.splice(indices[i]); + target[indices[i]] = source[i]; + if (chunk.length > 0) + for (let j = 0; j < chunk.length; j++) + target.push(chunk[j]); + } + console.log("Output:", target); +} diff --git a/challenge-264/zapwai/perl/ch-1.pl b/challenge-264/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..5e94bf268b --- /dev/null +++ b/challenge-264/zapwai/perl/ch-1.pl @@ -0,0 +1,21 @@ +use v5.38; +my $str = 'PeRlwEKLy'; +say "Input: \$str = $str"; +say "Output: " . proc($str); + +sub proc ($str) { + my @letters = split "", $str; + my @upper, my @lower; + for my $l (@letters) { + push @upper, $l if ($l =~ /[A-Z]/); + push @lower, $l if ($l =~ /[a-z]/); + } + my @common; + foreach my $u (@upper) { + foreach my $l (@lower) { + push @common, $u if (lc $u eq $l); + } + } + @common = sort @common; + return $common[$#common]; +} diff --git a/challenge-264/zapwai/perl/ch-2.pl b/challenge-264/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..07fd70b203 --- /dev/null +++ b/challenge-264/zapwai/perl/ch-2.pl @@ -0,0 +1,25 @@ +use v5.38; +my @source = (0, 1, 2, 3, 4); +my @indices = (0, 1, 2, 2, 1); +proc(\@source, \@indices); + +@source = (1, 2, 3, 4, 0); +@indices = (0, 1, 2, 3, 0); +proc(\@source, \@indices); + +@source = (1); +@indices = (0); +proc(\@source, \@indices); + +sub proc($s, $i) { + my @source = @$s; + my @indices = @$i; + say "Input: \@source = @source, \@indices = @indices"; + my @target; + for my $i (0 .. $#source) { + my @chunk = splice @target, $indices[$i]; + $target[$indices[$i]] = $source[$i]; + push @target, @chunk; + } + say "Output: @target"; +} diff --git a/challenge-264/zapwai/python/ch-1.py b/challenge-264/zapwai/python/ch-1.py new file mode 100644 index 0000000000..809cb08ea2 --- /dev/null +++ b/challenge-264/zapwai/python/ch-1.py @@ -0,0 +1,20 @@ +def proc(str): + letters = list(str) + upper = [] + lower= [] + for l in letters: + if l.isupper(): + upper.append(l) + else: + lower.append(l) + common = [] + for u in upper: + for l in lower: + if u.lower() == l: + common.append(u) + common.sort() + return common[len(common)-1] + +str = 'PeRlwEKLy' +print("Input: str =", str) +print("Output:", proc(str)) diff --git a/challenge-264/zapwai/python/ch-2.py b/challenge-264/zapwai/python/ch-2.py new file mode 100644 index 0000000000..5ea59280a9 --- /dev/null +++ b/challenge-264/zapwai/python/ch-2.py @@ -0,0 +1,26 @@ +def proc(source, indices): + print("Input: Source =", source, "Indices =", indices) + target = [] + for i in range(len(source)): + chunk = target[indices[i]:] + target = target[0:indices[i]] + while indices[i] >= len(target): + target.append(None) + target[indices[i]] = source[i] + if len(chunk) > 0: + for entry in chunk: + target.append(entry) + print("Output:", target) + +source = [0, 1, 2, 3, 4] +indices = [0, 1, 2, 2, 1] +proc(source, indices) + +source = [1, 2, 3, 4, 0] +indices = [0, 1, 2, 3, 0] +proc(source, indices) + +source = [1] +indices = [0] +proc(source, indices) + diff --git a/challenge-264/zapwai/rust/ch-1.rs b/challenge-264/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..14e04a2322 --- /dev/null +++ b/challenge-264/zapwai/rust/ch-1.rs @@ -0,0 +1,26 @@ +fn main() { + let str = String::from("PeRlwEKLy"); + println!("Input: str: {}", str); + println!("Output: {}", proc(str)); +} + +fn proc(str :String) -> char { + let letters :Vec<char> = str.chars().collect(); + let mut upper :Vec<char> = Vec::new(); + let mut lower :Vec<char> = Vec::new(); + for l in letters { + if l.is_uppercase() {upper.push(l);} + else {lower.push(l);} + } + let mut common = Vec::new(); + for u in upper { + let g = u.to_lowercase().next().unwrap(); + for l in &lower { + if g == *l { + common.push(u); + } + } + } + common.sort(); + return *common.last().unwrap(); +} diff --git a/challenge-264/zapwai/rust/ch-2.rs b/challenge-264/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..b6519250f1 --- /dev/null +++ b/challenge-264/zapwai/rust/ch-2.rs @@ -0,0 +1,33 @@ +fn proc(source :Vec<i32>, indices :Vec<i32>) { + println!("Input: Source = {:?}, Indices = {:?}", source, indices); + let mut target : Vec<i32> = Vec::new(); + + for i in 0 .. source.len() { + let chunk = &target[indices[i] as usize .. target.len()]; + let pre = &target[0 .. indices[i] as usize]; + let mut land : Vec<i32> = Vec::new(); + for p in pre { + land.push(*p); + } + land.push(source[i]); + for c in chunk { + land.push(*c); + } + target = land; + } + println!("Output: {:?}", target); +} + +fn main() { + let mut source = vec![0, 1, 2, 3, 4]; + let mut indices = vec![0, 1, 2, 2, 1]; + proc(source, indices); + + source = vec![1, 2, 3, 4, 0]; + indices = vec![0, 1, 2, 3, 0]; + proc(source, indices); + + source = vec![1]; + indices = vec![0]; + proc(source, indices); +} |
