diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 00:28:56 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-26 00:28:56 +0000 |
| commit | 203478bbfe21091f94c4559377f730f75e688dfd (patch) | |
| tree | ac6700738a351c6dbdbe0f16503cba14cf2d91d2 /challenge-196/paulo-custodio/forth | |
| parent | 6737a18810400c88848bb9364f9abf5297a5caef (diff) | |
| download | perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.gz perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.tar.bz2 perlweeklychallenge-club-203478bbfe21091f94c4559377f730f75e688dfd.zip | |
Add Perl, C, C++, BASIC and Forth solutions
Diffstat (limited to 'challenge-196/paulo-custodio/forth')
| -rw-r--r-- | challenge-196/paulo-custodio/forth/ch-1.fs | 64 | ||||
| -rw-r--r-- | challenge-196/paulo-custodio/forth/ch-2.fs | 62 |
2 files changed, 126 insertions, 0 deletions
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 |
