-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.py
More file actions
87 lines (67 loc) · 2.39 KB
/
Hash.py
File metadata and controls
87 lines (67 loc) · 2.39 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 29 16:45:32 2023
"""
import csv
import numpy as np
class HashTabel:
def __init__(self): #Inicializar
self.max = 6
self.arr = [ [] for i in range(self.max)]
def get_hash(self, key): #Fazer a função de hash baseado em divisão
h = 0
for char in key:
h += ord(char)
return h % self.max
def __getitem__(self,key): # Acessar o data
h = self.get_hash(key)
for index, element in enumerate(self.arr[h]):
if key == element[0]:
return element[1]
def __setitem__(self, key, valor): #Mudar o data
h = self.get_hash(key)
if len(self.arr[h]) == 0: #Olhar se não já foi usado aquele valor de hash
self.arr[h].append((key,valor))
found = False
for index, element in enumerate(self.arr[h]): #Olhar se já tem essa "key"
if key == element[0]:
self.arr[h][index] = (key, valor)
found = True
if found == False:
self.arr[h].append((key,valor)) #Se não tem, vamos criar e colocar um novo
return
def __delitem__(self, key): #Deletar um valor
h = self.get_hash(key)
for index, element in enumerate(self.arr[h]):
if key == element[0]:
del self.arr[h][index]
def load(self, data): #De uma lista de tuplas criar o hash tabela correspondente
for index in np.arange( len(data) ):
key = data[index][0]
valor = data[index][1]
self[key] = valor
return
def destribution(self): #Para ter uma ideá se o data é bem distribuição
p = []
for i in np.arange( len(self.arr) ):
p.append(len(self.arr[i]))
return p,np.var(p)
# Baixando a dada escolhido
weather = []
with open("nyc_weather.csv", "r") as f:
for line in f:
print(line)
tokens = line.split(",")
date = tokens[0]
temperature = float(tokens[1])
weather.append((date,temperature))
del weather[0]
s = HashTabel()
load(s, weather)
#Testar que as funções estão funcionando
s['Jan 9'] = 100.0
print(s.arr)
del s['Jan 10']
print(s.arr)
p, var = destribution(s)
print(p,var)