Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
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
Exemplo n.º 10
0
 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)
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
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
Exemplo n.º 13
0
 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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
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()