aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 13:13:23 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-02 13:13:23 +0000
commit9162319e3baf7440de59e1d240d39faecc844e43 (patch)
tree7d75b1af10c3762a05fa1dea4fd6f59f505b7aaf
parent7066c7942a06778399ffc17888dc4fb7876787b7 (diff)
downloadperlweeklychallenge-club-9162319e3baf7440de59e1d240d39faecc844e43.tar.gz
perlweeklychallenge-club-9162319e3baf7440de59e1d240d39faecc844e43.tar.bz2
perlweeklychallenge-club-9162319e3baf7440de59e1d240d39faecc844e43.zip
Add Forth solution
-rw-r--r--challenge-205/paulo-custodio/forth/ch-2.fs68
1 files changed, 68 insertions, 0 deletions
diff --git a/challenge-205/paulo-custodio/forth/ch-2.fs b/challenge-205/paulo-custodio/forth/ch-2.fs
new file mode 100644
index 0000000000..83f01cbdfe
--- /dev/null
+++ b/challenge-205/paulo-custodio/forth/ch-2.fs
@@ -0,0 +1,68 @@
+#! /usr/bin/env gforth
+
+\ Challenge 205
+\
+\ Task 2: Maximum XOR
+\ Submitted by: Mohammad S Anwar
+\
+\ You are given an array of integers.
+\
+\ Write a script to find the highest value obtained by XORing any two distinct members of the array.
+\ Example 1
+\
+\ Input: @array = (1,2,3,4,5,6,7)
+\ Output: 7
+\
+\ The maximum result of 1 xor 6 = 7.
+\
+\ Example 2
+\
+\ Input: @array = (2,4,1,3)
+\ Output: 7
+\
+\ The maximum result of 4 xor 3 = 7.
+\
+\ Example 3
+\
+\ Input: @array = (10,5,7,12,8)
+\ Output: 15
+\
+\ The maximum result of 10 xor 5 = 15.
+
+
+\ array of numbers, setup by collect-args
+0 VALUE items \ array of numbers
+0 VALUE num_items \ number of elements
+
+: items[] ( i -- addr )
+ CELLS items +
+;
+
+
+\ collect arguments from input and store in items
+: collect_args ( -- )
+ HERE TO items
+ BEGIN NEXT-ARG DUP WHILE \ while argments
+ 0 0 2SWAP >NUMBER 2DROP DROP ,
+ REPEAT
+ 2DROP
+ HERE items - CELL / TO num_items
+;
+
+
+\ compute max of XOR of all pairs
+: max_xor ( -- n )
+ 0 ( max )
+ num_items 1 > IF
+ num_items 1- 0 DO
+ num_items I 1+ DO
+ I items[] @
+ J items[] @ XOR MAX
+ LOOP
+ LOOP
+ THEN
+;
+
+
+collect_args max_xor . CR
+BYE