aboutsummaryrefslogtreecommitdiff
path: root/challenge-092/paulo-custodio/basic/ch-2.bas
blob: dc292b276997f23f6d60422144b07d2b0dc889b7 (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
62
63
64
65
66
' Challenge 092
'
' TASK #2 � Insert Interval
' Submitted by: Mohammad S Anwar
' You are given a set of sorted non-overlapping intervals and a new interval.
'
' Write a script to merge the new interval to the given set of intervals.
'
' Example 1:
' Input $S = (1,4), (8,10); $N = (2,6)
' Output: (1,6), (8,10)
' Example 2:
' Input $S = (1,2), (3,7), (8,10); $N = (5,8)
' Output: (1,2), (3,10)
' Example 3:
' Input $S = (1,5), (7,9); $N = (10,11)
' Output: (1,5), (7,9), (10,11)

redim shared timeline(1) as Boolean

sub fill_timeline()
    dim i as integer, j as integer, p as integer, bg as integer, ed as integer
    i=1
    do while command(i)<>""
        ' parse begin,end
        p=instr(command(i),",")
        if p=0 then error 5
        bg=val(command(i))
        ed=val(mid(command(i),p+1))
        ' resize timeline if needed
        if 2*ed>ubound(timeline) then
            redim preserve timeline(2*ed+2)
        end if

        ' fill interval
        for j=2*bg to 2*ed
            timeline(j)=true
        next
        i=i+1
    loop
end sub

sub print_timeline()
    dim i as integer
    redim intervals(0) as integer

    for i=lbound(timeline) to ubound(timeline)-1
        if timeline(i)=false and timeline(i+1)=true then
            redim preserve intervals(ubound(intervals)+1)
            intervals(ubound(intervals))=int(i/2)+1
        elseif timeline(i)=true and timeline(i+1)=false then
            redim preserve intervals(ubound(intervals)+1)
            intervals(ubound(intervals))=int(i/2)
        end if
    next

    for i=1 to ubound(intervals) step 2
        print "(";trim(str(intervals(i)));",";trim(str(intervals(i+1)));")";
        if i+2<ubound(intervals) then print ", ";
    next
    print
end sub

' main
fill_timeline
print_timeline