-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
36 lines (25 loc) · 897 Bytes
/
data.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
import math
import exceptions
class Data:
'''
data takes symbols and the associated probabilities as a list
eg:
symbols = ['a', 'b', 'c']
prob = [0.5, 0.25, 0.25]
'''
def __init__(self, symbols: list, prob: list, d = [0,1]):
self.symbols = symbols
self.prob = prob
self.d = d
if len(self.symbols) != len(set(self.symbols)):
raise(exceptions.Duplicate(f"Duplicate Symbols detected"))
if len(self.symbols) != len(self.prob):
raise(exceptions.SrcDataMismatch(f"Symbol and probability length mismatch \nExpected {max(self.symbols, self.prob)} got {min(self.symbols, self.prob)}"))
for p in self.prob:
if isinstance(p, float):
pass
else:
raise(exceptions.TypeError(f"Expected float got {type(p)}"))
if sum(self.prob) != 1:
print("here")
raise(exceptions.ProbabilityMismatch(f"Probabilities don't add to 1, got {sum(self.prob)}"))