repl.it
@big_deelan/

Hashing

Python

No description

fork
loading
Files
  • main.py
  • cities.txt
  • hashtable.txt
main.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
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
89
90
91
92
93
#Subroutine
def calcCityCode(cityname):
  citycode = 0
  for letter in cityname:
    citycode += ord(letter) #citycode = function
  citycode *= ord(letter)
  return citycode #This returns a value

#hashTable = parameter
#This subroutine creates the hashtable and writes the addresses to it
def writeHashtable(hashTable):
  filepath = "hashtable.txt"
  myFile = open(filepath, "w") # myFile = the pointer, w = opened in write mode, opening filepath which is looking in the file hashtable.txt
  for lines in hashTable:
    myFile.write(str(lines) + "\n")
  myFile.close()

def openread(hashTable):
  filepath = "cities.txt"
  linecounter = 0
  collisionCount = 0
  for line in open(filepath, 'r'): #filepath = argument
    linelist = line.split(",") 
    city = linelist[1][1:len(linelist[1])-1] #This strips away the quote marks from the search
    citycode = calcCityCode(city) 
    hashValue = citycode % 40000 # %, the modulo operator returns the remainder of calculated division
    if linecounter < 10:
      print(city, citycode, hashValue) 
    while hashTable[hashValue] != -1:
      collisionCount += 1
      citycode += 1
      hashValue = citycode % 40000
    hashTable[hashValue] = linecounter
    linecounter += 1
  print("Collisions =", collisionCount)
    
def openTen(hashTable):
  filepath = "cities.txt"
  linecounter = 0
  collisionCount = 0
  myFile = open(filepath, "r")
  for x in range(10):
    line = myFile.readline()
    linelist = line.split(",") 
    city = linelist[1][1:len(linelist[1])-1]
    citycode = calcCityCode(city)
    hashValue = citycode % 40000
    if linecounter < 10:
      print(city, citycode, hashValue)
    while hashTable[hashValue] != -1:
      collisionCount += 1
      citycode += 1
      hashValue = citycode % 40000
    hashTable[hashValue] = linecounter
    linecounter += 1
  print("Collisions =", collisionCount)
  myFile.close()

def findCity(hashTable):
  cityName = input("Name a city to find: ")
  citycode = calcCityCode(cityName)
  hashValue = citycode % 40000
  searchLine = hashTable[hashValue]
  #
  matched = False
  filepath = "cities.txt"
  endSearch = False
  linecounter = 0
  for line in open(filepath, 'r'): #Alternative method for file reading, in a for loop the file closes itself
    if linecounter >= searchLine and matched == False and endSearch == False:
      linelist = line.split(",") 
      city = linelist[1][1:len(linelist[1])-1]
      if city == cityName:
        matched = True
        print("")
        print(line)
      else:
        citycode += 1
        hashValue = citycode % 40000
        searchLine = hashTable[hashValue]
      if searchLine == "-1" :
        endSearch = True
    linecounter += 1

# Main program 
print("")
hashTable = [-1 for x in range(40000)]
openread(hashTable)
#openTen(hashTable)
print("")
print(hashTable[0:10], "\n")
writeHashtable(hashTable)
findCity(hashTable)
Fetching token
?