-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.py
49 lines (37 loc) · 1.26 KB
/
histogram.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
"""
3. Histogram
Write a Python function histogram(alist, bins)
that receives a alist of numbers and a tuple bins
indicating how numbers should be divided in groups.
The function returns the frequency distribution of the numbers
according to the division by bins.
Given alist=[1, 1, 1, 4, 5, 8, 10] and bins=(0, 3, 7, 12), then there is the
following frequency distribution:
bins | frequency
---------------------
[0, 3[ | 3
[3, 7[ | 2
[7, 12[ | 2
and, therefore, the function returns the list [3, 2, 2].
Save the program in the file histogram.py
For example:
● histogram([1, 1, 1, 4, 5, 8, 10], (0, 3, 7, 12))
returns the list [3, 2, 2]
● histogram([0, 3, 4, 7, 8, 1, 5], (0, 3, 7, 12))
returns the list [2, 3, 2]
● histogram([3, 0, 1, 5, 3, 2], (0, 3, 6))
returns the list [3, 3]
@author: Luísa Maria Mesquita
"""
def histogram(alist, bins):
lb = bins[0]
result = []
for b in range(1, len(bins)):
up = bins[b]
count = 0
for num in alist:
if(num in list(range(lb, up))):
count = count + 1
result.append(count)
lb = up
return result