 # Untitled

a guest
Oct 9th, 2019
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. proc intervalInsert(intervals: var seq[tuple], interval: tuple) =
2.   var i = -1
3.   var a = interval
4.   var b = interval
5.   var ni = (0, 0)
6.   var last: int
7.   var pos = -1
8.   while i < intervals.len:
9.     inc(i)
10.     var min = intervals[i]
11.     var max = intervals[i]
12.     if pos == -1:
13.       if a > max: continue
14.       elif a >= min and a <= max:
15.         ni = min
16.       elif a > min:
17.         ni = a
18.       pos = i
19.     else:
20.       if b > max:
21.         continue
22.       elif b >= min and b <= max:
23.         ni = max
24.       elif b < min:
25.         ni = b
26.       last = i
27.       break
28.
29.   for d in countdown(last, pos):
30.     intervals.delete(d)
31.   intervals.insert(ni, pos)
32.
33. var ivals = @[(1, 3), (6, 9)]
34. var nival = (2, 5)
35.
36. echo ivals, " ", nival
37. ivals.intervalInsert(nival)
38. echo ivals
39.
40.
41. ivals = @[(1,2),(3,5),(6,7),(8,10),(12,16)]
42. nival = (4, 8)
43.
44. echo ivals, " ", nival
45. ivals.intervalInsert(nival)
46. echo ivals
47.
48. #output -> ((1,5), (6,9))
49.
50. #[
51. ((a1,b1), (a2,b2), (an, bn))
52.
53. (a, b)
54.
55. loop thru interval list where n is current value and is either a min or max
56.   if a > n then continue else
57.     if a is min then
58.     if a is max then
59.
60.
61. if a inrange of an, bn
62. then find
63.   where b falls in interval list
64.     check every number, n, in the interval list
65.     if passed interval delete interval
66.     if b <= n
67.       if n is min
68. when is inrange of an, bn
69. ]#
RAW Paste Data