-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHashTable.py
More file actions
88 lines (67 loc) · 1.94 KB
/
HashTable.py
File metadata and controls
88 lines (67 loc) · 1.94 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
88
__author__ = 'rohanmathure'
import fileinput
class MyHashtable:
def __init__(self,num):
self.entryNum = num
self.entryList = [None] * num
def put(self, k, v):
found=False
if k is None or v is None:
return
try:
hashValue=hash(k) % self.entryNum
if self.entryList[hashValue] is None:
self.entryList[hashValue]=[[k,v]]
else:
for entry in self.entryList[hashValue]:
if entry[0]==k:
entry[1]=v
found=True
if not found:
self.entryList[hashValue].append([k,v])
except Exception:
return
def get(self, k):
hashValue = hash(k)%self.entryNum
if not self.entryList[hashValue] is None:
for key,value in self.entryList[hashValue]:
if k==key:
return value
return None
if __name__ == "__main__":
hashtable = MyHashtable(100)
for line in fileinput.input():
line = line.strip()
k, v = line.split('=') if '=' in line else (line, None)
if v:
hashtable.put(k, v)
else:
print str(hashtable.get(k)).replace('None', 'null')
import unittest
class TestHash(unittest.TestCase):
def setUp(self):
self.hashTable = MyHashtable(100)
def testHash(self):
self.hashTable.put(2,'a')
self.hashTable.put(2,'b')
self.assertEqual(self.hashTable.get(2),'b')
def isPrime(n):
if n<2:
return False
i =2
while i<n:
if n%i==0:
return False
else:
i+=1
return True
def getNumberOfPrimes(n):
cnt=0
for i in range(1,n+1):
if isPrime(n):
cnt+=1
return cnt
import unittest
class TestPrime(unittest.TestCase):
def testPrime(self):
self.assertEqual(getNumberOfPrimes(100),25)