def test_hash_table_generic(): hashtable = Hashtable(1024) # testing for single input hashtable.add('ahmad', 25) hashtable.add('silent', True) assert hashtable.contains('ahmad') is True assert hashtable.contains('ahmadd') is False # testing for multiple inputs for a single bucket hashtable.add('hamad', 29) hashtable.add('listen', 'Hey man') assert hashtable.contains('ahmad') is True assert hashtable.contains('hamad') is True assert hashtable.contains('listen') is True assert hashtable.contains('silent') is True # check for duplicate input assert hashtable.add('ahmad', 35) == 'key already exists' # testing the values assert hashtable.get('ahmad') == 25 assert hashtable.get('hamad') == 29 assert hashtable.get('dada') == None
def printSummary(theTable): """ printSummary prints a summary of information about the hash table contents. printSummary : HashTable -> NoneType """ # Display the entire table! print("Unique words:", theTable.size) # Find the most common word in the text file. total = 0 maxWord = "" maxCount = 0 for key in keys(theTable): thisCount = get(theTable, key) total += thisCount if thisCount > maxCount: maxCount = thisCount maxWord = key print("There are " + str(len(keys(theTable))) + " words.") print("Total words:", total) print('"' + maxWord + "\" appeared ", str(maxCount), " times, more than any other word.") print('Imbalance =', imbalance(theTable)) print('Load =', load(theTable)) print('Size =', len(theTable.table))
def printTable( hTable ): """ Print the contents of the given hash table. Each key/value pair is displayed in parentheses, tuple-style. All pairs appear on a single line. printTable : HashTable -> NoneType """ print( "Word Count Data ---------------" ) lcount = 0 ltext = 0 for key in keys( hTable ): # print( "(" + key + "," + str( get( hTable, key ) ) + ")", end=" " ) txt = "(" + key + "," + str( get( hTable, key ) ) + ")" ltext += len( txt ) if ltext > 51: print( txt ) ltext = 0 else: print( txt, end=" " ) print()
def printTable(hTable): """ Print the contents of the given hash table. Each key/value pair is displayed in parentheses, tuple-style. All pairs appear on a single line. printTable : HashTable -> NoneType """ print("Word Count Data ---------------") lcount = 0 ltext = 0 for key in keys(hTable): # print( "(" + key + "," + str( get( hTable, key ) ) + ")", end=" " ) txt = "(" + key + "," + str(get(hTable, key)) + ")" ltext += len(txt) if ltext > 51: print(txt) ltext = 0 else: print(txt, end=" ") print()
def main(): #capacity = int( input( "Enter capacity (-1 for default): " ) ) capacity = 10 if capacity < 0: hTable = HashTable() else: hTable = HashTable(capacity) #filename = input( "Enter filename: " ) filename = 'atotc.txt' wordTable = word_count(hTable, filename) printSummary(wordTable) while True: print("Commands: k[ey] <word> f[ind] <word> q[uit] ? ", end=" ") response = input(":- ") # the displayed prompt query = response.split() if len(response) == 0 or not response[0] in "fkq": print(response + " invalid. Please enter a command and a word.") response = "" continue if query[0] == "k": print( "( " + query[1] + " in text ) is " \ + str( contains( wordTable, query[1] ) ) + "." ) if query[0] == "f": if contains(wordTable, query[1]): print( query[1] + " appears " \ + str( get( wordTable, query[1] ) ) + " times." ) else: print(query[1] + " in not in the text.") if query[0] == "q": break # answer = input("Do you want to see the entire table?(y/n) ") if answer != "y": return printTable(wordTable)
def word_count( hTable, filename ): """ Record the frequency of all words in the named file in the hashtable. word_count : HashTable String -> HashTable """ # Read the words of the text file into the word count table. fd = open( filename ) for line in fd: for word in line.split(): # using a regular expression argument to strip(), # strip out punctuation and convert token to lower-case. word = word.strip(",.\"\';:-!?").lower() if contains( hTable, word ): count = get( hTable, word ) put( hTable, word, count + 1 ) else: put( hTable, word, 1 ) fd.close() # closing the file is a 'good practice'. return hTable
def word_count(hTable, filename): """ Record the frequency of all words in the named file in the hashtable. word_count : HashTable String -> HashTable """ # Read the words of the text file into the word count table. fd = open(filename) for line in fd: for word in line.split(): # using a regular expression argument to strip(), # strip out punctuation and convert token to lower-case. word = word.strip(",.\"\';:-!?").lower() if contains(hTable, word): count = get(hTable, word) put(hTable, word, count + 1) else: put(hTable, word, 1) fd.close() # closing the file is a 'good practice'. return hTable
def main(): capacity = int( input( "Enter capacity (-1 for default): " ) ) if capacity < 0: hTable = HashTable() else: hTable = HashTable( capacity ) filename = input( "Enter filename: " ) wordTable = word_count( hTable, filename ) printSummary( wordTable ) print( "Average chain length: " + str( imbalance( wordTable ) ) ) while True: print( "Commands: k[ey] <word> f[ind] <word> q[uit] ? ", end=" " ) response = input( ":- " ) # the displayed prompt query = response.split() if len( response ) == 0 or not response[0] in "fkq": print( response + " invalid. Please enter a command and a word." ) response = "" continue if query[0] == "k": print( "( " + query[1] + " in text ) is " \ + str( contains( wordTable, query[1] ) ) + "." ) if query[0] == "f": if contains( wordTable, query[1] ): print( query[1] + " appears " \ + str( get( wordTable, query[1] ) ) + " times." ) else: print( query[1] + " in not in the text." ) if query[0] == "q": break # answer = input( "Do you want to see the entire table?(y/n) " ) if answer != "y": return printTable( wordTable )
def printSummary( theTable ): """ printSummary prints a summary of information about the hash table contents. printSummary : HashTable -> NoneType """ # Display the entire table! print( "Unique words:", theTable.size ) # Find the most common word in the text file. total = 0 maxWord = "" maxCount = 0 for key in keys( theTable ): thisCount = get( theTable, key ) total += thisCount if thisCount > maxCount: maxCount = thisCount maxWord = key print( "There are " + str( len( keys( theTable ) ) ) + " words." ) print( "Total words:", total ) print( '"' + maxWord + "\" appeared ", str( maxCount ), " times, more than any other word." )
size = int(input('Enter the # of elements to hold in hashtable: ')) hashtable = hashtable.HashTable(size) print_help() print('') while action is not QUIT.key: action = input('Enter a command: ') try: if action is SET.key: key = input('key: ') value = input('value: ') hashtable.set(key, value) elif action is GET.key: key = input('key: ') retrieved = hashtable.get(key) if retrieved is None: print('Did not find key with value ' + key) else: print('Retrieved value: ' + retrieved) elif action is DELETE.key: key = input('key: ') deleted = hashtable.delete(key) if deleted is None: print('Did not find key with value ' + key) else: print('Deleted value: ' + deleted) elif action is LOAD.key: print(hashtable.load()) elif action is PRINT.key: hashtable.printBins()
import psutil from hashtable import make, assign, iterate, get import random p = psutil.Process() st = p.memory_info().rss D = make() for i in range(100): k = random.randint(1, 1000000) v = i assign(D, k, v) k0, v0 = next(iter(iterate(D))) print(get(D, k0)) print("{} -> {}".format(st, p.memory_info().rss))