-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
68 lines (55 loc) · 1.85 KB
/
task2.py
File metadata and controls
68 lines (55 loc) · 1.85 KB
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
66
67
68
# Let make a histogram for for Population Estimation in 2015 for all 56 states.
population_index = 12
highest_population = 0
lowest_population = 0
population_data = []
state_index = 3
no_of_buckets = 10
bin_interval = 0
bucket_lower_limit = 0
bucket_upper_limit = 0
buckets = []
class Bucket(object):
upper_limit = 0
lower_limit = 0
def __init__(self, upper_limit, lower_limit):
self.upper_limit = upper_limit
self.lower_limit = lower_limit
self.entries = []
self.count = 0
def add_element(self, element):
if self.is_element_in_range(element):
self.entries.append(element)
else:
raise ValueError('Element Not in range')
def is_element_in_range(self, element):
if element >= self.lower_limit and element < self.upper_limit:
return True
else:
return False
def update_count(self):
self.count = len(self.entries)
f_obj = open('NST-EST2015-alldata.csv')
data = f_obj.readlines()
f_obj.close()
for record in data[1:]:
record_data = record.split(',')
if int(record_data[state_index]) != 0:
population_data.append(int(record_data[population_index]))
highest_population = max(population_data)
lowest_population = min(population_data)
bin_interval = int((highest_population - lowest_population) / no_of_buckets) + 1
for b in range(1, no_of_buckets + 1):
if b == 1:
bucket_lower_limit = lowest_population
else:
bucket_lower_limit = bucket_upper_limit
bucket_upper_limit = bucket_lower_limit + bin_interval
buckets.append(Bucket(bucket_upper_limit, bucket_lower_limit))
for state_pop in population_data:
for b in buckets:
if b.is_element_in_range(state_pop):
b.add_element(state_pop)
b.update_count()
for b in buckets:
print("Bucket(" + str(b.__dict__) + ")")