diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-25 22:33:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-25 22:33:01 +0100 |
| commit | 6b2e002330b881bdf16f469c1546e3d61dd76cba (patch) | |
| tree | df1ae637b1ca94319a525f023d56d39230abe12a | |
| parent | 9a8f0b8043c806ea406fdaddc718ccd263fbc093 (diff) | |
| parent | 9176b5be1ef39da532650c77b4df098f2a2a448e (diff) | |
| download | perlweeklychallenge-club-6b2e002330b881bdf16f469c1546e3d61dd76cba.tar.gz perlweeklychallenge-club-6b2e002330b881bdf16f469c1546e3d61dd76cba.tar.bz2 perlweeklychallenge-club-6b2e002330b881bdf16f469c1546e3d61dd76cba.zip | |
Merge pull request #10326 from zapwai/branch-for-275
Week 275
| -rw-r--r-- | challenge-275/zapwai/c/ch-1.c | 37 | ||||
| -rw-r--r-- | challenge-275/zapwai/c/ch-2.c | 41 | ||||
| -rw-r--r-- | challenge-275/zapwai/javascript/ch-1.js | 26 | ||||
| -rw-r--r-- | challenge-275/zapwai/javascript/ch-2.js | 33 | ||||
| -rw-r--r-- | challenge-275/zapwai/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-275/zapwai/perl/ch-2.pl | 34 | ||||
| -rw-r--r-- | challenge-275/zapwai/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-275/zapwai/python/ch-2.py | 31 | ||||
| -rw-r--r-- | challenge-275/zapwai/rust/ch-1.rs | 31 | ||||
| -rw-r--r-- | challenge-275/zapwai/rust/ch-2.rs | 38 |
10 files changed, 316 insertions, 0 deletions
diff --git a/challenge-275/zapwai/c/ch-1.c b/challenge-275/zapwai/c/ch-1.c new file mode 100644 index 0000000000..e3368fe9dd --- /dev/null +++ b/challenge-275/zapwai/c/ch-1.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <string.h> + +void proc(char sentence[], char keys[], int keyslen) { + printf("Input: Sentence = %s, Keys = ", sentence); + for (int i = 0; i < keyslen; i++) + printf("%c ", keys[i]); + printf("\n"); + int num = 0; + char* token = strtok(sentence, " "); + while (token != NULL) { + int tally = 0; + for (int k = 0; k < keyslen; k++) { + if (strchr(token, keys[k]) != NULL) { + break; + } else { + tally++; + } + } + if (tally == keyslen) + num++; + token = strtok(NULL, " "); + } + printf("Output: %d\n", num); +} + +int main(){ + char sentence[] = "Perl Weekly Challenge"; + char keys[] = {'l', 'a'}; + proc(sentence, keys, sizeof(keys) / sizeof(char)); + char sentence2[] = "Perl and Raku"; + char keys2[] = {'a'}; + proc(sentence2, keys2, sizeof(keys2) / sizeof(char)); + char sentence3[] = "Well done Team PWC"; + char keys3[] = {'l', 'o'}; + proc(sentence3, keys3, sizeof(keys3) / sizeof(char)); +} diff --git a/challenge-275/zapwai/c/ch-2.c b/challenge-275/zapwai/c/ch-2.c new file mode 100644 index 0000000000..7987cad7ce --- /dev/null +++ b/challenge-275/zapwai/c/ch-2.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <string.h> +#define MAX 50 + +char shifty(char c, int d) { + char alph[] = "abcdefghijklmnopqrstuvwxyz"; + int ind = (int) (strchr(alph, c) - alph); + return alph[d + ind]; +} + +void proc(char stringy[]) { + printf("Input: str = %s\n", stringy); + char lets[MAX]; + int letslen = 0; + char puts[MAX]; + int putslen = 0; + for (int i = 0; i < strlen(stringy); i++) { + if (i % 2 == 0) { + char let = stringy[i]; + lets[letslen++] = let; + } else { + char put = shifty(lets[letslen - 1], stringy[i] - '0'); + puts[putslen++] = put; + } + } + printf("Output: "); + for (int j = 0; j < strlen(stringy)/2; j++) + printf("%c%c", lets[j], puts[j]); + if ((strlen(stringy) % 2 == 1)) + printf("%c", lets[letslen - 1]); + printf("\n"); +} + +int main() { + char stringy[] = "a1c1e1"; + proc(stringy); + char stringy2[] = "a1b2c3d4"; + proc(stringy2); + char stringy3[] = "b2b"; + proc(stringy3); +} diff --git a/challenge-275/zapwai/javascript/ch-1.js b/challenge-275/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..77e8f64372 --- /dev/null +++ b/challenge-275/zapwai/javascript/ch-1.js @@ -0,0 +1,26 @@ +let sentence = "Perl Weekly Challenge"; +let keys = ['l', 'a']; +proc(sentence, keys); +sentence = "Perl and Raku"; keys = ['a']; +proc(sentence, keys); +sentence = "Well done Team PWC"; keys = ['l', 'o']; +proc(sentence, keys); +function proc(sentence, keys) { + console.log("Input: Sentence =", sentence, "keys =", keys); + let num = 0; + for (let word of sentence.split(' ')) { + let tally = 0; + for (let key of keys) { + if (word.indexOf(key) != -1) { + break; + } else { + tally++; + } + } + if (tally == keys.length) { + num++; + } + } + console.log("Output:", num); +} + diff --git a/challenge-275/zapwai/javascript/ch-2.js b/challenge-275/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..d57432c90b --- /dev/null +++ b/challenge-275/zapwai/javascript/ch-2.js @@ -0,0 +1,33 @@ +let stringy = 'a1c1e1'; +proc(stringy); +stringy = 'a1b2c3d4'; +proc(stringy); +stringy = 'b2b'; +proc(stringy); + +function proc(stringy) { + console.log("Input: str =", stringy); + let lets = []; + let puts = []; + for (let i = 0; i < stringy.length; i++) { + if (i % 2 == 0) { + let mylet = stringy.charAt(i); + lets.push(mylet); + } else { + let put = shifty(lets[lets.length - 1], stringy.charAt(i)); + puts.push(put); + } + } + output = "Output: "; + for (let j = 0; j <= (stringy.length)/2 - 1; j++) + output += lets[j] + puts[j]; + if (stringy.length % 2 == 1) + output += lets[lets.length - 1]; + console.log(output); +} + +function shifty(c, d) { + let alph = 'abcdefghijklmnopqrstuvwxyz'; + let ind = alph.indexOf(c); + return alph.charAt(+d + +ind); +} diff --git a/challenge-275/zapwai/perl/ch-1.pl b/challenge-275/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..b2c715877e --- /dev/null +++ b/challenge-275/zapwai/perl/ch-1.pl @@ -0,0 +1,20 @@ +use v5.38; +my $sentence = "Perl Weekly Challenge"; +my @keys = ('l', 'a'); +proc($sentence, @keys); +$sentence = "Perl and Raku"; @keys = ('a'); +proc($sentence, @keys); +$sentence = "Well done Team PWC"; @keys = ('l', 'o'); +proc($sentence, @keys); +sub proc($sentence, @keys) { + say "Input: Sentence = $sentence, keys = @keys"; + my $num = 0; + word: foreach my $word (split " ", $sentence) { + foreach my $key (@keys) { + next word if ($word =~ /$key/); + } + $num++; + } + say "Output: $num"; +} + diff --git a/challenge-275/zapwai/perl/ch-2.pl b/challenge-275/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..177442807f --- /dev/null +++ b/challenge-275/zapwai/perl/ch-2.pl @@ -0,0 +1,34 @@ +use v5.38; +my $alph = 'abcdefghijklmnopqrstuvwxyz'; +my $stringy = 'a1c1e1'; +proc($stringy); +$stringy = 'a1b2c3d4'; +proc($stringy); +$stringy = 'b2b'; +proc($stringy); + +sub proc($stringy) { + say "Input: str = $stringy"; + my @lets; + my @puts; + for my $i (0 .. length($stringy) - 1) { + if ($i % 2 == 0) { + my $let = substr $stringy, $i, 1; + push @lets, $let; + } else { + my $put = shifty($lets[$#lets], substr $stringy, $i, 1); + push @puts, $put; + } + } + print "Output: "; + print $lets[$_], $puts[$_] for (0 .. length($stringy)/2 - 1); + if ((length $stringy) % 2 == 1) { + print $lets[$#lets]; + } + print "\n"; +} + +sub shifty($c, $d) { + my $ind = index $alph, $c; + return substr $alph, $d + $ind, 1; +} diff --git a/challenge-275/zapwai/python/ch-1.py b/challenge-275/zapwai/python/ch-1.py new file mode 100644 index 0000000000..ed07e45940 --- /dev/null +++ b/challenge-275/zapwai/python/ch-1.py @@ -0,0 +1,25 @@ +def proc(sentence, keys) : + print("Input: Sentence =", sentence, "keys = ", keys) + num = 0 + for word in sentence.split(" "): + tally = 0 + for key in keys: + if key in word: + break + else: + tally += 1 + + if tally == len(keys): + num += 1 + + print("Output:", num) + +sentence = "Perl Weekly Challenge" +keys = ['l', 'a'] +proc(sentence, keys) +sentence = "Perl and Raku" +keys = ['a'] +proc(sentence, keys) +sentence = "Well done Team PWC" +keys = ['l', 'o'] +proc(sentence, keys) diff --git a/challenge-275/zapwai/python/ch-2.py b/challenge-275/zapwai/python/ch-2.py new file mode 100644 index 0000000000..c593ac08fb --- /dev/null +++ b/challenge-275/zapwai/python/ch-2.py @@ -0,0 +1,31 @@ +def shifty(c, d): + alph = 'abcdefghijklmnopqrstuvwxyz' + ind = alph.index(c) + A = list(alph) + return A[d + ind] + +def proc(stringy): + print("Input: str =", stringy); + lets = [] + puts = [] + for i in range(len(stringy)): + if i % 2 == 0: + let = stringy[i:i+1] + lets.append(let) + else: + put = shifty(lets[-1], ord(stringy[i:i+1]) - 48) + puts.append(put) + print("Output: ", end='') + for j in range((len(stringy)//2) ): + print(lets[j], puts[j], end='', sep='') + if (len(stringy)) % 2 == 1: + print(lets[-1], end='') + print("\n") + +stringy = 'a1c1e1' +proc(stringy) +stringy = 'a1b2c3d4' +proc(stringy) +stringy = 'b2b' +proc(stringy) + diff --git a/challenge-275/zapwai/rust/ch-1.rs b/challenge-275/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..d89498b264 --- /dev/null +++ b/challenge-275/zapwai/rust/ch-1.rs @@ -0,0 +1,31 @@ +fn main() { + let sentence = "Perl Weekly Challenge"; + let keys = vec!['l', 'a']; + proc(sentence, keys); + let sentence2 = "Perl and Raku"; + let keys2 = vec!['a']; + proc(sentence2, keys2); + let sentence3 = "Well done Team PWC"; + let keys3 = vec!['l', 'o']; + proc(sentence3, keys3); +} + +fn proc(sentence : &str, keys : Vec<char>) { + println!("Input: Sentence = {sentence}, keys = {:?}", keys); + let mut num = 0; + for word in sentence.split(" ") { + let mut tally = 0; + for key in &keys { + if word.contains(*key) { + break; + } else { + tally += 1; + } + } + if tally == keys.len() { + num += 1; + } + } + println!("Output: {num}"); +} + diff --git a/challenge-275/zapwai/rust/ch-2.rs b/challenge-275/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..0a6a4884ee --- /dev/null +++ b/challenge-275/zapwai/rust/ch-2.rs @@ -0,0 +1,38 @@ +fn main() { + let stringy = "a1c1e1"; + proc(stringy); + let stringy2 = "a1b2c3d4"; + proc(stringy2); + let stringy3 = "b2b"; + proc(stringy3); +} + +fn proc(stringy : &str) { + println!("Input: str = {stringy}"); + let mut lets : Vec<char> = Vec::new(); + let mut puts : Vec<char> = Vec::new(); + for i in 0 .. stringy.len() { + if i % 2 == 0 { + let mylet = stringy.chars().nth(i).unwrap(); + lets.push(mylet); + } else { + let put = shifty(lets.last().unwrap(), stringy.chars().nth(i).unwrap().to_digit(10).unwrap() as usize ); + puts.push(put); + } + } + print!("Output: "); + for j in 0 .. (stringy.len()/2) { + print!("{}{}", lets[j], puts[j]); + } + if (stringy.len()) % 2 == 1 { + print!("{}",lets.last().unwrap()); + } + println!(); + +} + +fn shifty(chr : &char, d : usize) -> char { + let alph = "abcdefghijklmnopqrstuvwxyz"; + let ind = alph.chars().position(|c| c == *chr).unwrap(); + return alph.chars().nth(d + ind).unwrap(); +} |
