-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d9559d3
Showing
8 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Current File", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "${file}", | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# https://colab.research.google.com/drive/1B4kcOEG5iDkT8N07NjPnOOXXWcp6VfZo?usp=sharing | ||
# https://wiki.python.org/moin/TimeComplexity | ||
|
||
import timeit | ||
|
||
# Função 1 - O(n) | ||
time_in_seconds = timeit.timeit("soma1(10)", | ||
'''def soma1(n): | ||
soma = 0; | ||
for i in range(n + 1): | ||
soma += i; return soma''', number=1000) | ||
|
||
print(f'\nEficiência da função 1 em segundos: {time_in_seconds}') | ||
|
||
# Função 2 - O(3) | ||
time_in_seconds = timeit.timeit("soma2(10)", ''' | ||
def soma2(n): | ||
return(n * (n + 1)) / 2 | ||
''', number = 1000) | ||
|
||
print(f'Eficiência da função 2 em segundos: {time_in_seconds}\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
print('\n-=-=-=--=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=') | ||
|
||
import timeit as t | ||
# Definindo a eficiência do algoritmo 1 | ||
|
||
# O(1000) -> O(n) | ||
def lista1(): | ||
lista = [] | ||
for i in range(1000): | ||
# lista.append(i) | ||
lista += [i] | ||
return lista | ||
|
||
seconds = t.timeit("lista1()", ''' | ||
def lista1(): | ||
lista = [] | ||
for i in range(1000): | ||
lista += [i] | ||
return lista''', | ||
number=1000) | ||
|
||
print(seconds) | ||
|
||
def lista2(): | ||
return range(1000) | ||
|
||
seconds2 = t.timeit("lista2()", ''' | ||
def lista2(): | ||
return range(1000)''', | ||
number=1000) | ||
print(seconds2) | ||
|
||
|
||
print('\n-=-=-=--=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
''' | ||
Funções Big-O | ||
* 1 Constant | ||
* log(n) Logarithmic | ||
* n Linear | ||
* nlog(n) Log Linear | ||
* n^2 Quadratic | ||
* n^3 Cubic | ||
* 2^n Exponetial | ||
''' | ||
|
||
from math import log | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
n = np.linspace(1, 10, 100) | ||
|
||
labels = ['Constant', 'Logarithmic', 'Linear', 'Log linear', 'Quadratic', 'Cubic', 'Exponential'] | ||
big_o = [np.ones(n.shape), np.log(n), n, n * np.log(n), n**2, n**3, 2**n] | ||
|
||
plt.figure(figsize=(10,8)) | ||
plt.ylim(0, 100) | ||
|
||
for i in range(len(big_o)): | ||
plt.plot(n, big_o[i], label = labels[i]) | ||
plt.legend() | ||
plt.ylabel('Runtime') | ||
plt.xlabel('n') | ||
plt.title("Complexido de Tempo das funções Big O") | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Exemplo de Big-O | ||
|
||
lista = [1, 2, 3, 4, 5] | ||
|
||
# Constant - O(1) | ||
def constant(n): | ||
print(n[0]) | ||
|
||
# constant(lista) | ||
|
||
# Linear - O(n) | ||
def linear(n): | ||
for i in n: | ||
print(i) | ||
|
||
# linear(lista) | ||
|
||
# Quadractic - O(n^2) - polynomial | ||
def quadratic(n): | ||
for i in n: | ||
for j in n: | ||
print(i, j) | ||
print('---') | ||
|
||
# quadratic(lista) | ||
|
||
# Combination | ||
|
||
# O(1) + O(5) + O(n) + O(n) + O(3) | ||
# O(9) + O(2n) -> O(n) | ||
def combination(n): | ||
# O(1) | ||
print(n[0]) | ||
|
||
# O(5) | ||
for i in range(5): | ||
print('test ', i) | ||
|
||
# O(n) | ||
for i in n: | ||
print(i) | ||
|
||
# O(n) | ||
for i in n: | ||
print(i) | ||
|
||
# O(3) | ||
print('Python') | ||
print('Python') | ||
print('Python') | ||
|
||
combination(lista) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Vetor não ordernado | ||
print() | ||
import numpy as np | ||
|
||
class VetorNaoOrdernado: | ||
def __init__(self, capacidade): | ||
self.capacidade = capacidade | ||
self.ultima_posicao = -1 | ||
self.valores = np.empty(self.capacidade, dtype=int) | ||
|
||
def __str__(self): | ||
representation = f"Lista: {self.valores} | Tamanho: {len(self.valores)}" | ||
return representation | ||
|
||
# O(n) | ||
def imprime(self): | ||
if self.ultima_posicao == -1: | ||
print('O vetor está vazio!') | ||
else: | ||
for i in range(self.ultima_posicao + 1): | ||
print(i, ' - ', self.valores[i]) | ||
|
||
# O(1) - O(2) | ||
def insere(self, valor): | ||
if self.ultima_posicao == self.capacidade - 1: | ||
print('Capacidade máxima atingida!') | ||
else: | ||
self.ultima_posicao += 1 | ||
self.valores[self.ultima_posicao] = valor | ||
|
||
# O(n) | ||
def pesquisar(self, valor): | ||
for i in range(self.ultima_posicao + 1): | ||
if valor == self.valores[i]: | ||
return i | ||
return None | ||
|
||
# O(n) | ||
def excluir(self, valor): | ||
posicao = self.pesquisar(valor) | ||
if posicao is None: | ||
return None | ||
else: | ||
for i in range(posicao, self.ultima_posicao): | ||
self.valores[i] = self.valores[i + 1] | ||
self.ultima_posicao -= 1 | ||
|
||
vetor = VetorNaoOrdernado(10) | ||
vetor.insere(3) | ||
vetor.insere(2) | ||
vetor.insere(4) | ||
vetor.insere(5) | ||
vetor.insere(6) | ||
vetor.insere(1) | ||
|
||
vetor.imprime() | ||
|
||
vetor.pesquisar(5) | ||
|
||
vetor.pesquisar(9) | ||
|
||
vetor.excluir(5) | ||
print('-='*10) | ||
# vetor.imprime() | ||
print(vetor) | ||
|
||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
''' | ||
* Inserção | ||
4, 2, 1, 8, 5, _ , _ | ||
- Único passo (inserido na primeira célula vaga do vetor) | ||
- O algoritmo já conhece a lo | ||
* Busca | ||
* Remoção | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
|
||
class VetorOrdernado(): | ||
'''Classe de vetores ordenados - Insira o tamanho do vetor ao instanciar a classe''' | ||
def __init__(self, capacidade): | ||
import numpy as np | ||
self.capacidade = capacidade | ||
self.ultima_posicao = -1 | ||
self.valores = np.empty(self.capacidade, dtype=int) | ||
|
||
# O(n) | ||
def imprime(self): | ||
if self.ultima_posicao == -1: | ||
print('O vetor está vazio') | ||
else: | ||
for i in range(self.ultima_posicao + 1): | ||
print(f'{i} - {self.valores[i]}') | ||
|
||
# O(n) | ||
def insere(self, valor): | ||
if self.ultima_posicao == self.capacidade - 1: | ||
print('Capacidade máxima atingida') | ||
return | ||
|
||
posicao = 0 | ||
for i in range(self.ultima_posicao + 1): | ||
posicao = i | ||
if self.valores[i] > valor: | ||
break | ||
if i == self.ultima_posicao: | ||
posicao = i + 1 | ||
|
||
x = self.ultima_posicao | ||
while x >= posicao: | ||
self.valores[x+1] = self.valores[x] | ||
x -= 1 | ||
|
||
self.valores[posicao] = valor | ||
self.ultima_posicao += 1 | ||
|
||
# O(n) | ||
def pesquisar(self, valor): | ||
for i in range(self.ultima_posicao + 1): | ||
if self.valores[i] > valor: | ||
return -1 | ||
if self.valores[i] == valor: | ||
return i | ||
if i == self.ultima_posicao: | ||
return -1 | ||
|
||
def excluir(self, valor): | ||
posicao = self.pesquisar(valor) | ||
if posicao == -1: | ||
return -1 | ||
else: | ||
for i in range(posicao, self.ultima_posicao): | ||
self.valores[i] = self.valores[i+1] | ||
|
||
self.ultima_posicao -= 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
vetor = VetorOrdernado(10) | ||
|
||
vetor.insere(6) | ||
vetor.insere(4) | ||
vetor.insere(3) | ||
vetor.insere(5) | ||
vetor.insere(1) | ||
vetor.insere(8) | ||
|
||
vetor.pesquisar(3) | ||
vetor.pesquisar(2) | ||
vetor.pesquisar(9) | ||
|
||
vetor.excluir(5) | ||
vetor.excluir(9) |