-
Notifications
You must be signed in to change notification settings - Fork 0
/
conjuntos.py
60 lines (54 loc) · 1.46 KB
/
conjuntos.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
57
58
59
60
#coding: UTF-8
#Functions called in gui_conjuntos.py
import sys
def ordenaVetor(vetor):
#Ordena em ordem crescente
for i in range(len(vetor) - 1):
for j in range(len(vetor) - 1):
if vetor[j] > vetor[j + 1]:
aux = vetor[j]
vetor[j] = vetor[j + 1]
vetor[j + 1] = aux
vetor = list(set(vetor)) #Remove itens duplicados
return vetor
def uniao(a, b):
#Elementos de A e elementos de B
uniao = []
for i in range(0, len(a)):
uniao.append(a[i])
for j in range(0, len(b)):
uniao.append(b[j])
uniao = ordenaVetor(uniao)
return uniao
def intersecao(a, b):
#Elementos comuns ao conjunto A e ao conjunto B
inter = []
for j in range(0, len(b)):
for i in range(0, len(a)):
if (a[i] == b[j]):
inter.append(a[i])
inter = ordenaVetor(inter)
return inter
def diferenca(a, b):
#Elementos exclusivos de A
for j in range(0, len(b)):
for i in range(0, len(a)):
if (a[i] == b[j]):
a[i] = 'null'
#Substitui interseções por 0 e as remove
while a.count('null') > 0:
a.remove('null')
a = ordenaVetor(a)
return a
def difSimetrica(a, b):
#Elementos de A ou elementos de B mas não de ambos
difS = uniao(a, b)
inter = intersecao(a, b)
for i in range(0, len(difS)):
for j in range(0, len(inter)):
if(difS[i] == inter[j]):
difS[i] = 'null'
#União - Interseção
while difS.count('null') > 0:
difS.remove('null')
return difS