def test_get(self): lst = ArrayList() lst.num_items = 3 lst.arr = [1, 2, 3] self.assertEqual(get(lst, 2), 3) self.assertEqual(get(lst, 1,), 2) self.assertRaises(IndexError, get, lst, 3)
def check_size(cnt, n): sum = 0 for i in range(n.capacity): if array_list.get(cnt, i) != None: x = len(array_list.get(n, i)) * (array_list.get(cnt, i)) sum += x return sum
def build_leaf(x, func): #convert array into linked list new = linked_list.empty_list() for index in range(x.capacity): if (array_list.get(x, index)) != None: leaf_node = Leaf(chr(index), array_list.get(x, index)) new = linked_list.insert_sorted(new, leaf_node, func) return new
def header_writer(bitwriter,alist): count=0 for i in range(array_list.length(alist)-1): if(array_list.get(alist,i) != 0): count+=1 bitwriter.write_byte(count) for i in range(array_list.length(alist)-1): if(array_list.get(alist,i) != 0): bitwriter.write_byte(i) bitwriter.write_int(array_list.get(alist,i))
def chr_code(nd, x): new = array_list.empty_list() if type(nd) == Leaf: for i in range(x.capacity): if array_list.get(x, i) != None: new = array_list.set(new, i, '0') return new else: for i in range(x.capacity): if array_list.get(x, i) != None: new = array_list.set(new, i, indv_code(nd, chr(i))) return new
def convert_llist(alist): llist=None for i in range(array_list.length(alist)-1): g=array_list.get(alist, i) if(g != 0): llist=linked_list.insert_sorted(llist,Leaf(i,g),comes_before) return llist
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 peek(self): """returns the value at the top of the Stack. Args: self (StackArray): The Stack Returns: (int): Value at top of stack """ if self.num_items == 0: raise IndexError('') return array_list.get(self.arr_list, self.num_items - 1)
def count_occurances(path): file=open_file(path) if(file==None): return None else: huffcount=array_list.List([0]*256,256,256) for line in file: for ch in line: val=ord(ch) huffcount=array_list.set(huffcount, val , array_list.get(huffcount,val)+1) file.close() return huffcount
def peek(self): """method that tells us what the item at the top of the stack is Args: No args Returns: item (any type) : the item (data) at the top of the stack, without removing it Raises: IndexError : when the stack is empty """ if self.is_empty(): raise IndexError return alist.get(self.arr_list, self.num_items - 1)
def huffman_decode(infile,outfile): bitreader=HuffmanBitsReader(infile) #Creates the HuffmanBitsReader object charcount=header_reader(bitreader) #Creates an ArrayList of chracter counts htree=build_Hufftree(charcount) #Creates a Huffman Tree out of the character count list count=0 for i in range(256): count+=array_list.get(charcount,i) file=open(outfile,'w') #Creates the File we are writing to while(count>0): file.write(read_bin(htree, bitreader)) count-=1 bitreader.close() file.close()
def count(inf): inFile = open(inf, 'r') if inFile.readline() == '': inFile.close() return array_list.empty_list() else: inFile.close() inFile = open(inf, 'r') new = array_list.empty_list() for temp in inFile: for char in range(len(temp)): index = ord(temp[char]) val = array_list.get(new, index) if val == None: new = array_list.set(new, index, 1) else: new = array_list.set(new, index, val + 1) inFile.close() return new
def test_array_list3(self): lst = ArrayList() for i in range(3): lst = array_list.insert(lst, 0, i) pos = array_list.size(lst) - 1 self.assertEqual(array_list.get(lst, pos), 0)
def peek(self): """Write signature and purpose """ if not self.size(): raise IndexError return array_list.get(self.arr_list, self.num_items - 1)
def file_encoder(bitwriter, infile, alist): file=open_file(infile) for line in file: for ch in line: bitwriter.write_code(array_list.get(alist,ord(ch))) file.close()