Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit66d7f97

Browse files
matheustguimaraeskeon
authored andcommitted
Add histogram algorithm (keon#572)
* Add histogram algorithm* Add histogram in README.md* Add histogram tests* Fix typo in documentation
1 parentd6ca8b0 commit66d7f97

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ If you want to uninstall algorithms, it is as simple as:
123123
-[pacific_atlantic](algorithms/dfs/pacific_atlantic.py)
124124
-[sudoku_solver](algorithms/dfs/sudoku_solver.py)
125125
-[walls_and_gates](algorithms/dfs/walls_and_gates.py)
126+
-[distribution](algorithms/distribution)
127+
-[histogram](algorithms/distribution/histogram.py)
126128
-[dp](algorithms/dp)
127129
-[buy_sell_stock](algorithms/dp/buy_sell_stock.py)
128130
-[climbing_stairs](algorithms/dp/climbing_stairs.py)

‎algorithms/distribution/__init__.py

Whitespace-only changes.

‎algorithms/distribution/histogram.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Histogram function.
3+
4+
Histogram is an accurate representation of the distribution of numerical data.
5+
It is an estimate of the probability distribution of a continuous variable.
6+
https://en.wikipedia.org/wiki/Histogram
7+
8+
Example:
9+
list_1 = [3, 3, 2, 1]
10+
:return {1: 1, 2: 1, 3: 2}
11+
12+
list_2 = [2, 3, 5, 5, 5, 6, 4, 3, 7]
13+
:return {2: 1, 3: 2, 4: 1, 5: 3, 6: 1, 7: 1}
14+
"""
15+
16+
17+
defget_histogram(input_list:list)->dict:
18+
"""
19+
Get histogram representation
20+
:param input_list: list with different and unordered values
21+
:return histogram: dict with histogram of input_list
22+
"""
23+
# Create dict to store histogram
24+
histogram= {}
25+
# For each list value, add one to the respective histogram dict position
26+
foriininput_list:
27+
histogram[i]=histogram.get(i,0)+1
28+
returnhistogram

‎tests/test_histogram.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fromalgorithms.distribution.histogramimportget_histogram
2+
3+
importunittest
4+
5+
6+
classTestListsInHistogram(unittest.TestCase):
7+
deftest_histogram(self):
8+
list_1= [3,3,2,1]
9+
list_2= [2,3,5,5,5,6,4,3,7]
10+
11+
self.assertEqual(get_histogram(list_1), {1:1,2:1,3:2})
12+
self.assertEqual(get_histogram(list_2),
13+
{2:1,3:2,4:1,5:3,6:1,7:1})
14+
15+
16+
if__name__=='__main__':
17+
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp