예제 #1
0
                return False
            ol = ol.next
            nl = nl.next
        return True
    
if __name__ == "__main__":
    l = linklistnode(3)
    for i in [5,6,3,7,9,5,6,5,6,3]:
        l.append(i)
    l.print_list()
    
    helper = linklistutils()
    helper.rm_dup(l)
    l.print_list()

    test(helper.find_kth_end(l, 100), None)
    test(helper.find_kth_end(l, 1), 6)
    test(helper.find_kth_end(l, 2), 9)
    test(helper.find_kth_end(l, 3), 7)

    pl = linklistnode('r')
    for i in ['a', 'c', 'e', 'c', 'a','r']:
        pl.append(i)
    pl.print_list()
    test(helper.is_palindrome(pl), True)

    print("2nd list")
    pl = linklistnode('r')
    for i in ['a', 'c', 'e', 'r', 'a','r']:
        pl.append(i)
    pl.print_list()
예제 #2
0
            if not n.next:
                n.next = new_node
                break
            else:
                pass
            n = n.next

    def print_list(self):
        n = self;
        while n:
            print(n.data)
            n = n.next

if __name__ == "__main__":
    l = linklistnode(154)
    test(l.data, 154)
    l.append(3)
    test(l.data, 154)
    test(l.next.data, 3)
    
    l.insert(7)
    print (l.data)
    test(l.data, 7)
    test(l.next.data, 154)
    test(l.next.next.data, 3)
    test(l.next.next.next, None)
    
    l.append(300)
    l.print_list()

예제 #3
0
#!/usr/bin/env python

import sys
from testy import test

# reverse a string
def reverse(s):
    l = len(s)
    ns = ''
    for i in range(l-1, -1, -1):
        ns += s[i]
    return ns

if __name__ == '__main__':
    print test(reverse("take this"), 'siht ekat')
#!/usr/bin/env python

from testy import test

# return true if string has all unique characters
def string_unique(s):
    h = {}
    for c in s:
        if c in h:
            return False
        else:
            h[c] = 1
    return True

if __name__ == '__main__':
    print test(string_unique('tony tam is not unique'), False)
    print test(string_unique('tony am'), True)
예제 #5
0
    
    # number of bits to shift from A to B
    def num_bits_to_shift(self, a, b):
        num = 0
        # bit by bit from the right.  Calculate the difference
        while a != 0:
            if self.getbit(a, 0) ^ self.getbit(b,0) == 1:
                num += 1
            a = a >> 1
            b = b >> 1
        return num
    
        return -1

if __name__ == '__main__':
    b = bit()
    test(b.getbit(8, 3), 1)
    test(b.getbit(8, 0), 0)
    
    test(b.setbit(8, 0), 9)
    test(b.setbit(8, 1), 10)
    test(b.setbit(1, 1), 3)
    test(b.setbit(1, 0), 1)

    test(b.clearbit(8, 3), 0)
    test(b.clearbit(8, 0), 8)

    test(b.insertbits(0b10000000000, 0b0000000010011, 2, 6), 0b10001001100)

    test(b.num_bits_to_shift(0b11101, 0b01111), 2)