-
Notifications
You must be signed in to change notification settings - Fork 0
/
30 - unique_elements.py
56 lines (39 loc) · 1.55 KB
/
30 - unique_elements.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
50
51
52
53
54
55
56
import sys, os
sys.path.append(os.path.dirname(__file__) + "/..")
import useful_functions as useful
def main():
array = useful.random_array(-10, 10, 20, "y")
print("\nSource Array = ")
useful.print_array(array)
print("\nUnique Elements From Array = ")
print("Sorted: ", end="")
useful.print_array(uniques_sorted(array.copy()))
print("Keep Order: ", end="")
useful.print_array(unique_elements(array.copy()))
# Сортируем список, после чего поочерёдно проверяем элементы на уникальность.
def uniques_sorted(user_list):
user_list.sort()
uniques = []
for index in range(0, len(user_list)):
current = user_list[index]
if index == 0:
next = user_list[index + 1]
if current != next:
uniques.append(current)
elif index == len(user_list) - 1:
previous = user_list[index - 1]
if current != previous:
uniques.append(current)
else:
next = user_list[index + 1]
previous = user_list[index - 1]
if current != next and current != previous:
uniques.append(current)
return uniques
# Альтернативная реализация, в которой мы используем метод list.count().
def unique_elements(user_list):
uniques = []
for x in user_list:
if user_list.count(x) == 1: uniques.append(x)
return uniques
main()