aboutsummaryrefslogtreecommitdiff
path: root/challenge-201
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-11 23:20:09 +0000
committerGitHub <noreply@github.com>2023-03-11 23:20:09 +0000
commit993b5cfec02444f8ab158507ea64f42bcd3d175b (patch)
tree3678bcde9af7c2fb9a2fcb9f8cd5deefd17f59c2 /challenge-201
parent51465a4186e14e5f9ae26e7f5b20dbb9f5ea8aae (diff)
parent0ad64a5e5a740655432a36b2af0c35760289cf89 (diff)
downloadperlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.gz
perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.bz2
perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.zip
Merge pull request #7709 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-201')
-rw-r--r--challenge-201/paulo-custodio/basic/ch-1.bas51
-rw-r--r--challenge-201/paulo-custodio/basic/ch-2.bas48
-rw-r--r--challenge-201/paulo-custodio/c/ch-1.c66
-rw-r--r--challenge-201/paulo-custodio/c/ch-2.c53
-rw-r--r--challenge-201/paulo-custodio/cpp/ch-1.cpp53
-rw-r--r--challenge-201/paulo-custodio/cpp/ch-2.cpp52
-rw-r--r--challenge-201/paulo-custodio/forth/ch-1.fs61
-rw-r--r--challenge-201/paulo-custodio/forth/ch-2.fs47
-rw-r--r--challenge-201/paulo-custodio/perl/ch-2.pl1
9 files changed, 431 insertions, 1 deletions
diff --git a/challenge-201/paulo-custodio/basic/ch-1.bas b/challenge-201/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..bf4291e1e5
--- /dev/null
+++ b/challenge-201/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,51 @@
+' Challenge 201
+'
+' Task 1: Missing Numbers
+' Submitted by: Mohammad S Anwar
+'
+' You are given an array of unique numbers.
+'
+' Write a script to find out all missing numbers in the range 0..$n where $n
+' is the array size.
+'
+' Example 1
+'
+' Input: @array = (0,1,3)
+' Output: 2
+'
+' The array size i.e. total element count is 3, so the range is 0..3.
+' The missing number is 2 in the given array.
+'
+' Example 2
+'
+' Input: @array = (0,1)
+' Output: 2
+'
+' The array size is 2, therefore the range is 0..2.
+' The missing number is 2.
+
+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
+
+dim nums() as integer, got() as boolean, i as integer
+
+collect_args nums()
+for i=0 to ubound(nums)
+ if nums(i)>ubound(got) then redim preserve got(nums(i))
+ got(nums(i))=true
+next
+for i=0 to ubound(nums)+1
+ if i>ubound(got) then
+ print i;
+ elseif not got(i) then
+ print i;
+ end if
+next
+print
diff --git a/challenge-201/paulo-custodio/basic/ch-2.bas b/challenge-201/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..9f6a8290a4
--- /dev/null
+++ b/challenge-201/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,48 @@
+' Challenge 201
+'
+' Task 2: Penny Piles
+' Submitted by: Robbie Hatley
+'
+' You are given an integer, $n > 0.
+'
+' Write a script to determine the number of ways of putting $n pennies in a row
+' of piles of ascending heights from left to right.
+' Example
+'
+' Input: $n = 5
+' Output: 7
+'
+' Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:
+'
+' 1 1 1 1 1
+' 1 1 1 2
+' 1 2 2
+' 1 1 3
+' 2 3
+' 1 4
+' 5
+
+function make_piles1(prev as integer, n as integer) as integer
+ dim count as integer, i as integer, max as integer
+
+ count=0
+ if n=0 then
+ count=count+1
+ elseif n>0 then
+ if prev<0 then
+ max=n
+ else
+ max=prev
+ endif
+ for i=1 to max
+ count=count+make_piles1(i, n-i)
+ next
+ end if
+ make_piles1=count
+end function
+
+function make_piles(n as integer) as integer
+ make_piles=make_piles1(-1,n)
+end function
+
+print make_piles(val(command(1)))
diff --git a/challenge-201/paulo-custodio/c/ch-1.c b/challenge-201/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..2f2d1b1c64
--- /dev/null
+++ b/challenge-201/paulo-custodio/c/ch-1.c
@@ -0,0 +1,66 @@
+/*
+Challenge 201
+
+Task 1: Missing Numbers
+Submitted by: Mohammad S Anwar
+
+You are given an array of unique numbers.
+
+Write a script to find out all missing numbers in the range 0..$n where $n
+is the array size.
+
+Example 1
+
+Input: @array = (0,1,3)
+Output: 2
+
+The array size i.e. total element count is 3, so the range is 0..3.
+The missing number is 2 in the given array.
+
+Example 2
+
+Input: @array = (0,1)
+Output: 2
+
+The array size is 2, therefore the range is 0..2.
+The missing number is 2.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+int compare(const void*a, const void* b) {
+ return *(int*)a - *(int*)b;
+}
+
+void print_missing(int nums[], int nums_size) {
+ qsort(nums, nums_size, sizeof(int), compare);
+ for (int i = 0; i <= nums_size; i++) { // check 0..N
+ if (bsearch(&i, nums, nums_size, sizeof(int), compare) == NULL)
+ printf("%d ", i);
+ }
+ printf("\n");
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ fputs("usage: ch-1 nums...", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ int* nums = check_mem(malloc(argc*sizeof(int)));
+ for (int i = 0; i < argc; i++)
+ nums[i] = atoi(argv[i]);
+ print_missing(nums, argc);
+
+ free(nums);
+}
diff --git a/challenge-201/paulo-custodio/c/ch-2.c b/challenge-201/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..b3587f47f5
--- /dev/null
+++ b/challenge-201/paulo-custodio/c/ch-2.c
@@ -0,0 +1,53 @@
+/*
+Challenge 201
+
+Task 2: Penny Piles
+Submitted by: Robbie Hatley
+
+You are given an integer, $n > 0.
+
+Write a script to determine the number of ways of putting $n pennies in a row
+of piles of ascending heights from left to right.
+Example
+
+Input: $n = 5
+Output: 7
+
+Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:
+
+ 1 1 1 1 1
+ 1 1 1 2
+ 1 2 2
+ 1 1 3
+ 2 3
+ 1 4
+ 5
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int make_piles1(int prev, int n) {
+ int count = 0;
+ if (n == 0)
+ count++;
+ else if (n > 0) {
+ int max = (prev < 0) ? n : prev;
+ for (int i = 1; i <= max; i++)
+ count += make_piles1(i, n-i);
+ }
+ return count;
+}
+
+int make_piles(int n) {
+ return make_piles1(-1, n);
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ fputs("usage: ch-2 N", stderr);
+ exit(EXIT_FAILURE);
+ }
+
+ printf("%d\n", make_piles(atoi(argv[1])));
+}
diff --git a/challenge-201/paulo-custodio/cpp/ch-1.cpp b/challenge-201/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..88557d44f7
--- /dev/null
+++ b/challenge-201/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,53 @@
+/*
+Challenge 201
+
+Task 1: Missing Numbers
+Submitted by: Mohammad S Anwar
+
+You are given an array of unique numbers.
+
+Write a script to find out all missing numbers in the range 0..$n where $n
+is the array size.
+
+Example 1
+
+Input: @array = (0,1,3)
+Output: 2
+
+The array size i.e. total element count is 3, so the range is 0..3.
+The missing number is 2 in the given array.
+
+Example 2
+
+Input: @array = (0,1)
+Output: 2
+
+The array size is 2, therefore the range is 0..2.
+The missing number is 2.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+void print_missing(std::vector<int>& nums) {
+ std::sort(nums.begin(), nums.end());
+ for (size_t i = 0; i <= nums.size(); i++) { // check 0..N
+ if (!std::binary_search(nums.begin(), nums.end(), i))
+ std::cout << i << " ";
+ }
+ std::cout << std::endl;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ std::cerr << "usage: ch-1 nums..." << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ std::vector<int> nums;
+ for (int i = 0; i < argc; i++)
+ nums.push_back(atoi(argv[i]));
+ print_missing(nums);
+}
diff --git a/challenge-201/paulo-custodio/cpp/ch-2.cpp b/challenge-201/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..9eb7e7a8e7
--- /dev/null
+++ b/challenge-201/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,52 @@
+/*
+Challenge 201
+
+Task 2: Penny Piles
+Submitted by: Robbie Hatley
+
+You are given an integer, $n > 0.
+
+Write a script to determine the number of ways of putting $n pennies in a row
+of piles of ascending heights from left to right.
+Example
+
+Input: $n = 5
+Output: 7
+
+Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:
+
+ 1 1 1 1 1
+ 1 1 1 2
+ 1 2 2
+ 1 1 3
+ 2 3
+ 1 4
+ 5
+*/
+
+#include <iostream>
+
+int make_piles1(int prev, int n) {
+ int count = 0;
+ if (n == 0)
+ count++;
+ else if (n > 0) {
+ int max = (prev < 0) ? n : prev;
+ for (int i = 1; i <= max; i++)
+ count += make_piles1(i, n-i);
+ }
+ return count;
+}
+
+int make_piles(int n) {
+ return make_piles1(-1, n);
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) {
+ std::cerr << "usage: ch-2 N" << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ std::cout << make_piles(atoi(argv[1])) << std::endl;
+}
diff --git a/challenge-201/paulo-custodio/forth/ch-1.fs b/challenge-201/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..343135c81e
--- /dev/null
+++ b/challenge-201/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,61 @@
+#! /usr/bin/env gforth
+
+\ Challenge 201
+\
+\ Task 1: Missing Numbers
+\ Submitted by: Mohammad S Anwar
+\
+\ You are given an array of unique numbers.
+\
+\ Write a script to find out all missing numbers in the range 0..$n where $n
+\ is the array size.
+\
+\ Example 1
+\
+\ Input: @array = (0,1,3)
+\ Output: 2
+\
+\ The array size i.e. total element count is 3, so the range is 0..3.
+\ The missing number is 2 in the given array.
+\
+\ Example 2
+\
+\ Input: @array = (0,1)
+\ Output: 2
+\
+\ The array size is 2, therefore the range is 0..2.
+\ The missing number is 2.
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: nums[] ( i -- addr )
+ CELLS nums +
+;
+
+: find_nums { n -- f }
+ FALSE ( f )
+ nums_size 0 DO
+ I nums[] @ n = IF
+ DROP TRUE LEAVE
+ THEN
+ LOOP
+;
+
+: collect_args ( -- )
+ BEGIN NEXT-ARG DUP WHILE
+ 0 0 2SWAP >NUMBER 2DROP DROP
+ nums_size nums[] !
+ nums_size 1+ TO nums_size
+ REPEAT
+ 2DROP
+;
+
+: .missing ( -- )
+ nums_size 1+ 0 DO
+ I find_nums 0= IF I . THEN
+ LOOP
+ CR
+;
+
+collect_args .missing BYE
diff --git a/challenge-201/paulo-custodio/forth/ch-2.fs b/challenge-201/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..cabb49b33e
--- /dev/null
+++ b/challenge-201/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,47 @@
+#! /usr/bin/env gforth
+
+\ Challenge 201
+\
+\ Task 2: Penny Piles
+\ Submitted by: Robbie Hatley
+\
+\ You are given an integer, $n > 0.
+\
+\ Write a script to determine the number of ways of putting $n pennies in a row
+\ of piles of ascending heights from left to right.
+\ Example
+\
+\ Input: $n = 5
+\ Output: 7
+\
+\ Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles:
+\
+\ 1 1 1 1 1
+\ 1 1 1 2
+\ 1 2 2
+\ 1 1 3
+\ 2 3
+\ 1 4
+\ 5
+
+: make_piles1 { prev n -- count }
+ 0 ( count )
+ n 0= IF
+ 1+ \ count++
+ ELSE n 0 > IF
+ prev 0 < IF n ELSE prev THEN 1+
+ 1 DO
+ I n I - RECURSE
+ +
+ LOOP
+ THEN THEN
+;
+
+: make_piles ( n )
+ -1 SWAP make_piles1
+;
+
+
+NEXT-ARG DUP 0= THROW 0 0 2SWAP >NUMBER 2DROP DROP \ get n
+make_piles . CR
+BYE
diff --git a/challenge-201/paulo-custodio/perl/ch-2.pl b/challenge-201/paulo-custodio/perl/ch-2.pl
index e4e9fef4b8..f7ce4d9e62 100644
--- a/challenge-201/paulo-custodio/perl/ch-2.pl
+++ b/challenge-201/paulo-custodio/perl/ch-2.pl
@@ -25,7 +25,6 @@
# 5
use Modern::Perl;
-no warnings 'recursion';
sub make_piles1 {
my($count, $prev, $n) = @_;