Skip to content

Commit 2a14fe5

Browse files
authored
Create automatic histogram.py
1 parent 098494a commit 2a14fe5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List, Dict
2+
3+
def create_value_count(A: List) -> Dict:
4+
val_cnt = {}
5+
for a in A:
6+
if a in val_cnt:
7+
val_cnt[a] += 1
8+
else:
9+
val_cnt[a] = 1
10+
return val_cnt
11+
12+
13+
def automatic_histogram(dataset, x):
14+
element_count = create_value_count(dataset)
15+
16+
unique_elements = list(element_count.keys())
17+
18+
i = 0
19+
step = len(unique_elements) // x
20+
if len(unique_elements) % x != 0:
21+
step += 1
22+
buckets = {}
23+
while i < len(unique_elements):
24+
tup = tuple(unique_elements[i:i+step])
25+
buckets[tup] = 0
26+
i += step
27+
28+
histogram = {}
29+
for e, count in element_count.items():
30+
for bucket in buckets:
31+
if e in bucket:
32+
if len(bucket) > 1:
33+
bucket_str = str(min(bucket))+'-'+str(max(bucket))
34+
else:
35+
bucket_str = str(bucket[0])
36+
if bucket_str in histogram:
37+
histogram[bucket_str] += count
38+
else:
39+
histogram[bucket_str] = count
40+
break
41+
return histogram

0 commit comments

Comments
 (0)