def test_partition(self): a_list = LinkedList() a_list.insert_many([9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 40, 6]) partition_element = 5 partition(a_list, partition_element) less = True for element in a_list.get_as_list(): if element >= partition_element: less = False if less: self.assertTrue(element < partition_element) else: self.assertTrue(element >= partition_element)
class TestKthLast(unittest.TestCase): def setUp(self) -> None: self.original_list = LinkedList() self.original_list.insert_many([1, 2, 3, 4, 5, 6, 7, 8, 9]) def test_remove_duplicates(self): self.assertEqual(find_kth_last(self.original_list, 0), 9) self.assertEqual(find_kth_last(self.original_list, 1), 8) self.assertEqual(find_kth_last(self.original_list, 2), 7) self.assertEqual(find_kth_last(self.original_list, 3), 6) self.assertEqual(find_kth_last(self.original_list, 20), None) def test_remove_duplicates_recursive(self): self.assertEqual(find_kth_last_recursive(self.original_list, 0), 9) self.assertEqual(find_kth_last_recursive(self.original_list, 1), 8) self.assertEqual(find_kth_last_recursive(self.original_list, 2), 7) self.assertEqual(find_kth_last_recursive(self.original_list, 3), 6) self.assertEqual(find_kth_last_recursive(self.original_list, 20), None)
def setUp(self) -> None: self.palindrome = LinkedList() self.palindrome_2 = LinkedList() self.not_palindrome = LinkedList() self.palindrome.insert_many([1, 2, 3, 3, 2, 1]) self.palindrome_2.insert_many([1, 2, 3, 2, 1]) self.not_palindrome.insert_many([1, 5, 2, 3, 2, 1])
class TestIsPalindrome(unittest.TestCase): def setUp(self) -> None: self.palindrome = LinkedList() self.palindrome_2 = LinkedList() self.not_palindrome = LinkedList() self.palindrome.insert_many([1, 2, 3, 3, 2, 1]) self.palindrome_2.insert_many([1, 2, 3, 2, 1]) self.not_palindrome.insert_many([1, 5, 2, 3, 2, 1]) def test_palindromes(self): self.assertTrue(is_palindrome(self.palindrome)) self.assertTrue(is_palindrome(self.palindrome_2)) self.assertFalse(is_palindrome(self.not_palindrome))
class Queue(object): """docstring for Queue""" def __init__(self, value): super(Queue, self).__init__() self.queue = LinkedList(value) def enqueue(self, value): self.queue.insert_back(value) return def dequeue(self): curr = self.queue.get_head() if curr is None: return result = curr.value self.queue.delete(result) return result def print_queue(self): self.queue.print_list()
def setUp(self) -> None: self.original_list = LinkedList() self.original_list.insert_many([1, 2, 3, 4, 5, 6, 7, 8, 9])