aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-26 00:28:56 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-26 00:28:56 +0000
commit203478bbfe21091f94c4559377f730f75e688dfd (patch)
treeac6700738a351c6dbdbe0f16503cba14cf2d91d2
parent6737a18810400c88848bb9364f9abf5297a5caef (diff)
downloadperlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.gz
perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.bz2
perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.zip
Add Perl, C, C++, BASIC and Forth solutions
-rw-r--r--challenge-196/paulo-custodio/Makefile2
-rw-r--r--challenge-196/paulo-custodio/basic/ch-1.bas53
-rw-r--r--challenge-196/paulo-custodio/basic/ch-2.bas60
-rw-r--r--challenge-196/paulo-custodio/c/ch-1.c69
-rw-r--r--challenge-196/paulo-custodio/c/ch-2.c67
-rw-r--r--challenge-196/paulo-custodio/cpp/ch-1.cpp60
-rw-r--r--challenge-196/paulo-custodio/cpp/ch-2.cpp59
-rw-r--r--challenge-196/paulo-custodio/forth/ch-1.fs64
-rw-r--r--challenge-196/paulo-custodio/forth/ch-2.fs62
-rw-r--r--challenge-196/paulo-custodio/perl/ch-1.pl47
-rw-r--r--challenge-196/paulo-custodio/perl/ch-2.pl51
-rw-r--r--challenge-196/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-196/paulo-custodio/t/test-2.yaml20
13 files changed, 634 insertions, 0 deletions
diff --git a/challenge-196/paulo-custodio/Makefile b/challenge-196/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-196/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-196/paulo-custodio/basic/ch-1.bas b/challenge-196/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..d96c2d638a
--- /dev/null
+++ b/challenge-196/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,53 @@
+' Challenge 197
+'
+' Task 1: Pattern 132
+' Submitted by: Mohammad S Anwar
+' You are given a list of integers, @list.
+'
+' Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+'
+'
+' Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+'
+'
+' Example 1
+' Input: @list = (3, 1, 4, 2)
+' Output: (1, 4, 2) respect the Pattern 132.
+' Example 2
+' Input: @list = (1, 2, 3, 4)
+' Output: () since no susbsequence can be found.
+' Example 3
+' Input: @list = (1, 3, 2, 4, 6, 5)
+' Output: (1, 3, 2) if more than one subsequence found then return the first.
+' Example 4
+' Input: @list = (1, 3, 4, 2)
+' Output: (1, 3, 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
+
+sub print_pattern132(nums() as integer)
+ dim i as integer, j as integer, k as integer
+ for i=0 to ubound(nums)-2
+ for j=i+1 to ubound(nums)-1
+ for k=j+1 to ubound(nums)
+ if nums(i)<nums(k) and nums(k)<nums(j) then
+ print "(";nums(i);nums(j);nums(k);")"
+ exit sub
+ end if
+ next
+ next
+ next
+ print "()"
+end sub
+
+dim nums() as integer
+collect_args nums()
+print_pattern132 nums()
diff --git a/challenge-196/paulo-custodio/basic/ch-2.bas b/challenge-196/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..b1fefc8c8a
--- /dev/null
+++ b/challenge-196/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,60 @@
+' Challenge 197
+'
+' Task 2: Range List
+' Submitted by: Mohammad S Anwar
+' You are given a sorted unique integer array, @array.
+'
+' Write a script to find all possible Number Range i.e [x, y] represent range
+' all integers from x and y (both inclusive).
+'
+'
+' Each subsequence of two or more contiguous integers
+'
+'
+' Example 1
+' Input: @array = (1,3,4,5,7)
+' Output: [3,5]
+' Example 2
+' Input: @array = (1,2,3,6,7,9)
+' Output: [1,3], [6,7]
+' Example 3
+' Input: @array = (0,1,2,4,5,6,8,9)
+' Output: [0,2], [4,6], [8,9]
+
+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
+
+sub print_ranges(nums() as integer)
+ dim i as integer, j as integer, sep as string
+ print "(";
+ i=0
+ do while i<=ubound(nums)
+ j=0
+ do while i+j<=ubound(nums)
+ if nums(i)+j<>nums(i+j) then
+ exit do
+ else
+ j=j+1
+ end if
+ loop
+ if j>1 then
+ print sep;"[";nums(i);",";nums(i+j-1);"]";
+ sep=", "
+ i=i+j
+ else
+ i=i+1
+ end if
+ loop
+ print ")"
+end sub
+
+dim nums() as integer
+collect_args nums()
+print_ranges nums()
diff --git a/challenge-196/paulo-custodio/c/ch-1.c b/challenge-196/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..6445371db7
--- /dev/null
+++ b/challenge-196/paulo-custodio/c/ch-1.c
@@ -0,0 +1,69 @@
+/*
+Challenge 197
+
+Task 1: Pattern 132
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+
+
+Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+
+
+Example 1
+Input: @list = (3, 1, 4, 2)
+Output: (1, 4, 2) respect the Pattern 132.
+Example 2
+Input: @list = (1, 2, 3, 4)
+Output: () since no susbsequence can be found.
+Example 3
+Input: @list = (1, 3, 2, 4, 6, 5)
+Output: (1, 3, 2) if more than one subsequence found then return the first.
+Example 4
+Input: @list = (1, 3, 4, 2)
+Output: (1, 3, 2)
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+void print_patter132(int* nums, int nums_size) {
+ printf("(");
+ 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 (nums[i]<nums[k] && nums[k]<nums[j]) {
+ printf("%d %d %d", nums[i], nums[j], nums[k]);
+ goto end;
+ }
+ }
+ }
+ }
+end:
+ printf(")\n");
+}
+
+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]);
+
+ print_patter132(nums, argc);
+
+ free(nums);
+}
diff --git a/challenge-196/paulo-custodio/c/ch-2.c b/challenge-196/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..0f82a96990
--- /dev/null
+++ b/challenge-196/paulo-custodio/c/ch-2.c
@@ -0,0 +1,67 @@
+/*
+Challenge 197
+
+Task 2: Range List
+Submitted by: Mohammad S Anwar
+You are given a sorted unique integer array, @array.
+
+Write a script to find all possible Number Range i.e [x, y] represent range
+all integers from x and y (both inclusive).
+
+
+Each subsequence of two or more contiguous integers
+
+
+Example 1
+Input: @array = (1,3,4,5,7)
+Output: [3,5]
+Example 2
+Input: @array = (1,2,3,6,7,9)
+Output: [1,3], [6,7]
+Example 3
+Input: @array = (0,1,2,4,5,6,8,9)
+Output: [0,2], [4,6], [8,9]
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+void print_ranges(int* nums, int nums_size) {
+ printf("(");
+ const char* sep="";
+ for (int i=0; i<nums_size; i++) {
+ int j=0;
+ while (i+j<nums_size && nums[i]+j==nums[i+j])
+ j++;
+ if (j>1) {
+ printf("%s[%d,%d]", sep, nums[i], nums[i+j-1]);
+ sep=", ";
+ i+=j-1;
+ }
+ }
+ printf(")\n");
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ fputs("usage: ch-2 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]);
+
+ print_ranges(nums, argc);
+
+ free(nums);
+}
diff --git a/challenge-196/paulo-custodio/cpp/ch-1.cpp b/challenge-196/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..8c62d7d84e
--- /dev/null
+++ b/challenge-196/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,60 @@
+/*
+Challenge 197
+
+Task 1: Pattern 132
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+
+
+Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+
+
+Example 1
+Input: @list = (3, 1, 4, 2)
+Output: (1, 4, 2) respect the Pattern 132.
+Example 2
+Input: @list = (1, 2, 3, 4)
+Output: () since no susbsequence can be found.
+Example 3
+Input: @list = (1, 3, 2, 4, 6, 5)
+Output: (1, 3, 2) if more than one subsequence found then return the first.
+Example 4
+Input: @list = (1, 3, 4, 2)
+Output: (1, 3, 2)
+*/
+
+#include <iostream>
+#include <vector>
+
+void print_patter132(const std::vector<int>& nums) {
+ int nums_size=static_cast<int>(nums.size());
+ std::cout<<"(";
+ 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 (nums[i]<nums[k] && nums[k]<nums[j]) {
+ std::cout<<nums[i]<<" "<<nums[j]<<" "<<nums[k];
+ goto end;
+ }
+ }
+ }
+ }
+end:
+ std::cout<<")"<<std::endl;
+}
+
+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]));
+
+ print_patter132(nums);
+}
diff --git a/challenge-196/paulo-custodio/cpp/ch-2.cpp b/challenge-196/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..04c5a3f0b1
--- /dev/null
+++ b/challenge-196/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,59 @@
+/*
+Challenge 197
+
+Task 2: Range List
+Submitted by: Mohammad S Anwar
+You are given a sorted unique integer array, @array.
+
+Write a script to find all possible Number Range i.e [x, y] represent range
+all integers from x and y (both inclusive).
+
+
+Each subsequence of two or more contiguous integers
+
+
+Example 1
+Input: @array = (1,3,4,5,7)
+Output: [3,5]
+Example 2
+Input: @array = (1,2,3,6,7,9)
+Output: [1,3], [6,7]
+Example 3
+Input: @array = (0,1,2,4,5,6,8,9)
+Output: [0,2], [4,6], [8,9]
+*/
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+void print_ranges(const std::vector<int>& nums) {
+ int nums_size=static_cast<int>(nums.size());
+ std::cout<<"(";
+ std::string sep="";
+ for (int i=0; i<nums_size; i++) {
+ int j=0;
+ while (i+j<nums_size && nums[i]+j==nums[i+j])
+ j++;
+ if (j>1) {
+ std::cout<<sep<<"["<<nums[i]<<","<<nums[i+j-1]<<"]";
+ sep=", ";
+ i+=j-1;
+ }
+ }
+ std::cout<<")"<<std::endl;
+}
+
+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]));
+
+ print_ranges(nums);
+}
diff --git a/challenge-196/paulo-custodio/forth/ch-1.fs b/challenge-196/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..2cb27cc3f4
--- /dev/null
+++ b/challenge-196/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,64 @@
+#! /usr/bin/env gforth
+
+\ Challenge 197
+\
+\ Task 1: Pattern 132
+\ Submitted by: Mohammad S Anwar
+\ You are given a list of integers, @list.
+\
+\ Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+\
+\
+\ Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+\
+\
+\ Example 1
+\ Input: @list = (3, 1, 4, 2)
+\ Output: (1, 4, 2) respect the Pattern 132.
+\ Example 2
+\ Input: @list = (1, 2, 3, 4)
+\ Output: () since no susbsequence can be found.
+\ Example 3
+\ Input: @list = (1, 3, 2, 4, 6, 5)
+\ Output: (1, 3, 2) if more than one subsequence found then return the first.
+\ Example 4
+\ Input: @list = (1, 3, 4, 2)
+\ Output: (1, 3, 2)
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: nums[] ( idx -- 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
+;
+
+: print_pattern_sub ( -- )
+ 0 { i } BEGIN i nums_size 2 - < WHILE
+ i 1+ { j } BEGIN j nums_size 1- < WHILE
+ j 1+ { k } BEGIN k nums_size < WHILE
+ i nums[] @ k nums[] @ <
+ k nums[] @ j nums[] @ < AND IF
+ i nums[] @ . j nums[] @ . k nums[] @ .
+ EXIT
+ THEN
+ k 1+ TO k REPEAT
+ j 1+ TO j REPEAT
+ i 1+ TO i REPEAT
+;
+
+: print_pattern132 ( -- )
+ '(' EMIT print_pattern_sub ')' EMIT CR
+;
+
+collect_args
+print_pattern132
+BYE
diff --git a/challenge-196/paulo-custodio/forth/ch-2.fs b/challenge-196/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..8af253fc45
--- /dev/null
+++ b/challenge-196/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,62 @@
+#! /usr/bin/env gforth
+
+\ Challenge 197
+\
+\ Task 2: Range List
+\ Submitted by: Mohammad S Anwar
+\ You are given a sorted unique integer array, @array.
+\
+\ Write a script to find all possible Number Range i.e [x, y] represent range
+\ all integers from x and y (both inclusive).
+\
+\
+\ Each subsequence of two or more contiguous integers
+\
+\
+\ Example 1
+\ Input: @array = (1,3,4,5,7)
+\ Output: [3,5]
+\ Example 2
+\ Input: @array = (1,2,3,6,7,9)
+\ Output: [1,3], [6,7]
+\ Example 3
+\ Input: @array = (0,1,2,4,5,6,8,9)
+\ Output: [0,2], [4,6], [8,9]
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: nums[] ( idx -- 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
+;
+
+: print_ranges_sub ( -- )
+ FALSE { sep }
+ 0 { i } BEGIN i nums_size < WHILE
+ 0 { j } BEGIN i j + nums_size <
+ i nums[] @ j + i j + nums[] @ = AND WHILE
+ j 1+ TO j REPEAT
+ j 1 > IF
+ sep IF ',' EMIT SPACE THEN TRUE TO sep
+ '[' EMIT i nums[] @ . ',' EMIT i j + 1- nums[] @ . ']' EMIT
+ i j + 1- TO i
+ THEN
+ i 1+ TO i REPEAT
+;
+
+: print_ranges ( -- )
+ '(' EMIT print_ranges_sub ')' EMIT CR
+;
+
+collect_args
+print_ranges
+BYE
diff --git a/challenge-196/paulo-custodio/perl/ch-1.pl b/challenge-196/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d28eed71b1
--- /dev/null
+++ b/challenge-196/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl
+
+# Challenge 197
+#
+# Task 1: Pattern 132
+# Submitted by: Mohammad S Anwar
+# You are given a list of integers, @list.
+#
+# Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+#
+#
+# Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+#
+#
+# Example 1
+# Input: @list = (3, 1, 4, 2)
+# Output: (1, 4, 2) respect the Pattern 132.
+# Example 2
+# Input: @list = (1, 2, 3, 4)
+# Output: () since no susbsequence can be found.
+# Example 3
+# Input: @list = (1, 3, 2, 4, 6, 5)
+# Output: (1, 3, 2) if more than one subsequence found then return the first.
+# Example 4
+# Input: @list = (1, 3, 4, 2)
+# Output: (1, 3, 2)
+
+use Modern::Perl;
+
+sub pattern132 {
+ my(@n)=@_;
+ for my $i (0..$#n-2) {
+ for my $j ($i+1..$#n-1) {
+ for my $k ($j+1..$#n) {
+ if ($n[$i]<$n[$k] && $n[$k]<$n[$j]) {
+ return ($n[$i],$n[$j],$n[$k]);
+ }
+ }
+ }
+ }
+ return ();
+}
+
+@ARGV or die "usage: ch-1.pl nums...\n";
+my @n=@ARGV;
+my @pattern132=pattern132(@n);
+say "(@pattern132)";
diff --git a/challenge-196/paulo-custodio/perl/ch-2.pl b/challenge-196/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..a5f5137546
--- /dev/null
+++ b/challenge-196/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+# Challenge 197
+#
+# Task 2: Range List
+# Submitted by: Mohammad S Anwar
+# You are given a sorted unique integer array, @array.
+#
+# Write a script to find all possible Number Range i.e [x, y] represent range
+# all integers from x and y (both inclusive).
+#
+#
+# Each subsequence of two or more contiguous integers
+#
+#
+# Example 1
+# Input: @array = (1,3,4,5,7)
+# Output: [3,5]
+# Example 2
+# Input: @array = (1,2,3,6,7,9)
+# Output: [1,3], [6,7]
+# Example 3
+# Input: @array = (0,1,2,4,5,6,8,9)
+# Output: [0,2], [4,6], [8,9]
+
+use Modern::Perl;
+
+sub range_list {
+ my(@n)=@_;
+ my @ranges;
+ my $s=0;
+ while ($s<@n) {
+ my $i=0;
+ while ($s+$i<@n && $n[$s]+$i==$n[$s+$i]) {
+ $i++;
+ }
+ if ($i>1) {
+ push @ranges, [@n[$s, $s+$i-1]];
+ $s+=$i;
+ }
+ else {
+ $s++;
+ }
+ }
+ return @ranges;
+}
+
+@ARGV or die "usage: ch-1.pl nums...\n";
+my @n=@ARGV;
+my @ranges=range_list(@n);
+say "(",join(", ", map {"[".$_->[0].",".$_->[1]."]"} @ranges),")";
diff --git a/challenge-196/paulo-custodio/t/test-1.yaml b/challenge-196/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..a756803c83
--- /dev/null
+++ b/challenge-196/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 3 1 4 2
+ input:
+ output: (1 4 2)
+- setup:
+ cleanup:
+ args: 1 2 3 4
+ input:
+ output: ()
+- setup:
+ cleanup:
+ args: 1 3 2 4 6 5
+ input:
+ output: (1 3 2)
+- setup:
+ cleanup:
+ args: 1 3 4 2
+ input:
+ output: (1 3 2)
diff --git a/challenge-196/paulo-custodio/t/test-2.yaml b/challenge-196/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..68fa3ada32
--- /dev/null
+++ b/challenge-196/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 1 3 4 5 7
+ input:
+ output: ([3,5])
+- setup:
+ cleanup:
+ args: 1 2 3 6 7 9
+ input:
+ output: ([1,3], [6,7])
+- setup:
+ cleanup:
+ args: 0 1 2 4 5 6 8 9
+ input:
+ output: ([0,2], [4,6], [8,9])
+- setup:
+ cleanup:
+ args: 0 2 4 6 8
+ input:
+ output: ()