File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments