generated from ISP19/unittesting-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listutil.py
44 lines (35 loc) · 1.02 KB
/
listutil.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
ERROR_MESSAGES = {
"invalid_type": "Invalid Type"
}
class InvalidTypeException(Exception):
def __init__(self):
super().__init__(ERROR_MESSAGES["invalid_type"])
def unique(lst):
"""Returns a list containing only the first occurrence of each distinct
element in list. That is, all duplicates are omitted.
Arguments:
lst: a list of elements (not modified)
Returns:
a new list containing only distinct elements from list
(error)
Examples:
>>> unique([5])
[5]
>>> unique(["b","a","a","b","b","b","a","a"])
['b', 'a']
>>> unique([])
[]
"""
if type(lst) is not list:
raise InvalidTypeException()
freq_list = []
for item in lst:
if item not in freq_list:
freq_list.append(item)
return list(freq_list)
if __name__ == "__main__":
"""Run the doctests in all methods."""
# from random import randint
# print([randint(0, 22) for i in range(10000)])
import doctest
doctest.testmod(verbose=True)