diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-13 18:37:54 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-13 18:37:54 +0000 |
| commit | 7e52ec89708d688cd1560115a962acb56be7622a (patch) | |
| tree | 4f84e4bc7872fdb93e5ccd9d30e7eb61bf7acc6c | |
| parent | d18742a481f56afe01099d0a3252810bfbfef65c (diff) | |
| download | perlweeklychallenge-club-7e52ec89708d688cd1560115a962acb56be7622a.tar.gz perlweeklychallenge-club-7e52ec89708d688cd1560115a962acb56be7622a.tar.bz2 perlweeklychallenge-club-7e52ec89708d688cd1560115a962acb56be7622a.zip | |
Add C, C++ and Forth solution
| -rw-r--r-- | challenge-199/paulo-custodio/basic/ch-1.bas | 65 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/basic/ch-2.bas | 72 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/c/ch-1.c | 80 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/c/ch-2.c | 79 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/cpp/ch-1.cpp | 71 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/cpp/ch-2.cpp | 71 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/forth/ch-1.fs | 72 | ||||
| -rw-r--r-- | challenge-199/paulo-custodio/forth/ch-2.fs | 86 |
8 files changed, 596 insertions, 0 deletions
diff --git a/challenge-199/paulo-custodio/basic/ch-1.bas b/challenge-199/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..156d8237d6 --- /dev/null +++ b/challenge-199/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,65 @@ +' Challenge 199 +' +' Task 1: Good Pairs +' Submitted by: Mohammad S Anwar +' +' You are given a list of integers, @list. +' +' Write a script to find the total count of Good Pairs. +' +' A pair (i, j) is called good if list[i] == list[j] and i < j. +' +' +' Example 1 +' +' Input: @list = (1,2,3,1,1,3) +' Output: 4 +' +' There are 4 good pairs found as below: +' (0,3) +' (0,4) +' (3,4) +' (2,5) +' +' Example 2 +' +' Input: @list = (1,2,3) +' Output: 0 +' +' Example 3 +' +' Input: @list = (1,1,1,1) +' Output: 6 +' +' Good pairs are below: +' (0,1) +' (0,2) +' (0,3) +' (1,2) +' (1,3) +' (2,3) + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +function count_good_pairs(nums() as integer) as integer + dim count as integer, i as integer, j as integer + count=0 + for i=0 to ubound(nums)-1 + for j=i+1 to ubound(nums) + if nums(i)=nums(j) then count=count+1 + next + next + count_good_pairs=count +end function + +dim nums() as integer +collect_args nums() +print count_good_pairs(nums()) diff --git a/challenge-199/paulo-custodio/basic/ch-2.bas b/challenge-199/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..7672dd7941 --- /dev/null +++ b/challenge-199/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,72 @@ +' Challenge 199 +' +' Task 2: Good Triplets +' Submitted by: Mohammad S Anwar +' +' You are given an array of integers, @array and three integers $x,$y,$z. +' +' Write a script to find out total Good Triplets in the given array. +' +' A triplet array[i], array[j], array[k] is good if it satisfies the following +' conditions: +' +' a) 0 <= i < j < k <= n (size of given array) +' b) abs(array[i] - array[j]) <= x +' c) abs(array[j] - array[k]) <= y +' d) abs(array[i] - array[k]) <= z +' +' Example 1 +' +' Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +' Output: 4 +' +' Good Triplets are as below: +' (3,0,1) where (i=0, j=1, k=2) +' (3,0,1) where (i=0, j=1, k=3) +' (3,1,1) where (i=0, j=2, k=3) +' (0,1,1) where (i=1, j=2, k=3) +' +' Example 2 +' +' Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +' Output: 0 + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +function pop(nums() as integer) as integer + pop=nums(ubound(nums)) + redim preserve nums(ubound(nums)-1) +end function + +function count_good_triplets(nums() as integer, _ + x as integer, y as integer, z as integer) as integer + dim count as integer, i as integer, j as integer, k as integer + count=0 + for i=0 to ubound(nums)-2 + for j=i+1 to ubound(nums)-1 + for k=j+1 to ubound(nums) + if abs(nums(i) - nums(j)) <= x and _ + abs(nums(j) - nums(k)) <= y and _ + abs(nums(i) - nums(k)) <= z then + count=count+1 + end if + next + next + next + count_good_triplets=count +end function + +dim nums() as integer, x as integer, y as integer, z as integer +collect_args nums() +z=pop(nums()) +y=pop(nums()) +x=pop(nums()) +print count_good_triplets(nums(), x, y, z) diff --git a/challenge-199/paulo-custodio/c/ch-1.c b/challenge-199/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..ee01a083ad --- /dev/null +++ b/challenge-199/paulo-custodio/c/ch-1.c @@ -0,0 +1,80 @@ +/* +Challenge 199 + +Task 1: Good Pairs +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to find the total count of Good Pairs. + + A pair (i, j) is called good if list[i] == list[j] and i < j. + + +Example 1 + +Input: @list = (1,2,3,1,1,3) +Output: 4 + +There are 4 good pairs found as below: +(0,3) +(0,4) +(3,4) +(2,5) + +Example 2 + +Input: @list = (1,2,3) +Output: 0 + +Example 3 + +Input: @list = (1,1,1,1) +Output: 6 + +Good pairs are below: +(0,1) +(0,2) +(0,3) +(1,2) +(1,3) +(2,3) +*/ + +#include <stdio.h> +#include <stdlib.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int count_good_pairs(int nums[], int nums_size) { + int count = 0; + for (int i = 0; i < nums_size-1; i++) { + for (int j = i+1; j < nums_size; j++) { + if (nums[i] == nums[j]) + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + fputs("usage: ch-1 nums...\n", stderr); + return EXIT_FAILURE; + } + + int* nums = check_mem(malloc(argc * sizeof(int))); + for (int i = 0; i < argc; i++) + nums[i] = atoi(argv[i]); + + printf("%d\n", count_good_pairs(nums, argc)); + + free(nums); +} diff --git a/challenge-199/paulo-custodio/c/ch-2.c b/challenge-199/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..caacde0fd0 --- /dev/null +++ b/challenge-199/paulo-custodio/c/ch-2.c @@ -0,0 +1,79 @@ +/* +Challenge 199 + +Task 2: Good Triplets +Submitted by: Mohammad S Anwar + +You are given an array of integers, @array and three integers $x,$y,$z. + +Write a script to find out total Good Triplets in the given array. + +A triplet array[i], array[j], array[k] is good if it satisfies the following +conditions: + +a) 0 <= i < j < k <= n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[i] - array[k]) <= z + +Example 1 + +Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +Output: 4 + +Good Triplets are as below: +(3,0,1) where (i=0, j=1, k=2) +(3,0,1) where (i=0, j=1, k=3) +(3,1,1) where (i=0, j=2, k=3) +(0,1,1) where (i=1, j=2, k=3) + +Example 2 + +Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +Output: 0 +*/ + +#include <stdio.h> +#include <stdlib.h> + +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + +int count_good_triplets(int nums[], int nums_size, int x, int y, int z) { + int count = 0; + for (int i = 0; i < nums_size-2; i++) { + for (int j = i+1; j < nums_size-1; j++) { + for (int k = j+1; k < nums_size; k++) { + if (abs(nums[i] - nums[j]) <= x && + abs(nums[j] - nums[k]) <= y && + abs(nums[i] - nums[k]) <= z) + count++; + } + } + } + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 6) { + fputs("usage: ch-1 nums... x y z\n", stderr); + return EXIT_FAILURE; + } + + int z = atoi(argv[--argc]); + int y = atoi(argv[--argc]); + int x = atoi(argv[--argc]); + int* nums = check_mem(malloc(argc * sizeof(int))); + for (int i = 0; i < argc; i++) + nums[i] = atoi(argv[i]); + + printf("%d\n", count_good_triplets(nums, argc, x, y, z)); + + free(nums); +} diff --git a/challenge-199/paulo-custodio/cpp/ch-1.cpp b/challenge-199/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..e9af6899bd --- /dev/null +++ b/challenge-199/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,71 @@ +/* +Challenge 199 + +Task 1: Good Pairs +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to find the total count of Good Pairs. + + A pair (i, j) is called good if list[i] == list[j] and i < j. + + +Example 1 + +Input: @list = (1,2,3,1,1,3) +Output: 4 + +There are 4 good pairs found as below: +(0,3) +(0,4) +(3,4) +(2,5) + +Example 2 + +Input: @list = (1,2,3) +Output: 0 + +Example 3 + +Input: @list = (1,1,1,1) +Output: 6 + +Good pairs are below: +(0,1) +(0,2) +(0,3) +(1,2) +(1,3) +(2,3) +*/ + +#include <iostream> +#include <vector> + +int count_good_pairs(std::vector<int> nums) { + int nums_size = static_cast<int>(nums.size()); + int count = 0; + for (int i = 0; i < nums_size-1; i++) { + for (int j = i+1; j < nums_size; j++) { + if (nums[i] == nums[j]) + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc == 0) { + std::cerr << "usage: ch-1 nums..." << std::endl; + return EXIT_FAILURE; + } + + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + + std::cout << count_good_pairs(nums) << std::endl; +} diff --git a/challenge-199/paulo-custodio/cpp/ch-2.cpp b/challenge-199/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..64dfc8cb57 --- /dev/null +++ b/challenge-199/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,71 @@ +/* +Challenge 199 + +Task 2: Good Triplets +Submitted by: Mohammad S Anwar + +You are given an array of integers, @array and three integers $x,$y,$z. + +Write a script to find out total Good Triplets in the given array. + +A triplet array[i], array[j], array[k] is good if it satisfies the following +conditions: + +a) 0 <= i < j < k <= n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[i] - array[k]) <= z + +Example 1 + +Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +Output: 4 + +Good Triplets are as below: +(3,0,1) where (i=0, j=1, k=2) +(3,0,1) where (i=0, j=1, k=3) +(3,1,1) where (i=0, j=2, k=3) +(0,1,1) where (i=1, j=2, k=3) + +Example 2 + +Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +Output: 0 +*/ + +#include <iostream> +#include <vector> + + +int count_good_triplets(std::vector<int> nums, int x, int y, int z) { + int nums_size = static_cast<int>(nums.size()); + int count = 0; + for (int i = 0; i < nums_size-2; i++) { + for (int j = i+1; j < nums_size-1; j++) { + for (int k = j+1; k < nums_size; k++) { + if (abs(nums[i] - nums[j]) <= x && + abs(nums[j] - nums[k]) <= y && + abs(nums[i] - nums[k]) <= z) + count++; + } + } + } + return count; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 6) { + std::cerr << "usage: ch-1 nums... x y z" << std::endl; + return EXIT_FAILURE; + } + + int z = atoi(argv[--argc]); + int y = atoi(argv[--argc]); + int x = atoi(argv[--argc]); + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + + std::cout << count_good_triplets(nums, x, y, z) << std::endl; +} diff --git a/challenge-199/paulo-custodio/forth/ch-1.fs b/challenge-199/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..7c38b60267 --- /dev/null +++ b/challenge-199/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,72 @@ +#! /usr/bin/env gforth + +\ Challenge 199 +\ +\ Task 1: Good Pairs +\ Submitted by: Mohammad S Anwar +\ +\ You are given a list of integers, @list. +\ +\ Write a script to find the total count of Good Pairs. +\ +\ A pair (i, j) is called good if list[i] == list[j] and i < j. +\ +\ +\ Example 1 +\ +\ Input: @list = (1,2,3,1,1,3) +\ Output: 4 +\ +\ There are 4 good pairs found as below: +\ (0,3) +\ (0,4) +\ (3,4) +\ (2,5) +\ +\ Example 2 +\ +\ Input: @list = (1,2,3) +\ Output: 0 +\ +\ Example 3 +\ +\ Input: @list = (1,1,1,1) +\ Output: 6 +\ +\ Good pairs are below: +\ (0,1) +\ (0,2) +\ (0,3) +\ (1,2) +\ (1,3) +\ (2,3) + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( i -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: count_good_pairs ( -- n ) + 0 ( count ) + nums_size 2 >= IF + nums_size 1- 0 DO + nums_size I 1+ DO + I nums[] @ J nums[] @ = IF 1+ THEN + LOOP + LOOP + THEN +; + +collect_args count_good_pairs . CR +BYE diff --git a/challenge-199/paulo-custodio/forth/ch-2.fs b/challenge-199/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..fc5168ecac --- /dev/null +++ b/challenge-199/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,86 @@ +#! /usr/bin/env gforth + +\ Challenge 199 +\ +\ Task 2: Good Triplets +\ Submitted by: Mohammad S Anwar +\ +\ You are given an array of integers, @array and three integers $x,$y,$z. +\ +\ Write a script to find out total Good Triplets in the given array. +\ +\ A triplet array[i], array[j], array[k] is good if it satisfies the following +\ conditions: +\ +\ a) 0 <= i < j < k <= n (size of given array) +\ b) abs(array[i] - array[j]) <= x +\ c) abs(array[j] - array[k]) <= y +\ d) abs(array[i] - array[k]) <= z +\ +\ Example 1 +\ +\ Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +\ Output: 4 +\ +\ Good Triplets are as below: +\ (3,0,1) where (i=0, j=1, k=2) +\ (3,0,1) where (i=0, j=1, k=3) +\ (3,1,1) where (i=0, j=2, k=3) +\ (0,1,1) where (i=1, j=2, k=3) +\ +\ Example 2 +\ +\ Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +\ Output: 0 + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +0 VALUE x 0 VALUE y 0 VALUE z + +: nums[] ( i -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: pop_nums ( -- n ) + nums_size 1 < THROW + nums_size 1- DUP nums[] @ ( size-1 n ) + SWAP TO nums_size +; + +: count_good_triplets ( -- ) + 0 0 0 { i j k } + 0 ( count ) + 0 TO i + BEGIN i nums_size 2 - < WHILE + i 1+ TO j + BEGIN j nums_size 1- < WHILE + j 1+ TO k + BEGIN k nums_size < WHILE + i nums[] @ j nums[] @ - ABS x <= + j nums[] @ k nums[] @ - ABS y <= AND + i nums[] @ k nums[] @ - ABS z <= AND + IF 1+ THEN + k 1+ TO k + REPEAT + j 1+ TO j + REPEAT + i 1+ TO i + REPEAT +; + +collect_args nums_size 6 < THROW +pop_nums TO z +pop_nums TO y +pop_nums TO x +count_good_triplets . CR +BYE |
