From 203478bbfe21091f94c4559377f730f75e688dfd Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 26 Mar 2023 00:28:56 +0000 Subject: Add Perl, C, C++, BASIC and Forth solutions --- challenge-196/paulo-custodio/Makefile | 2 + challenge-196/paulo-custodio/basic/ch-1.bas | 53 ++++++++++++++++++++++ challenge-196/paulo-custodio/basic/ch-2.bas | 60 +++++++++++++++++++++++++ challenge-196/paulo-custodio/c/ch-1.c | 69 +++++++++++++++++++++++++++++ challenge-196/paulo-custodio/c/ch-2.c | 67 ++++++++++++++++++++++++++++ challenge-196/paulo-custodio/cpp/ch-1.cpp | 60 +++++++++++++++++++++++++ challenge-196/paulo-custodio/cpp/ch-2.cpp | 59 ++++++++++++++++++++++++ challenge-196/paulo-custodio/forth/ch-1.fs | 64 ++++++++++++++++++++++++++ challenge-196/paulo-custodio/forth/ch-2.fs | 62 ++++++++++++++++++++++++++ challenge-196/paulo-custodio/perl/ch-1.pl | 47 ++++++++++++++++++++ challenge-196/paulo-custodio/perl/ch-2.pl | 51 +++++++++++++++++++++ challenge-196/paulo-custodio/t/test-1.yaml | 20 +++++++++ challenge-196/paulo-custodio/t/test-2.yaml | 20 +++++++++ 13 files changed, 634 insertions(+) create mode 100644 challenge-196/paulo-custodio/Makefile create mode 100644 challenge-196/paulo-custodio/basic/ch-1.bas create mode 100644 challenge-196/paulo-custodio/basic/ch-2.bas create mode 100644 challenge-196/paulo-custodio/c/ch-1.c create mode 100644 challenge-196/paulo-custodio/c/ch-2.c create mode 100644 challenge-196/paulo-custodio/cpp/ch-1.cpp create mode 100644 challenge-196/paulo-custodio/cpp/ch-2.cpp create mode 100644 challenge-196/paulo-custodio/forth/ch-1.fs create mode 100644 challenge-196/paulo-custodio/forth/ch-2.fs create mode 100644 challenge-196/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-196/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-196/paulo-custodio/t/test-1.yaml create mode 100644 challenge-196/paulo-custodio/t/test-2.yaml 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)"" + 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 +#include + +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 +#include + +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; i1) { + 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 +#include + +void print_patter132(const std::vector& nums) { + int nums_size=static_cast(nums.size()); + std::cout<<"("; + for (int i=0; i nums; + for (int i=0; i +#include +#include + +void print_ranges(const std::vector& nums) { + int nums_size=static_cast(nums.size()); + std::cout<<"("; + std::string sep=""; + for (int i=0; i1) { + std::cout< nums; + for (int i=0; iNUMBER 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: () -- cgit