aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-11-18 19:14:44 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-11-18 19:14:44 +0000
commit3f2e90968b6c2f270508604f6c077e00405fbdfa (patch)
tree8800b2fc841a84c19df2d45265d793998eb7e3c8 /challenge-243
parent071507ff37c4526c29c02e689137780d3519d7f9 (diff)
downloadperlweeklychallenge-club-3f2e90968b6c2f270508604f6c077e00405fbdfa.tar.gz
perlweeklychallenge-club-3f2e90968b6c2f270508604f6c077e00405fbdfa.tar.bz2
perlweeklychallenge-club-3f2e90968b6c2f270508604f6c077e00405fbdfa.zip
Add Forth solutions
Diffstat (limited to 'challenge-243')
-rw-r--r--challenge-243/paulo-custodio/forth/ch-1.fs58
-rw-r--r--challenge-243/paulo-custodio/forth/ch-2.fs56
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-243/paulo-custodio/forth/ch-1.fs b/challenge-243/paulo-custodio/forth/ch-1.fs
new file mode 100644
index 0000000000..5325b2cc8b
--- /dev/null
+++ b/challenge-243/paulo-custodio/forth/ch-1.fs
@@ -0,0 +1,58 @@
+#! /usr/bin/env gforth
+
+\ Challenge 243
+\
+\ Task 1: Reverse Pairs
+\ Submitted by: Mohammad S Anwar
+\ You are given an array of integers.
+\
+\ Write a script to return the number of reverse pairs in the given array.
+\
+\ A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j].
+\
+\ Example 1
+\ Input: @nums = (1, 3, 2, 3, 1)
+\ Output: 2
+\
+\ (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+\ (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+\ Example 2
+\ Input: @nums = (2, 4, 3, 5, 1)
+\ Output: 3
+\
+\ (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+\ (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+\ (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: 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
+;
+
+: count_reverse_pairs ( -- n )
+ 0 { count }
+ nums_size 1- 0 DO
+ nums_size I 1+ DO
+ J nums[] @ I nums[] @ 2* > IF
+ count 1+ TO count
+ THEN
+ LOOP
+ LOOP
+ count
+;
+
+\ main
+collect_args
+count_reverse_pairs . CR
+BYE
diff --git a/challenge-243/paulo-custodio/forth/ch-2.fs b/challenge-243/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..d762dbf362
--- /dev/null
+++ b/challenge-243/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,56 @@
+#! /usr/bin/env gforth
+
+\ Challenge 243
+\
+\ Task 1: Reverse Pairs
+\ Submitted by: Mohammad S Anwar
+\ You are given an array of integers.
+\
+\ Write a script to return the number of reverse pairs in the given array.
+\
+\ A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j].
+\
+\ Example 1
+\ Input: @nums = (1, 3, 2, 3, 1)
+\ Output: 2
+\
+\ (1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1
+\ (3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1
+\ Example 2
+\ Input: @nums = (2, 4, 3, 5, 1)
+\ Output: 3
+\
+\ (1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1
+\ (2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1
+\ (3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1
+
+CREATE nums 256 CELLS ALLOT
+0 VALUE nums_size
+
+: 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
+;
+
+: sum_floor ( -- n )
+ 0 { sum }
+ nums_size 0 DO
+ nums_size 0 DO
+ sum J nums[] @ I nums[] @ / + TO sum
+ LOOP
+ LOOP
+ sum
+;
+
+\ main
+collect_args
+sum_floor . CR
+BYE