Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2128,6 +2128,59 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
</a>
</td>
</tr>
<tr>
<td><a href="https://pt.wikipedia.org/wiki/Tabela_de_dispers%C3%A3o">Hash Table</a></td>
<td> <!-- C -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- C++ -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Python -->
<a href="./src/python/hash_table.py">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/python/python-original.svg" />
</a>
</td>
<td> <!-- Go -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Ruby -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- JavaScript -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Swift -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Rust -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
<td> <!-- Elixir -->
<a href="./CONTRIBUTING.md">
<img align="center" height="30" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
</tr>
</table>
<table align="center">
<tr>
Expand Down
86 changes: 86 additions & 0 deletions src/python/hash_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""
Implementação de uma hashtable
"""

import traceback


class HashTable:

def __init__(self, size=10):
self.size = size
self.tables = [[] for _ in range(self.size)]

def hashing(self, key):
return hash(key) % self.size

def __setitem__(self, key, value):
hash_key = self.hashing(key)
bucket = self.tables[hash_key]
if bucket:
for index, k_value in enumerate(bucket):
k, v = k_value
if key == k:
bucket[index] = key, value
return
bucket.append((key, value))

def __getitem__(self, key):
hash_key = self.hashing(key)
bucket = self.tables[hash_key]
if bucket:
for index, item in enumerate(bucket):
k, value = item
if key == k:
return value
raise KeyError(key)

def __delitem__(self, key):
hash_key = self.hashing(key)
bucket = self.tables[hash_key]
if bucket:
for index, item in enumerate(bucket):
k, value = item
if key == k:
del bucket[index]
return
raise KeyError(key)


if __name__ == '__main__':

# Cria as tabelas de dispersão
hash_table = HashTable()

# Insere as chaves e valores
for i in range(143):
hash_table[i] = hex(round(i * 23))

# Deletar item
print("Deletar chave 0 de valor: ", hash_table[0])
del hash_table[0]

# Procurar item
print('Valor da chave 23: ', hash_table[23])

# Subscrever um valor de uma chave
hash_table[1] = 'TESTE DE SUBSCREVER'

# Exibir dispersão
for i, table in enumerate(hash_table.tables):
print("Table {index} -> {table} Len -> {len}".
format(index=i + 1,
table=table,
len=len(table)))

# Tentar deletar uma chave que não existe
try:
del hash_table[493084]
except KeyError:
print(traceback.print_exc())

# Tentar acessar uma chave que não existe
try:
print(hash_table[493084])
except KeyError:
print(traceback.print_exc())