1 | use Treap |
---|
2 | |
---|
3 | class TestTreap |
---|
4 | def main |
---|
5 | tr = Treap<of String, int>() |
---|
6 | for l in File.readAllLines('TestTreap.cobra') |
---|
7 | l = l.replace('.', ' ') |
---|
8 | l = l.replace(',', ' ') |
---|
9 | l = l.replace('(', ' ') |
---|
10 | l = l.replace(')', ' ') |
---|
11 | l = l.replace(r':', ' ') |
---|
12 | l = l.replace(r'[', r' [ ') |
---|
13 | l = l.replace(r']', r' ] ') |
---|
14 | l = l.replace(r'{', r' { ') |
---|
15 | l = l.replace(r'}', r' } ') |
---|
16 | l = l.replace(r'"', ' " ') |
---|
17 | l = l.replace(r'#', '# ') |
---|
18 | l = l.replace(r'==', '== ') |
---|
19 | for t in l.split(c' ') |
---|
20 | t = t.trim |
---|
21 | if not t.length, continue |
---|
22 | if tr.contains(t) |
---|
23 | tr[t] = tr[t] + 1 |
---|
24 | else |
---|
25 | tr[t] = 1 |
---|
26 | |
---|
27 | #print 'ntokens=', tr.count |
---|
28 | #print 'min [tr.getMinKey] = [tr.getMinValue]' |
---|
29 | #print 'max [tr.getMaxKey] = [tr.getMaxValue]' |
---|
30 | |
---|
31 | #print '== Ascending== ' |
---|
32 | minKey as String? |
---|
33 | maxKey as String? |
---|
34 | count = 0 |
---|
35 | for v in tr # .ascendingOrder |
---|
36 | #print ' [v.key] = [v.value]' |
---|
37 | if not minKey |
---|
38 | minKey = v.key |
---|
39 | minVal = v.value |
---|
40 | maxKey = v.key |
---|
41 | maxVal = v.value |
---|
42 | count += 1 |
---|
43 | |
---|
44 | assert minKey == tr.getMinKey |
---|
45 | assert minVal == tr.getMinValue |
---|
46 | assert maxKey == tr.getMaxKey |
---|
47 | assert maxVal == tr.getMaxValue |
---|
48 | assert count == tr.count |
---|
49 | |
---|
50 | #print '== Descending== ' |
---|
51 | maxKey = nil |
---|
52 | for v in tr.descendingOrder |
---|
53 | #print ' [v.key] = [v.value]' |
---|
54 | if not maxKey |
---|
55 | maxKey = v.key |
---|
56 | maxVal = v.value |
---|
57 | minKey = v.key |
---|
58 | minVal = v.value |
---|
59 | count -= 1 |
---|
60 | |
---|
61 | assert minKey == tr.getMinKey |
---|
62 | assert minVal == tr.getMinValue |
---|
63 | assert maxKey == tr.getMaxKey |
---|
64 | assert maxVal == tr.getMaxValue |
---|
65 | assert count == 0 |
---|