def huffman_encode(inf, outf): hb_writer = HuffmanBitsWriter(outf) c = count(inf) x = build_leaf(c, comes_before) if linked_list.length(x) == 0: hb_writer.write_byte(0) hb_writer.close() return '' lst = build_nodes(x) new = chr_code(lst, c) y = build_leaf(c, comes_before_characters) lgth = linked_list.length(y) hb_writer.write_byte(lgth) for i in range(lgth): hb_writer.write_byte(ord(y.first.character)) hb_writer.write_int(y.first.occurrence) y = y.rest if type(lst) == Node: inFile = open(inf, 'r') s = '' for line in inFile: for char in line: s += str(array_list.get(new, ord(char))) hb_writer.write_code(s) inFile.close() hb_writer.close() return lst.character
def equalize_lengths(l, l2): l_length = length(l) l2_length = length(l2) if l_length > l2_length: l2 = pad_to_equal_length(l_length, l2) elif l2_length > l_length: l = pad_to_equal_length(l2_length, l) return [l, l2]
def get(ht, key): index = hash(key) % ht.size if ht.table[index] == None: raise LookupError if linked_list.length(ht.table[index]) > 1: for item_index in range(linked_list.length(ht.table[index])): itemref = linked_list.get(ht.table[index], item_index) if itemref[0] == key: return itemref[1] raise LookupError return ht.table[index].first[1]
def build_huffman(OccuranceList): if OccuranceList == array_list.empty_list(): return None huffman_linked_list = linked_list.empty_list() for index, occurance in enumerate(OccuranceList.array): if occurance != 0: temp_leaf = Leaf( chr(index), occurance) #figure out how to go from into to asciib huffman_linked_list = linked_list.insert_sorted( huffman_linked_list, temp_leaf, comes_before) while linked_list.length(huffman_linked_list) != 1: temp_leaf_1 = linked_list.get(huffman_linked_list, 0) temp_leaf_2 = linked_list.get(huffman_linked_list, 1) huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0) huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0) total_freq = temp_leaf_1.freq + temp_leaf_2.freq if comes_before(temp_leaf_1, temp_leaf_2): if ord(temp_leaf_1.char) < ord(temp_leaf_2.char): temp_node = Node(temp_leaf_1.char, total_freq, temp_leaf_1, temp_leaf_2) else: temp_node = Node(temp_leaf_2.char, total_freq, temp_leaf_1, temp_leaf_2) huffman_linked_list = linked_list.insert_sorted( huffman_linked_list, temp_node, comes_before) return huffman_linked_list.first
def remove(ht, key): index = hash(key) % ht.size if ht.table[index] == None: raise LookupError if linked_list.length(ht.table[index]) > 1: for item_index in range(linked_list.length(ht.table[index])): itemref = linked_list.get(ht.table[index], item_index) if itemref[0] == key: ht.table[index] = linked_list.remove(ht.table[index], item_index)[1] ht.items -= 1 return ht raise LookupError ht.table[index] = None ht.items -= 1 return ht
def insert(ht, key, item): index = hash(key) % ht.size if ht.table[index] != None: if insert_helper(ht.table[index], key, item): pass else: ht.collisions += 1 ht.items += 1 ht.table[index] = linked_list.add( ht.table[index], linked_list.length(ht.table[index]), (key, item)) else: ht.table[index] = linked_list.add(ht.table[index], linked_list.length(ht.table[index]), (key, item)) ht.items += 1 if ht.items / ht.size > 1.5: new_table = HashTable(ht.size * 2, ht.items, [None] * (ht.size * 2), 0) for item in ht.table: if item != None: if linked_list.length(item) > 1: for item_index in range(linked_list.length(item)): new_index = hash(linked_list.get( item, item_index)[0]) % new_table.size if new_table.table[new_index] != None: new_table.collisions += 1 new_table.table[new_index] = linked_list.add( new_table.table[new_index], linked_list.length(new_table.table[new_index]), linked_list.get(item, item_index)) else: new_table.table[new_index] = linked_list.Pair( linked_list.get(item, item_index), None) else: new_index = hash(item.first[0]) % new_table.size # if new_table.table[new_index] != None: # if insert_helper(new_table.table[new_index], item.first[0], item.first[1]): # pass # else: # collisions += 1 # new_table.table[new_index] = linked_list.Pair(item.first, None) # else: new_table.table[new_index] = linked_list.Pair( item.first, None) ht = new_table return ht
def build_Hufftree(alist): llist = convert_llist(alist) while (linked_list.length(llist) > 1): node1,llist=linked_list.remove(llist, 0) node2,llist=linked_list.remove(llist, 0) n=Node(0,(node1.freq+node2.freq),node1,node2) n.char=(node1.char<node2.char) and node1.char or node2.char llist=linked_list.insert_sorted(llist, n, comes_before) if(llist==None): return None else: return linked_list.get(llist, 0)
def join_leaves(sorted_list): #whilethere is more than one node in the list while linked_list.length(sorted_list) > 1: #remove two nodes from beginning of list (lowest frequencies) (leaf1, sorted_list) = linked_list.remove(sorted_list, 0) (leaf2, sorted_list) = linked_list.remove(sorted_list, 0) #create new Node with these two leaves as children, lesser of two on left(leaf1) node_frequency = leaf1.frequency + leaf2.frequency if leaf1.character < leaf2.character: character = leaf1.character else: character = leaf2.character new_node = Node(node_frequency, character, leaf1, leaf2) sorted_list = linked_list.insert_sorted(sorted_list, new_node, comes_before) return sorted_list
def build_tree(list): sorted = build_sorted_leaves(list) # TODO: re-nest build_sorted_leaves? while ll.length(sorted) > 1: # TODO: inefficient tup = ll.remove(sorted, 0) first = tup[0] sorted = tup[1] tup = ll.remove(sorted, 0) second = tup[0] sorted = tup[1] if first.char < second.char: char = first.char else: char = second.char node = Node(char, first.freq + second.freq, first, second) sorted = ll.insert_sorted(sorted, node, comes_before) return node
def build_tree(list, func): sorted_list1 = sorted_leaf_list(list) sorted_list = array_to_linked(sorted_list1) while linked_list.length(sorted_list) > 1: leaf_node, sorted_list = linked_list.remove(sorted_list, 0) leaf_node1, sorted_list = linked_list.remove(sorted_list, 0) if int(leaf_node.val) < int(leaf_node1.val): x = Node(leaf_node.val, leaf_node.freq + leaf_node1.freq, leaf_node, leaf_node1) else: x = Node(leaf_node1.val, leaf_node.freq + leaf_node1.freq, leaf_node, leaf_node1) sorted_list = linked_list.insert_sorted(sorted_list, x, func) ptree = linked_list.get(sorted_list, 0) return ptree
def test_lenght_grows(self): l = ListNode('1') self.assertEqual(length(l), 1) l = shift_list(ListNode('0'), l) self.assertEqual(length(l), 2, 'Length should grow by 1')
def test_resulting_list_has_desired_length(self): l = ListNode('1') l = pad_to_equal_length(5, l) self.assertEqual(length(l), 5)