aboutsummaryrefslogtreecommitdiff
path: root/challenge-199/paulo-custodio/forth/ch-2.fs
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-199/paulo-custodio/forth/ch-2.fs')
-rw-r--r--challenge-199/paulo-custodio/forth/ch-2.fs86
1 files changed, 86 insertions, 0 deletions
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