def josephus(n, m):
    L = DoublyLinkedList()
    for i in range(1, n + 1):
        L.append(chr(ord('A') + i - 1))
    p = L.first()
    for i in range(n - 1):
        print(L, p.el)
        for j in range(m):
            p = p.next
            if p.el is None:
                p = L.first()
        q = p
        p = p.next
        if p.el is None:
            p = L.first()
        L.remove(q)
    return L.first().el
Example #2
0
check("a", a, "[1, 3, 5, 7, 9, 3, 9, 3, 7, 13, 5]")
a.remove_all(37)
print("After a.remove_all(37)")
check("a", a, "[1, 3, 5, 7, 9, 3, 9, 3, 7, 13, 5]")
a.remove_all(3)
print("After a.remove_all(3)")
check("a", a, "[1, 5, 7, 9, 9, 7, 13, 5]")

b.remove_all(81)
print("After b.remove_all(81)")
check("b", b, "[]")

a = make(1, 3, 5, 7, 9, 11, 3, 9, 3, 11, 7, 11, 13, 5)
print("a = %s" % a)
n = a.find_first(5)
m = a.find_last(9)
b = a.takeout(n, m)
print("After b = a.takeout(n, m)")
check("a", a, "[1, 3, 3, 11, 7, 11, 13, 5]")
check("b", b, "[5, 7, 9, 11, 3, 9]")

c = b.takeout(b.first(), b.last())
print("After c = b.takeout(b.first(), b.last())")
check("b", b, "[]")
check("c", c, "[5, 7, 9, 11, 3, 9]")

d = c.takeout(c.first().next, c.find_first(11))
print("After d = c.takeout(c.first().next, c.find_first(11))")
check("c", c, "[5, 3, 9]")
check("d", d, "[7, 9, 11]")