示例#1
0
文件: 4.2.py 项目: tzyl/ctci-python
class DirectedGraph(object):
    def __init__(self):
        self.V = LinkedList()

    def __iter__(self):
        return self.V.__iter__()

    def add(self, vertex):
        self.V.insert(vertex)
    def setUp(self):
        # Set up some Elements
        self.e1 = LinkedListElement(1)
        self.e2 = LinkedListElement(2)
        self.e3 = LinkedListElement(3)
        self.e4 = LinkedListElement(4)

        # Start setting up a LinkedList
        self.ll = LinkedList(self.e1)
        return 
    def testReturnKth(self):
        """ Test Successful return Kth """
        k = 2
        linkedlist = LinkedList()

        node1 = Node(1)
        node2 = Node(2)
        node3 = Node(3)
        node4 = Node(4)
        linkedlist.head = node1
        node1.addNode(node2)
        node2.addNode(node3)
        node3.addNode(node4)
        result = returnKth(k, linkedlist)
        self.assertEquals(node2, result)
示例#4
0
def all_paths_from(root, value):
    if root is None:
        return []

    left_paths = all_paths_from(root.left, value - root.value)
    for path in left_paths:
        path.insert(Node(root.key, root.value))
    right_paths = all_paths_from(root.right, value - root.value)
    for path in right_paths:
        path.insert(Node(root.key, root.value))

    if root.value == value:
        path = LinkedList()
        path.insert(Node(root.key, root.value))
        return left_paths + right_paths + [path]

    return left_paths + right_paths
 def test_remove(self):
     s = LinkedList()
     self.assertRaises(ListOutOfRangeError, s.remove, 0)
     s.insert('a', 0)
     s.insert('b', 1)
     s.insert('c', 2)
     self.assertEquals(s.remove(1), 'b')
     self.assertEquals(s.get(0), 'a')
     self.assertEquals(s.get(1), 'c')
     self.assertRaises(ListOutOfRangeError, s.remove, 2)
     self.assertEquals(s.remove(1), 'c')
     self.assertEquals(s.remove(0), 'a')
     self.assertRaises(ListOutOfRangeError, s.remove, 0)
示例#6
0
文件: 4.2.py 项目: tzyl/ctci-python
 def __init__(self):
     self.V = LinkedList()
class TestLinkedListMethods(unittest.TestCase):

    def setUp(self):
        # Set up some Elements
        self.e1 = LinkedListElement(1)
        self.e2 = LinkedListElement(2)
        self.e3 = LinkedListElement(3)
        self.e4 = LinkedListElement(4)

        # Start setting up a LinkedList
        self.ll = LinkedList(self.e1)
        return 

    def tearDown(self):
        #Clear For Next Test
        self.e1 = self.e2 = self.e3 = self.e4 = None
        self.ll = None

    #TEST FUNCTION
    def test_append(self):
        #Setup
        self.ll.append(self.e2)
        self.ll.append(self.e3)
        self.assertEqual(self.ll.head.next.value,2)
        self.assertEqual(self.ll.head.next.next.value,3)

    def test_getPosition(self):
        #Setup
        self.ll.append(self.e2)
        self.ll.append(self.e3)
        self.assertEqual(self.ll.get_position(3).value,3)

    def test_insert(self):
        #Setup
        self.ll.append(self.e2)
        self.ll.append(self.e3)
        
        # Test insert
        self.ll.insert(self.e4,3)
        # Should print 4 now
        self.assertEqual(self.ll.get_position(3).value,4)

    def test_delete(self):
        #Setup
        self.ll.append(self.e2)
        self.ll.append(self.e4)
        self.ll.append(self.e3)
        
        # Test delete
        self.ll.delete(1)
        
        self.assertEqual(self.ll.get_position(1).value,2)
        self.assertEqual(self.ll.get_position(2).value,4)
        self.assertEqual(self.ll.get_position(3).value,3)
示例#8
0
    while (i < position and cur is not None):
        i += 1
        cur = cur.next

    return cur.value


e1 = LinkedListElement(1)
e2 = LinkedListElement(2)
e3 = LinkedListElement(3)
e4 = LinkedListElement(4)
e5 = LinkedListElement(5)
e6 = LinkedListElement(6)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)
ll.append(e6)

print("Test K To Last")
print(getLength(e1))
print(kToLast(ll, 0))
print(kToLast(ll, 1))
print(kToLast(ll, 2))
print(kToLast(ll, 3))
print(kToLast(ll, 4))
print(kToLast(ll, 5))
示例#9
0
文件: 4.4.py 项目: tzyl/ctci-python
def get_each_depth(binary_tree):
    linked_lists = []
    current = LinkedList()
    current.insert(binary_tree.root)
    while not current.empty():
        linked_lists.append(current)
        children = LinkedList()
        for node in current:
            if node.left is not None:
                children.insert(node.left)
            if node.right is not None:
                children.insert(node.right)
        current = children
    return linked_lists
 def test_index(self):
     s = LinkedList()
     s.insert('a', 0)
     self.assertEquals(s.index('a'), 0)
     self.assertEquals(s.index('b'), None)
     s.insert('b', 1)
     self.assertEquals(s.index('a'), 0)
     self.assertEquals(s.index('b'), 1)
     s.insert('c', 1)
     self.assertEquals(s.index('a'), 0)
     self.assertEquals(s.index('b'), 2)
     self.assertEquals(s.index('c'), 1)
     s.insert('d', 0)
     self.assertEquals(s.index('d'), 0)
     self.assertEquals(s.index('a'), 1)
     self.assertEquals(s.index('b'), 3)
     self.assertEquals(s.index('c'), 2)
 def test_get(self):
     s = LinkedList()
     s.insert('a', 0)
     s.insert('b', 0)
     s.insert('c', 0)
     self.assertEquals(s.get(0), 'c')
     self.assertEquals(s.get(1), 'b')
     self.assertEquals(s.get(2), 'a')
     self.assertRaises(ListOutOfRangeError, s.get, 4)
     s.insert('d', 1)
     self.assertEquals(s.get(0), 'c')
     self.assertEquals(s.get(1), 'd')
     self.assertEquals(s.get(2), 'b')
示例#12
0
import sys
sys.path.insert(
    1, "C:\Users\Tim\Desktop\competition\clrs\datastructures_old_py2")
from datastructures.linkedlist import LinkedList, Node


def delete(x):
    """Deletes node x from singly linked list given only access to x.
    Note: Will not work if x is the last node in list. Maybe mark the node
    as dummy to deal with this case. This method still has side effects if
    there are external pointers to nodes as this does not actually delete the
    node but tries to mimic it.
    """
    if x is None or x.next is None:
        return False
    x.key, x.next = x.next.key, x.next.next
    return True


linked_list = LinkedList()
for i in xrange(10):
    linked_list.insert(Node(i))
linked_list.traverse()
print delete(linked_list.search(5))
linked_list.traverse()
示例#13
0
文件: 2.2.py 项目: tzyl/ctci-python
    for _ in xrange(k):
        if y is None:
            print "Not enough elements to get %sth from last!" % k
        y = y.next
    while y is not None:
        x = x.next
        y = y.next
    return x


def get_from_end_recursive(linked_list, k):
    def _get_from_end_recursive(current, k):
        if current is None:
            return None, 0
        node, i = _get_from_end_recursive(current.next, k)
        i += 1
        if i == k:
            return current, i
            print current.key
        return node, i

    return _get_from_end_recursive(linked_list.head, k)[0]


linked_list = LinkedList()
for i in xrange(10):
    linked_list.insert(Node(i))
for i in xrange(1, 11):
    print get_from_end(linked_list, i).key
    print get_from_end_recursive(linked_list, i).key
示例#14
0
def sumLists(firstList, secondList):
    return LinkedList(sumListsHelper(firstList.head, secondList.head, 0))
示例#15
0
文件: 4.2.py 项目: tzyl/ctci-python
 def __init__(self, key):
     self.key = key
     self.neighbours = LinkedList()
     self.discovered = False
 def test_empty(self):
     s = LinkedList()
     self.assertEquals(s.size(), 0)
     self.assertRaises(ListOutOfRangeError, s.remove, 1)
     self.assertRaises(ListOutOfRangeError, s.get, 0)
示例#17
0
import sys
sys.path.insert(1, "C:\Users\Tim\Desktop\competition\clrs\datastructures_old_py2")
from datastructures.linkedlist import LinkedList, Node


def partition(linked_list, x):
    """Partitions a linked list around a value x so that all nodes less than x
    come before all nodes greater than or equal to x.
    """
    current = linked_list.head
    while current is not None:
        next_ = current.next
        if current.key < x:
            linked_list.delete(current)
            linked_list.insert(current)
        current = next_

linked_list = LinkedList()
for i in xrange(10):
    linked_list.insert(Node(i))
linked_list.traverse()
partition(linked_list, 5)
linked_list.traverse()
 def test_size(self):
     s = LinkedList()
     s.insert('a', 0)
     s.insert('b', 0)
     self.assertEquals(s.size(), 2)
     s.insert('c', 2)
     self.assertEquals(s.size(), 3)
     s.remove(0)
     self.assertEquals(s.size(), 2)
     s.remove(0)
     self.assertEquals(s.size(), 1)
     s.remove(0)
     self.assertEquals(s.size(), 0)
示例#19
0
文件: 2.6.py 项目: tzyl/ctci-python
                slow = slow.next
            return head
    # No loop.
    return None


def find_loop_start2(linked_list):
    """Given a corrupt circular linked list returns the start of the loop.
    Probably not allowed to use Python set.
    """
    seen = set()
    current = linked_list.head
    while current is not None:
        if current in seen:
            return current
        seen.add(current)
        current = current.next
    return None


linked_list = LinkedList()
for i in xrange(10):
    linked_list.insert(Node(i))
# Set up a loop.
x = linked_list.search(0)
y = linked_list.search(5)
x.next = y
y.prev = x

print find_loop_start(linked_list).key
示例#20
0
文件: 2.7.py 项目: tzyl/ctci-python
def size(linked_list):
    size = 0
    x = linked_list.head
    while x is not None:
        size += 1
        x = x.next
    return size


def check_palindrome2(linked_list):
    """Relies on doubly linked list."""
    x = linked_list.head
    while x.next is not None:
        x = x.next
    y = linked_list.head
    while y is not None:
        if x.key != y.key:
            return False
        x = x.prev
        y = y.next
    return True


a = LinkedList()
for i in xrange(9):
    a.insert(Node(i))
for i in reversed(xrange(9)):
    a.insert(Node(i))
a.traverse()
print check_palindrome_recursive(a)