예제 #1
0
def get(node, depth, target):
    if depth == target:
        return List(node.data) if node is not None else None

    left = get(node.l, depth + 1, target)
    right = get(node.r, depth + 1, target)

    if left is not None:
        left.append(right)

    return left
예제 #2
0
def build_structure(data, structures, name):
    fields = {}
    for key, value in data.items():
        if isinstance(value, dict):
            field = build_structure(value, structures, name=key)
        elif isinstance(value, list):
            if len(value) > 0:
                field = List(get_structure(value[0], structures, name=key))
            else:
                print("List {} does not have values to indicate type".format(
                    key),
                      file=sys.stderr)
                continue
        else:
            field = type_to_cls(value)
        fields[key] = field
    struct = Struct(name, fields)
    structures.append(struct)
    return struct
예제 #3
0
"""
Write code to partition a linked list around a value x, such that all nodes less than x come
before all nodes greater than or equal to x. lf x is contained within the list, the values of x only need
to be after the elements less than x (see below). The partition element x can appear anywhere in the
"right partition"; it does not need to appear between the left and right partitions.

Input: 3 -> 5 -> 8 -> 5 - > 10 -> 2 -> 1 [partition = 5)
Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
"""
from structures import List

lst = List(3, 5, 8, 5, 10, 2, 1)
lst.partition(5)

assert lst == (3, 2, 1, 5, 8, 5, 10)
예제 #4
0
"""
from structures import List


def start_of_loop(ll):
    seen = []
    for n in ll:
        if id(n) in seen:
            return n
        else:
            seen.append(id(n))
    else:
        return None


lst = List(1, 2, 3, 4)
lst.append(lst[2], copy=False)

assert 3 == start_of_loop(lst)


def start_of_loop_v2(ll):
    s = ll[1]
    f = ll[2]

    while id(s) != id(f):
        s = s.next
        f = f.next.next

    s = ll[0]
    while id(s) != id(f):
예제 #5
0
"""
Node: Implement an algorithm to delete a node in the middle (i.e., any node but
the first and last node, not necessarily the exact middle) of a singly linked list, given only access to
that node.

Input: the node c from the linked list a - >b- >c - >d - >e- >f
Result: nothing is returned, but the new linked list looks like a - >b- >d - >e- >f
"""
from structures import List

lst = List(1, 2, 3, 4, 5)
n = lst[2]

lst.remove(n)

assert lst == (1, 2, 4, 5)
예제 #6
0
"""
You have two numbers represented by a linked list, where each node contains a single
digit. The digits are stored in reverse order, such that the 1 's digit is at the head of the list. Write a
function that adds the two numbers and returns the sum as a linked list.

Input: (7-) 1 -) 6) + (5 -) 9 -) 2).Thatis,617 + 295.
Output: 2 -) 1 -) 9. That is, 912.


Suppose the digits are stored in forward order. Repeat the above problem.

Input: (6 -) 1 -) 7) + (2 -) 9 -) 5).Thatis,617 + 295.
Output: 9 -) 1 -) 2. That is, 912.
"""
from structures import List

l1 = List(7, 1, 6)
l2 = List(5, 9, 2)

assert 912 == l1 + l2
예제 #7
0
"""
Given two (singly) linked lists, determine if the two lists intersect. Return the inter-
secting node. Note that the intersection is defined based on reference, not value. That is, if the kth

node of the first linked list is the exact same node (by reference) as the jth node of the second
linked list, then they are intersecting.
"""
from structures import List


# noinspection PyShadowingNames
def intersect(lst1, lst2):
    for n1 in lst1:
        for n2 in lst2:
            if n1 is n2:
                return n1

    return None


lst1 = List(1, 2, 3)

lst2 = List(1)
lst2.append(lst1[2], copy=False)

assert 3 == intersect(lst1, lst2)
예제 #8
0
        if m == n:
            return head

        i = 1
        prev = None
        curr = head
        while i != m:
            prev = curr
            curr = curr.next
            i += 1

        one_before = prev  # one before the start of the reverse
        new_end = curr  # the new end of the given interval

        nxt = curr.next
        while i != n:
            nxt.next, curr, nxt = curr, nxt, nxt.next
            i += 1

        new_end.next = nxt

        if one_before is not None:
            one_before.next = curr
            return head
        else:
            return curr


s = Solution()
assert s.reverseBetween(List(1, 2, 3, 4, 5).head, 3, 4) == [1, 2, 4, 3, 5]
예제 #9
0
"""
Implement an algorithm to find the kth to last element of a singly linked list.
"""
from structures import List

lst = List(1, 2, 3, 4, 5, 6, 7)

k = 5

el = None
for i, n in enumerate(lst):
    if i == (k - 1):
        el = lst.head
    elif i > (k - 1):
        el = el.next

assert el == 3
예제 #10
0
    if not palindrome:
        return False, None

    if counterpart is None:
        counterpart = node2

    if node1 != counterpart:
        return False, None

    return True, counterpart.next


def is_palindrome_v2(ll):
    palindrome, _ = recurse(ll.head, ll.head)
    return palindrome


ll = List(1, 2, 2, 1)
assert is_palindrome_v2(ll)
assert is_palindrome(ll)


lst = List(1, 2, 3, 4, 3, 2, 1)
assert is_palindrome(lst)
assert is_palindrome_v2(lst)


lst = List(1, 2, 5, 2, 3)
assert not is_palindrome(lst)
assert not is_palindrome_v2(lst)