Skip to content

Commit f6b5879

Browse files
committed
improve -Wrote the test for top_one algorithm.
1 parent 7c05850 commit f6b5879

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

limit/test.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
import pytest
31
from .limit import limit_struct
42

53

top-one/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .top_one import top_one
2+
3+
4+
class TestTopOne:
5+
def test_top_one(self):
6+
assert top_one([1, 2, 1, 3, 4, 2, 2, 2]) == ([1, 2], 4)
7+
assert top_one([1]) == ([], 1)
8+
assert top_one([1, 2]) == ([], 1)
9+
assert top_one([]) == ()

top_one.py renamed to top-one/top_one.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ def top_one(array: list) -> list:
1515
counter = {}
1616
max_repeat = 0
1717

18-
for val in array:
19-
if val not in counter:
20-
counter[val] = 1
21-
else:
22-
counter[val] += 1
23-
if val not in result:
24-
result.append(val)
18+
try:
19+
for val in array:
20+
if val not in counter:
21+
counter[val] = 1
22+
else:
23+
counter[val] += 1
24+
if val not in result:
25+
result.append(val)
2526

26-
max_repeat = max(counter.values())
27+
max_repeat = max(counter.values())
2728

28-
return result, max_repeat
29+
return result, max_repeat
30+
except ValueError:
31+
return ()

0 commit comments

Comments
 (0)