コード例 #1
0
ファイル: p2_6.py プロジェクト: vatsalajha/CTCI_python
    if fst_len > 0 and fst_len % 2:
        stack.pop()

    print(f"Stack is {stack}, slp is {slp}")

    while slp:
        if slp.val != stack.pop():
            return False
        slp = slp.next

    return not stack


if __name__ == "__main__":

    exs = [
        [],
        [4],
        [1, 2, 2],
        [1, 2, 3, 4],
        [1, 1, 2, 1],
        [1, 1, 1],
        [1, 1, 1, 1],
        [1, 2, 0, 2, 1],
    ]

    for ex in exs:
        h_ex = llol(ex) if ex else None
        print(f"Result for {ex} is {check_ll_palindrome(h_ex)}")
        print("-" * 50)
コード例 #2
0
    return longer_ll.val


def adjust(head: Node, jumps):
    h = head
    for _ in range(jumps):
        h = h.next
    return h


def get_last_of_ll(head_in: Node):
    h = head_in
    while h.next:
        h = h.next
    return h


if __name__ == "__main__":
    exs = [([1, 2, 3, 4, 5, 6], [10, 20, 30], 4),
           ([1, 2, 3, 4, 5], [11, 22, 33, 44], 4),
           ([1, 2], [32, 43, 54, 65, 76], 1)]
    for a1, a2, con_point in exs:
        ll1 = llol(a1)
        ll2 = llol(a2)

        last_of_2 = get_last_of_ll(ll2)
        last_of_2.next = adjust(ll1, con_point)

        print(f"For lists {a1} and {a2} joined at {con_point}, \
             intersect node is : {get_intersecting_node(ll1,ll2)}")
コード例 #3
0
                high_head = elem_now
        else:
            if not low_head:
                low_head = elem_now
                low_root = elem_now
            else:
                low_head.next = elem_now
                low_head = elem_now
        elem_now = elem_next

    if low_root:
        low_head.next = high_root  # Connect the parts
        high_head.next = None
        return low_root
    else:
        return high_root  # No low elems


if __name__ == "__main__":
    exs = [([1, 2, 3, 4, 5, 6, 7], 4),
           ([10, 4, 7, 3, 1, 200, 40, 50, 3], 4),
           ([30, 40, 50], 30),
           ([1, 2, 5, 3, 2, 200], 3)
           ]

    for arr, x in exs:
        head = llol(arr)
        print(
            f"For\t{head} around partition {x},\ngot\t{part_around(x, head)}")
        print("-"*50)
コード例 #4
0
ファイル: p2_5.py プロジェクト: rcmangnale/CTCI_python
def sum_ll_rec(tl1, tl2):
    if tl1 is None and tl2 is None:
        return None, 0
    rest, carry = sum_ll_rec(tl1.next, tl2.next)

    cval = tl1.val + tl2.val + carry
    head = Node(cval % 10)

    head.next = rest
    return head, cval//10


if __name__ == "__main__":
    exs = [([1, 2, 3, 4, 5], [1, 2, 3]),
           ([9, 9, 9, 9], [2, 1]),
           ([9], [1])]

    for l1, l2 in exs:
        print(f"Backward result of sum of\t {l1} and {l2} is {sum_ll(llol(l1), llol(l2))}"
              f" same as {sum_ll(llol(l2), llol(l1))}")

        ll1r = llol(list(reversed(l1)))
        ll2r = llol(list(reversed(l2)))

        print(f"Forward result of sum of\t {l1} and {l2} is {sum_ll_reverse(llol(l1), llol(l2))}"
              f" same as {sum_ll_reverse(llol(l2), llol(l1))}")
        print("-"*50)


コード例 #5
0
ファイル: p2_1.py プロジェクト: vatsalajha/CTCI_python

def remove_dups_nobuf(h: Node):
    rc = h
    while rc:
        target = rc.val
        prev = rc
        restp = rc.next
        while restp:
            if restp.val == target:
                prev.next = restp.next
            else:
                prev = restp
            restp = restp.next
        rc = rc.next


if __name__ == "__main__":
    exs = [[1, 2, 3, 4, 5], [11, 2, 3, 4, 5, 6, 11],
           [11, 2, 3, 4, 5, 6, 11, 11, 11, 23], [0], [0, 0],
           [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1]]

    for ex in exs:
        r1 = llol(ex)
        r2 = llol(ex)
        remove_dups_nobuf(r1)
        remove_dups_nobuf(r2)
        print(f"For ex {ex} for ans 1: {r1}")
        print(f"For ex {ex} for ans 2: {r2}")
        print("-" * 50)