aboutsummaryrefslogtreecommitdiff
path: root/challenge-201/paulo-custodio/forth/ch-1.fs
blob: 343135c81e547ac75d91a8df51b74b3af5627c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env gforth

\ Challenge 201
\
\ Task 1: Missing Numbers
\ Submitted by: Mohammad S Anwar
\
\ You are given an array of unique numbers.
\
\ Write a script to find out all missing numbers in the range 0..$n where $n
\ is the array size.
\
\ Example 1
\
\ Input: @array = (0,1,3)
\ Output: 2
\
\ The array size i.e. total element count is 3, so the range is 0..3.
\ The missing number is 2 in the given array.
\
\ Example 2
\
\ Input: @array = (0,1)
\ Output: 2
\
\ The array size is 2, therefore the range is 0..2.
\ The missing number is 2.

CREATE nums 256 CELLS ALLOT
0 VALUE nums_size

: nums[] ( i -- addr )
    CELLS nums +
;

: find_nums { n -- f }
    FALSE                   ( f )
    nums_size 0 DO
        I nums[] @ n = IF
            DROP TRUE LEAVE
        THEN
    LOOP
;

: collect_args ( -- )
    BEGIN NEXT-ARG DUP WHILE
        0 0 2SWAP >NUMBER 2DROP DROP
        nums_size nums[] !
        nums_size 1+ TO nums_size
    REPEAT
    2DROP
;

: .missing ( -- )
    nums_size 1+ 0 DO
        I find_nums 0= IF I . THEN
    LOOP
    CR
;

collect_args .missing BYE