#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Remove duplicates from a linked list. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Can we assume this is a non-circular, singly linked list? # * Yes # * Can you insert None values in the list? # * No # * Can we assume we already have a linked list class that can be used for this problem? # * Yes # * Can we use additional data structures? # * Implement both solutions # * Can we assume this fits in memory? # * Yes # ## Test Cases # # * Empty linked list -> [] # * One element linked list -> [element] # * General case with no duplicates # * General case with duplicates # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: get_ipython().run_line_magic('run', '../linked_list/linked_list.py') get_ipython().run_line_magic('load', '../linked_list/linked_list.py') # In[ ]: class MyLinkedList(LinkedList): def remove_dupes(self): # TODO: Implement me pass # ## Unit Test # # # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_remove_duplicates.py import unittest class TestRemoveDupes(unittest.TestCase): def test_remove_dupes(self, linked_list): print('Test: Empty list') linked_list.remove_dupes() self.assertEqual(linked_list.get_all_data(), []) print('Test: One element list') linked_list.insert_to_front(2) linked_list.remove_dupes() self.assertEqual(linked_list.get_all_data(), [2]) print('Test: General case, duplicates') linked_list.insert_to_front(1) linked_list.insert_to_front(1) linked_list.insert_to_front(3) linked_list.insert_to_front(2) linked_list.insert_to_front(3) linked_list.insert_to_front(1) linked_list.insert_to_front(1) linked_list.remove_dupes() self.assertEqual(linked_list.get_all_data(), [1, 3, 2]) print('Test: General case, no duplicates') linked_list.remove_dupes() self.assertEqual(linked_list.get_all_data(), [1, 3, 2]) print('Success: test_remove_dupes\n') def main(): test = TestRemoveDupes() linked_list = MyLinkedList(None) test.test_remove_dupes(linked_list) if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb) for a discussion on algorithms and code solutions.