-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
65 lines (50 loc) · 1.72 KB
/
example.py
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
from __future__ import print_function
from fenwick import FenwickTree
n = 10
print('Note: Indexing is 0-based.')
print()
print('*******************************************')
print('* Initialize FenwickTree and update ')
print('* frequencies one at a time. ')
print('*******************************************')
print()
fenwick_tree = FenwickTree(n)
for x in range(n):
fenwick_tree.add(x, x + 1)
print('*******************************************')
print('* Initialize FenwickTree using existing ')
print('* frequencies. ')
print('*******************************************')
print()
fenwick_tree = FenwickTree(n)
f = range(1, n + 1)
fenwick_tree.init(f)
print('*******************************************')
print('* Calculate and print sum of all ')
print('* frequencies ')
print('*******************************************')
print()
sum_all = fenwick_tree.prefix_sum(n)
print(sum_all)
print()
print('*******************************************')
print('* Calculate and print sum of frequencies ')
print('* 2 through 8. ')
print('*******************************************')
print()
sum_2_8 = fenwick_tree.range_sum(2, 9)
print(sum_2_8)
print()
print('*******************************************')
print('* Calculate and print frequencies 5 ')
print('* through 9. ')
print('*******************************************')
print()
for x in range(5, 10):
print(fenwick_tree[x])
print()
print('*******************************************')
print('* Retrieve and print all frequencies. ')
print('*******************************************')
print()
print(fenwick_tree.frequencies())