diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-08 17:37:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-08 17:37:43 +0100 |
| commit | 64bc5662f4ec6323375d748a7066a6ac34aec9be (patch) | |
| tree | ce61f57d395559b80696c6c654e4f1ee6060d458 | |
| parent | 99c4270a274259398cfaab3722bc0c934ab5fdc6 (diff) | |
| parent | 0f2bbd6656772c2a3d5f4cca39763d05443c23b5 (diff) | |
| download | perlweeklychallenge-club-64bc5662f4ec6323375d748a7066a6ac34aec9be.tar.gz perlweeklychallenge-club-64bc5662f4ec6323375d748a7066a6ac34aec9be.tar.bz2 perlweeklychallenge-club-64bc5662f4ec6323375d748a7066a6ac34aec9be.zip | |
Merge pull request #10396 from zapwai/branch-for-277
Week 277
| -rw-r--r-- | challenge-277/zapwai/c/ch-1.c | 47 | ||||
| -rw-r--r-- | challenge-277/zapwai/c/ch-2.c | 30 | ||||
| -rw-r--r-- | challenge-277/zapwai/javascript/ch-1.js | 38 | ||||
| -rw-r--r-- | challenge-277/zapwai/javascript/ch-2.js | 17 | ||||
| -rw-r--r-- | challenge-277/zapwai/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-277/zapwai/perl/ch-2.pl | 17 | ||||
| -rw-r--r-- | challenge-277/zapwai/python/ch-1.py | 31 | ||||
| -rw-r--r-- | challenge-277/zapwai/python/ch-2.py | 13 | ||||
| -rw-r--r-- | challenge-277/zapwai/rust/ch-1.rs | 40 | ||||
| -rw-r--r-- | challenge-277/zapwai/rust/ch-2.rs | 20 |
10 files changed, 285 insertions, 0 deletions
diff --git a/challenge-277/zapwai/c/ch-1.c b/challenge-277/zapwai/c/ch-1.c new file mode 100644 index 0000000000..289de53f5d --- /dev/null +++ b/challenge-277/zapwai/c/ch-1.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <string.h> +#include <stdbool.h> + +bool is_multi(char* word, char* words[], int wordslen) { + int cnt = 0; + for (int i = 0; i < wordslen; i++) { + char* w = words[i]; + if (word == w) + cnt++; + } + return (cnt == 1) ? false : true; +} + +void proc(char* words1[], int words1len, char* words2[], int words2len) { + printf("Input:"); + for (int i = 0; i < words1len; i++) + printf("%s ", words1[i]); + printf("| "); + for (int i = 0; i < words2len; i++) + printf("%s ", words2[i]); + printf("\n"); + int cnt = 0; + + for (int i = 0; i < words1len; i++) { + char* word1 = words1[i]; + if (is_multi(word1, words1, words1len)) + continue; + for (int j = 0; j < words2len; j++) { + char* word2 = words2[j]; + if (is_multi(word2, words2, words2len)) + continue; + if (strcmp(word1, word2) == 0) + cnt++; + } + } + printf("Output: %d\n", cnt); +} + +int main() { + char* words1[] = {"Perl", "is", "my", "friend"}; + char* words2[] = {"Perl", "and", "Raku", "are", "friend"}; + proc(words1, sizeof(words1)/sizeof(char*), words2, sizeof(words2) / sizeof(char*)); + char* words3[] = {"Perl", "and", "Python", "are", "very", "similar"}; + char* words4[] = {"Python", "is", "top", "in", "guest", "languages"}; + proc(words3, sizeof(words3)/sizeof(char*), words4, sizeof(words4) / sizeof(char*)); +} diff --git a/challenge-277/zapwai/c/ch-2.c b/challenge-277/zapwai/c/ch-2.c new file mode 100644 index 0000000000..36a2a418ba --- /dev/null +++ b/challenge-277/zapwai/c/ch-2.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <math.h> +#include <stdlib.h> + +int min(int a, int b) { + return (a < b) ? a : b; +} + +void proc(int ints[], int intslen) { + printf("Input: { "); + for (int i = 0; i < intslen; i++) + printf("%d ", ints[i]); + printf("}\n"); + int cnt = 0; + for (int i = 0; i < intslen - 1; i++) { + for (int j = i + 1; j < intslen; j++) { + if (ints[i] != ints[j] && (abs(ints[i] - ints[j]) < min(ints[i],ints[j]))) + cnt++; + } + } + printf("Output: %d\n", cnt); +} + +int main() { + int ints[] = {1, 2, 3, 4, 5}; + proc(ints, sizeof(ints) / sizeof(int)); + int ints2[] = {5, 7, 1, 7}; + proc(ints2, sizeof(ints2) / sizeof(int)); +} + diff --git a/challenge-277/zapwai/javascript/ch-1.js b/challenge-277/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..8eefbacbe0 --- /dev/null +++ b/challenge-277/zapwai/javascript/ch-1.js @@ -0,0 +1,38 @@ +let words1 = ["Perl", "is", "let", "friend"]; +let words2 = ["Perl", "and", "Raku", "are", "friend"]; +proc(words1, words2); +words1 = ["Perl", "and", "Python", "are", "very", "similar"]; +words2 = ["Python", "is", "top", "in", "guest", "languages"]; +proc(words1, words2); + +function proc(words1, words2) { + console.log("Input:", words1, words2); + let cnt = 0; + for (let word1 of words1) { + if (is_multi(word1, words1)) { + continue; + } + for (let word2 of words2) { + if (is_multi(word2, words2)) { + continue; + } + if (word1 == word2) { + cnt++ ; + } + } + } + console.log("Output:", cnt); +} + +function is_multi(word, words) { + let cnt = 0; + for (let w of words) { + if (word == w) { + cnt++ ; + } + } + if (cnt == 1) { + return 0; + } + return 1; +} diff --git a/challenge-277/zapwai/javascript/ch-2.js b/challenge-277/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..6365e573e0 --- /dev/null +++ b/challenge-277/zapwai/javascript/ch-2.js @@ -0,0 +1,17 @@ +let ints = [1, 2, 3, 4, 5]; +proc(ints); +ints = [5, 7, 1, 7]; +proc(ints); + +function proc(ints) { + console.log("Input:", ints); + let cnt = 0; + for (let i = 0; i < ints.length - 1; i++) { + for (let j = i + 1; j < ints.length; j++) { + if (ints[i] != ints[j] && (Math.abs(ints[i] - ints[j]) < Math.min(ints[i],ints[j]))) { + cnt++; + } + } + } + console.log("Output:", cnt); +} diff --git a/challenge-277/zapwai/perl/ch-1.pl b/challenge-277/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..e1c466206b --- /dev/null +++ b/challenge-277/zapwai/perl/ch-1.pl @@ -0,0 +1,32 @@ +use v5.38; +my @words1 = ("Perl", "is", "my", "friend"); +my @words2 = ("Perl", "and", "Raku", "are", "friend"); +proc(\@words1, \@words2); +@words1 = ("Perl", "and", "Python", "are", "very", "similar"); +@words2 = ("Python", "is", "top", "in", "guest", "languages"); +proc(\@words1, \@words2); + +sub proc($words1, $words2) { + say "Input: @words1, @words2"; + my @words1 = @$words1; + my @words2 = @$words2; + my $cnt = 0; + for my $word1 (@words1) { + next if (is_multi($word1, @words1)); + for my $word2 (@words2) { + next if (is_multi($word2, @words2)); + $cnt++ if ($word1 eq $word2); + } + } + say "Output: $cnt"; +} + +#return true if $word occurs more than once in the list. +sub is_multi($word, @words) { + my $cnt = 0; + foreach my $w (@words) { + $cnt++ if ($word eq $w); + } + return 0 if ($cnt == 1); + return 1; +} diff --git a/challenge-277/zapwai/perl/ch-2.pl b/challenge-277/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..9e6ae856ef --- /dev/null +++ b/challenge-277/zapwai/perl/ch-2.pl @@ -0,0 +1,17 @@ +use v5.38; +use List::Util qw(min); +my @ints = (1, 2, 3, 4, 5); +proc(@ints); +@ints = (5, 7, 1, 7); +proc(@ints); + +sub proc(@ints) { + say "Input: (@ints)"; + my $cnt = 0; + for my $i (0 .. $#ints - 1) { + for my $j ($i + 1 .. $#ints) { + $cnt++ if ($ints[$i] != $ints[$j] && (abs($ints[$i] - $ints[$j]) < min($ints[$i], $ints[$j]))); + } + } + say "Output: $cnt"; +} diff --git a/challenge-277/zapwai/python/ch-1.py b/challenge-277/zapwai/python/ch-1.py new file mode 100644 index 0000000000..824c3f52a7 --- /dev/null +++ b/challenge-277/zapwai/python/ch-1.py @@ -0,0 +1,31 @@ +def proc(words1, words2): + print("Input:", words1, ",", words2) + words1 = words1 + words2 = words2 + cnt = 0 + for word1 in words1: + if is_multi(word1, words1): + continue + for word2 in words2: + if (is_multi(word2, words2)): + continue + if word1 == word2: + cnt += 1 + + print("Output:", cnt) + +def is_multi(word, words): + cnt = 0 + for w in words: + if word == w: + cnt += 1 + if cnt == 1: + return 0 + return 1 + +words1 = ["Perl", "is", "my", "friend"] +words2 = ["Perl", "and", "Raku", "are", "friend"] +proc(words1, words2) +words1 = ["Perl", "and", "Python", "are", "very", "similar"] +words2 = ["Python", "is", "top", "in", "guest", "languages"] +proc(words1, words2) diff --git a/challenge-277/zapwai/python/ch-2.py b/challenge-277/zapwai/python/ch-2.py new file mode 100644 index 0000000000..2b19f08d39 --- /dev/null +++ b/challenge-277/zapwai/python/ch-2.py @@ -0,0 +1,13 @@ +def proc(ints): + print("Input:", ints ) + cnt = 0 + for i in range(len(ints) - 1): + for j in range(i + 1, len(ints)): + if ints[i] != ints[j] and abs(ints[i] - ints[j]) < min(ints[i], ints[j]): + cnt += 1 + print("Output:", cnt) + +ints = [1, 2, 3, 4, 5] +proc(ints) +ints = [5, 7, 1, 7] +proc(ints) diff --git a/challenge-277/zapwai/rust/ch-1.rs b/challenge-277/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..9a63664fa4 --- /dev/null +++ b/challenge-277/zapwai/rust/ch-1.rs @@ -0,0 +1,40 @@ +fn main() { + let words1 = vec!["Perl", "is", "my", "friend"]; + let words2 = vec!["Perl", "and", "Raku", "are", "friend"]; + proc(words1, words2); + let words3 = vec!["Perl", "and", "Python", "are", "very", "similar"]; + let words4 = vec!["Python", "is", "top", "in", "guest", "languages"]; + proc(words3, words4); +} + +fn proc(words1 : Vec<&str>, words2 : Vec<&str>) { + println!("Input: {:?}, {:?}", words1, words2); + let mut cnt = 0; + for word1 in &words1 { + if is_multi(word1, &words1) { + continue; + } + for word2 in &words2 { + if is_multi(word2, &words2) { + continue; + } + if word1 == word2 { + cnt += 1; + } + } + } + println!("Output: {cnt}"); +} + +fn is_multi(word : &str, words : &Vec<&str>) -> bool { + let mut cnt = 0; + for w in words { + if word == *w { + cnt += 1; + } + } + if cnt == 1 { + return false; + } + return true; +} diff --git a/challenge-277/zapwai/rust/ch-2.rs b/challenge-277/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..e2727f6519 --- /dev/null +++ b/challenge-277/zapwai/rust/ch-2.rs @@ -0,0 +1,20 @@ +use std::cmp::min; +fn main() { + let ints = vec![1, 2, 3, 4, 5]; + proc(ints); + let ints2 = vec![5, 7, 1, 7]; + proc(ints2); +} + +fn proc(ints : Vec<i32>) { + println!("Input: {:?}", ints); + let mut cnt = 0; + for i in 0 .. ints.len() - 1 { + for j in i + 1 .. ints.len() { + if ints[i] != ints[j] && (ints[i] - ints[j]).abs() < min(ints[i],ints[j]) { + cnt += 1; + } + } + } + println!("Output: {cnt}"); +} |
