15.08.2013 Views

code for the example in the Note on Hashing

code for the example in the Note on Hashing

code for the example in the Note on Hashing

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simple Hash<str<strong>on</strong>g>in</str<strong>on</strong>g>g Algorithm<br />

snIn = open ("names.txt","r") # Opens a path to <str<strong>on</strong>g>the</str<strong>on</strong>g> data file<br />

collisi<strong>on</strong> = 0 # Stores <str<strong>on</strong>g>the</str<strong>on</strong>g> number of collisi<strong>on</strong>s<br />

listOfStudents = [] # List to store data<br />

# Secti<strong>on</strong> to load an "empty" list with bogus data<br />

<str<strong>on</strong>g>for</str<strong>on</strong>g> i <str<strong>on</strong>g>in</str<strong>on</strong>g> range (0,10):<br />

listOfStudents.append ("zzz")<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t (listOfStudents) # Inserted <str<strong>on</strong>g>for</str<strong>on</strong>g> test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t (len (listOfStudents)) # Inserted <str<strong>on</strong>g>for</str<strong>on</strong>g> test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

# Secti<strong>on</strong> to store <str<strong>on</strong>g>the</str<strong>on</strong>g> names <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> list<br />

while True :<br />

name = str.strip (snIn.readl<str<strong>on</strong>g>in</str<strong>on</strong>g>e ()) # Inputs <str<strong>on</strong>g>the</str<strong>on</strong>g> last name of <str<strong>on</strong>g>the</str<strong>on</strong>g> student from <str<strong>on</strong>g>the</str<strong>on</strong>g> file<br />

if name == "" : # This will exit <str<strong>on</strong>g>the</str<strong>on</strong>g> loop if last name is a null str<str<strong>on</strong>g>in</str<strong>on</strong>g>g (end of file)<br />

break<br />

elementInList = ord (name [0] ) % 10 # Simple hash<str<strong>on</strong>g>in</str<strong>on</strong>g>g algorithm to calculate where <str<strong>on</strong>g>the</str<strong>on</strong>g> name should go<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t ("Where it should go:", elementInList) # For test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

while True: # Will loop until <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>in</str<strong>on</strong>g>putted name is <str<strong>on</strong>g>in</str<strong>on</strong>g>serted <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> list<br />

if listOfStudents[elementInList] == "zzz": # Inserts name if <str<strong>on</strong>g>the</str<strong>on</strong>g> element is "blank"<br />

listOfStudents[elementInList] = name<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t ("where it went:", elementInList) # For test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t ()<br />

break<br />

else : # If <str<strong>on</strong>g>the</str<strong>on</strong>g> element checked above c<strong>on</strong>ta<str<strong>on</strong>g>in</str<strong>on</strong>g>s a name, it will <str<strong>on</strong>g>in</str<strong>on</strong>g>crease <str<strong>on</strong>g>the</str<strong>on</strong>g> element to # be checked by 1<br />

collisi<strong>on</strong> +=1<br />

elementInList += 1<br />

if elementInList == len (listOfStudents) : # If <str<strong>on</strong>g>the</str<strong>on</strong>g> end of <str<strong>on</strong>g>the</str<strong>on</strong>g> list is reached, it will start <str<strong>on</strong>g>the</str<strong>on</strong>g> search<br />

# at <str<strong>on</strong>g>the</str<strong>on</strong>g> beg<str<strong>on</strong>g>in</str<strong>on</strong>g>n<str<strong>on</strong>g>in</str<strong>on</strong>g>g of <str<strong>on</strong>g>the</str<strong>on</strong>g> list<br />

elementInList = 0<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t ("Collisi<strong>on</strong>s:", collisi<strong>on</strong>) # Inserted <str<strong>on</strong>g>for</str<strong>on</strong>g> test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t (listOfStudents) # Inserted <str<strong>on</strong>g>for</str<strong>on</strong>g> test<str<strong>on</strong>g>in</str<strong>on</strong>g>g purposes - this l<str<strong>on</strong>g>in</str<strong>on</strong>g>e will be removed<br />

# Secti<strong>on</strong> to f<str<strong>on</strong>g>in</str<strong>on</strong>g>d <str<strong>on</strong>g>the</str<strong>on</strong>g> names <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> list<br />

while True:<br />

nameToF<str<strong>on</strong>g>in</str<strong>on</strong>g>d = <str<strong>on</strong>g>in</str<strong>on</strong>g>put ("What name do you wish to f<str<strong>on</strong>g>in</str<strong>on</strong>g>d: ") # Name user wishes to f<str<strong>on</strong>g>in</str<strong>on</strong>g>d<br />

elementInList = ord (nameToF<str<strong>on</strong>g>in</str<strong>on</strong>g>d[0] ) % 10 # Hash<str<strong>on</strong>g>in</str<strong>on</strong>g>g algorithm to calculate where <str<strong>on</strong>g>the</str<strong>on</strong>g> name should be<br />

while True:<br />

if listOfStudents[elementInList] == nameToF<str<strong>on</strong>g>in</str<strong>on</strong>g>d: # Checks if <str<strong>on</strong>g>the</str<strong>on</strong>g> name is <str<strong>on</strong>g>in</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> specified element<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t (name, "is <str<strong>on</strong>g>in</str<strong>on</strong>g> positi<strong>on</strong>", elementInList)<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t ()<br />

break<br />

elif listOfStudents [elementInList] == "zzz": # Outputs a message if <str<strong>on</strong>g>the</str<strong>on</strong>g> name is not found.<br />

pr<str<strong>on</strong>g>in</str<strong>on</strong>g>t (name, "is not found") # This is assumed if <str<strong>on</strong>g>the</str<strong>on</strong>g> element c<strong>on</strong>ta<str<strong>on</strong>g>in</str<strong>on</strong>g>s "blank" data<br />

break


else: # If <str<strong>on</strong>g>the</str<strong>on</strong>g> element checked above c<strong>on</strong>ta<str<strong>on</strong>g>in</str<strong>on</strong>g>s a name, it will <str<strong>on</strong>g>in</str<strong>on</strong>g>crease <str<strong>on</strong>g>the</str<strong>on</strong>g> element<br />

# to be checked by 1<br />

collisi<strong>on</strong>+=1<br />

elementInList += 1<br />

if elementInList == len (listOfStudents) : # If <str<strong>on</strong>g>the</str<strong>on</strong>g> end of <str<strong>on</strong>g>the</str<strong>on</strong>g> list is reached, it will start <str<strong>on</strong>g>the</str<strong>on</strong>g> search<br />

# at <str<strong>on</strong>g>the</str<strong>on</strong>g> beg<str<strong>on</strong>g>in</str<strong>on</strong>g>n<str<strong>on</strong>g>in</str<strong>on</strong>g>g of <str<strong>on</strong>g>the</str<strong>on</strong>g> list<br />

elementInList = 0<br />

aga<str<strong>on</strong>g>in</str<strong>on</strong>g> = <str<strong>on</strong>g>in</str<strong>on</strong>g>put ("Ano<str<strong>on</strong>g>the</str<strong>on</strong>g>r? Y or N ")<br />

if aga<str<strong>on</strong>g>in</str<strong>on</strong>g> == "N":<br />

break

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!