Esempio n. 1
0
    def test_findMid_m3_odd(self):
        #arrange
        sll = SLL()
        for i in range(5):
            sll.insert(i)

        #act - assert
        self.assertEqual(2, findMid_m3(sll.head))
Esempio n. 2
0
    def test_findMid_m2_even(self):
        #arrange
        sll = SLL()
        for i in range(6):
            sll.insert(i)

        #act - assert
        self.assertEqual(3, findMid_m2(sll.head))
Esempio n. 3
0
    def test_insert_at_begining_on_empty_list(self):
        #arrange
        sll = SLL()

        #act
        sll.insert_at_begining(1)

        #assert
        self.assertEqual(1, sll.head.data)
Esempio n. 4
0
    def test_insert_on_empty_list(self):
        #arrange
        sll = SLL()

        #act
        sll.insert(1)

        #assert
        self.assertEqual(sll.head.data, 1)
        self.assertEqual(sll.tail, sll.head)
Esempio n. 5
0
 def __init__(self, capacity=20):
     self.capacity = capacity
     self.dict = {}
     self.size = 0
     #initiate every key with an empty SLL that will later be used for collisions
     for x in range(1, capacity + 1):
         self.dict[x] = SLL()
Esempio n. 6
0
def incidence_matrix_to_list(incimatrix):
    """
    Convert from an incidence matrix to adjacency lists.
    :param incimatrix: Input incidence matrix[nodeindex][edgeindex]
    :param edgemap: dictionary{(edge):edgeindex}
    :return: adjacent linked list
    """
    nodenum = len(incimatrix)
    edgenum = len(incimatrix[0])
    # # switch map to index:tuple
    # reversemap = {}
    # for key, value in edgemap.items():
    #     reversemap[value] = key
    lst = [SLL.LinkedList() for _ in range(nodenum)]
    for edge_index in range(edgenum):
        temppair = []
        for node_index in range(nodenum):
            if incimatrix[node_index][edge_index] == 1:
                temppair.append(node_index)
        lst[temppair[0]].append(temppair[1])
        lst[temppair[1]].append(temppair[0])
    # for node_index in range(nodenum):
    #     for edge_index in range(edgenum):
    #         if incimatrix[node_index][edge_index] == 1:
    #             edge = reversemap[edge_index]
    #             node = edge[0] if node_index != edge[0] else edge[1]
    #             lst[node_index].append(node)
    return lst
Esempio n. 7
0
 def __init__(self, capacity=20):
     '''
     initiate the hash table with a capacity that is set to 20 if none is provided
     :param capacity: int
     '''
     self.capacity = capacity
     self.dict = {}
     self.size = 0
     #initiate every key with an empty SLL that will later be used for collisions
     for x in range(1,capacity+1):
         self.dict[x] = SLL()
Esempio n. 8
0
def matrix_to_list(matrix):
    """
    Convert from an adjacency matrix to adjacency lists
    :param matrix: Input adjacency matrix[][]
    :return: Output a list of linked list, each linked list store the neighbours
    """
    length = len(matrix)
    lst = [SLL.LinkedList() for _ in range(length)]
    for i in range(length):
        for j in range(length):
            if matrix[i][j] == 1:
                lst[i].append(j)
    return lst
Esempio n. 9
0
    def test_insert_at_begining_on_nonempty_list(self):
        #arrange
        sll = SLL()
        for i in range(3):
            sll.insert(i)

        #act
        sll.insert_at_begining(4)

        #assert
        self.assertEqual(4, sll.head.data)
Esempio n. 10
0
    def test_insert_on_nonempty_list(self):
        #arrange
        sll = SLL()
        for i in range(3):
            sll.insert(i)

        #act
        sll.insert(4)

        #asser
        self.assertEqual(4, sll.tail.data)