Example #1
0
def sort_words(filename = "English.txt"):
    """Use the 'create_word_list' method from the 'WordList' module to generate
    a scrambled list of words from the specified file. Use an instance of
    the SortedLinkedList class to sort the list. Then return the list.
    
    Inputs:
        filename (str, opt): the file to be parsed and sorted.
            Defaults to 'English.txt'.
    
    Returns:
        A SortedLinkedList object containing the sorted list of words.
    """
    my_list = create_word_list(filename)
    sorted_list = SortedLinkedList()
    for x in my_list:
        sorted_list.add(x)

    file = open("test.txt",'w')
    it = sorted_list.head
    out_list = []
    while it.next != None:
        file.write(it.data + '\n')
        it = it.next
    file.close()
    return sorted_list
def sort_words(filename = "English.txt"):
    """Use the 'create_word_list' method from the 'WordList' module to generate
    a scrambled list of words from the specified file. The use an instance of
    the SortedLinkedList class to sort the list.
    """
    s = SortedLinkedList()                  # Create the Sorted List object
    word_list = create_word_list(filename)  # Generate the word list
    for word in word_list:                  # Add each word to the Sorted List
        s.add(word)
    return s                                # Return the Sorted Linked List.
Example #3
0
def sort_words(filename = "English.txt"):
    """Use the 'create_word_list' method from the 'WordList' module to generate
    a scrambled list of words from the specified file. Use an instance of
    the SortedLinkedList class to sort the list. Then return the list.
    
    Inputs:
        filename (str, opt): the file to be parsed and sorted.
            Defaults to 'English.txt'.
    
    Returns:
        A SortedLinkedList object containing the sorted list of words.
    """
    s = SortedLinkedList()                  # Create the Sorted List object
    word_list = create_word_list(filename)  # Generate the word list
    for word in word_list:                  # Add each word to the Sorted List
        s.add(word)
    return s                                # Return the Sorted Linked List.
Example #4
0
def sort_words(filename="English.txt"):
    """Use the 'create_word_list' method from the 'WordList' module to generate
    a scrambled list of words from the specified file. Use an instance of
    the SortedLinkedList class to sort the list. Then return the list.
    
    Inputs:
        filename (str, opt): the file to be parsed and sorted.
            Defaults to 'English.txt'.
    
    Returns:
        A SortedLinkedList object containing the sorted list of words.
    """
    words = create_word_list(filename)
    A = SortedLinkedList()
    for word in words:
        A.add(word)
    print A
    return A
Example #5
0
def plot_times(filename="English.txt", start=500, stop=5500, step=500):
    """Vary n from 'start' to 'stop', incrementing by 'step'. At each
    iteration, use the create_word_list() from the 'WordList' module to
    generate a list of n randomized words from the specified file.
    
    Time (separately) how long it takes to load a LinkedList, a BST, and
    an AVL with the data set.
    
    Choose 5 random words from the data set. Time how long it takes to
    find each word in each object. Calculate the average search time for
    each object.
    
    Create one plot with two subplots. In the first subplot, plot the
    number of words in each dataset against the build time for each object.
    In the second subplot, plot the number of words against the search time
    for each object.
    
    Inputs:
        filename (str): the file to use in creating the data sets.
        start (int): the lower bound on the sample interval.
        stop (int): the upper bound on the sample interval.
        step (int): the space between points in the sample interval.
    
    Returns:
        Show the plot, but do not return any values.
    """
    interval = (stop-start)/step
    n_list = np.linspace(start,stop,interval+1)
    n_list = np.int16(n_list)
    

    word_list = create_word_list(filename)
    
    load_list = []
    load_BST = []
    load_AVL = []
    
    find_list = []
    find_BST = []
    find_AVL = []
    
    for n in n_list:
        temp_word_list = word_list[:n]
        random_word_indices = np.random.randint(0,n,size=5)
        words_to_find = []
        for x in random_word_indices:
            words_to_find.append(temp_word_list[x])

        L = LinkedList()
        B = BST()
        A = AVL()
        
        start = time()
        for word in temp_word_list:
            L.add(word)
        end = time()
        load_list.append(end-start)

        start = time()
        for word in temp_word_list:
            B.insert(word)
        end = time()
        load_BST.append(end-start)

        start = time()
        for word in temp_word_list:
            A.insert(word)
        end = time()
        load_AVL.append(end-start)
        
        start = time()
        for word in words_to_find:
            iterative_search(L, word)
        end = time()
        find_list.append(end-start)

        start = time()
        for word in words_to_find:
            B.find(word)
        end = time()
        find_BST.append(end-start)

        start = time()
        for word in words_to_find:
            A.find(word)
        end = time()
        find_AVL.append(end-start)
    
    avg_find_list = sum(find_list[:])/5.
    avg_find_BST = sum(find_BST[:])/5.
    avg_find_AVL = sum(find_AVL[:])/5.

    plt.subplot(121)
    list_plot1 = plt.plot(n_list, load_list,label='Singly-Linked List')
    BST_plot1 = plt.plot(n_list, load_BST, label='Binary Search Tree')
    AVL_plot1 = plt.plot(n_list, load_AVL, label='AVL Tree')
    plt.legend()
    plt.xlabel('Data Points')
    plt.ylabel('Seconds')
    plt.title('Build Times')

    plt.subplot(122)
    list_plot2 = plt.plot(n_list, find_list,label='Singly-Linked List')
    BST_plot2 = plt.plot(n_list, find_BST, label='Binary Search Tree')
    AVL_plot2 = plt.plot(n_list, find_AVL, label='AVL Tree')
    plt.legend()
    plt.xlabel('Data Points')
    plt.ylabel('Seconds')
    plt.title('Search Times')

    plt.show() 
Example #6
0
def test(student_module, late=False):
    """Test script. You must import the student's 'solutions.py' and 'Node.py'
    files as modules.
    
     5 points for problem 1
    10 points for problem 2
    10 points for problem 3
    15 points for problem 4
    20 points for problem 5
    20 points for problem 6
    
    Inputs:
        student_module: the imported module for the student's file.
        late (bool): if True, half credit is awarded.
    
    Returns:
        score (int): the student's score, out of 80.
        feedback (str): a printout of test results for the student.
    """
    s = student_module
    SNode = s.LinkedListNode
    score = 0
    total = 80
    feedback = ""

    def strTest(x,y,m):
        """Test to see if x and y have the same string representation. If
        correct, award a points and return no message. If incorrect, return
        0 and return 'm' as feedback.
        """
        if str(x) == str(y): return 1, ""
        else:
            m += "\n\t\tCorrect response: " + str(x)
            m += "\n\t\tStudent response: " + str(y)
            return 0, m
    
    def testTail(x,y,m):
        """Test to see if x and y have the same tail attribute. If correct,
        award a point and return no message. If incorrect, return 0 and
        return 'm' as feedback. Problematic if list has 0 or 1 entries.
        """
        if x.tail.prev.next == y.tail.prev.next: return 1, ""
        else:
            m += "\n\t\tCorrect tail: " + str(x)
            m += "\n\t\tStudent tail: " + str(y)
            return 0, m
    
    def grade(p,m):
        """Manually grade a problem worth 'p' points with error message 'm'."""
        part = -1
        while part > p or part < 0:
            part = int(input("\nScore out of " + str(p) + ": "))
        if part == p: return p,""
        else: return part,m
    
    def shrink_file(infile, outfile):
        """Shrink the dataset in problem 6 so it can be tested quickly."""
        try:
            f = open(infile, 'r')
            f = f.read()
            f = f.split('\n')
            f = f[:-1]
            x = list(permutation(f))[::20]
            f = open(outfile, 'w')
            for i in x:
                f.write(i + '\n')
            f.close()
        except IOError:
            raise IOError(str(infile) + " not found!")
    
    try:    # Problem 1: 5 points
        feedback += "\n\nProblem 1 (5 points):"
        points = 0
        # Comparison magic methods
        n1 = SNode(5)
        n2 = SNode(5)
        if not (n1 < n2): points += 1
        if n1 == n2: points += 1
        n1 = SNode(4)
        n2 = SNode(6)
        if n1 < n2: points += 1
        if points < 3:
            feedback += "\n\t" + str(3-points)
            feedback += " Node class comparison magic method(s) failed"
        # __str__
        n1 = Node(6)
        p,f = strTest(n1,n2,"\n\tNode.__str__ failed")
        points += (p * 2); feedback += f
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
    
    try:    # Problem 2: 10 points
        feedback += "\n\nProblem 2 (10 points):"
        points = 0
        # Empty list
        l1 = list()
        l2 = s.LinkedList()
        p,f = strTest(l1,l2,"\n\tLinkedList.__str__ failed on empty list")
        points += p; feedback += f
        # Single item
        l1.append('this')
        l2.add('this')
        p,f = strTest(l1,l2,"\n\tLinkedList.__str__ failed with single item")
        points += (p * 3); feedback += f
        # Two items
        l1.append('little')
        l2.add('little')
        p,f = strTest(l1,l2,"\n\tLinkedList.__str__ failed with two items")
        points += (p * 3); feedback += f
        # Many items
        entries = ['Linked List',3,10.0,-1+3j,set(),[1,2,3]]
        for i in entries:
            l1.append(i)
            l2.add(i)
        p,f = strTest(l1,l2,"\n\tLinkedList.__str__ failed with many items")
        points += (p * 3); feedback += f
        if points == 0:
            feedback += "\n\tCheck LinkedList.add() and LinkedList.__str__"
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
        
    try:    # Problem 3: 10 points
        feedback += "\n\nProblem 3 (10 points):"
        points = 0
        l1 =   LinkedList()
        l2 = s.LinkedList()
        # remove() from empty list
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.remove(100)
            feedback += "\n\tNo exception raised by LinkedList.remove()"
            feedback += " on empty list"
        except ValueError as e:
            points += 1; print(e.message)
            p,f = grade(1,
                "\n\tLinkedList.remove() failed to report on empty list")
            points += p; feedback += f;
        # Test add() (no credit, but vital for other points)
        for i in [1,3,2,5,4,7,6,9,8]:
            l1.add(i); l2.add(i)
        p,f=strTest(l1,l2,
            "\n\tIf LinkedList.__str__ fails, these tests will all fail!")
        feedback += f
        # remove() head
        l1.remove(1); l1.remove(3)
        l2.remove(1); l2.remove(3)
        p,f = strTest(l1,l2, "\n\tLinkedList.remove() failed on head removal")
        points += (p * 2); feedback += f
        # remove() end
        l1.remove(8); l1.remove(9)
        l2.remove(8); l2.remove(9)
        p,f = strTest(l1,l2, "\n\tLinkedList.remove() failed on tail removal")
        points += (p * 2); feedback += f
        # remove() from middle
        l1.remove(5); l1.remove(4)
        l2.remove(5); l2.remove(4)
        p,f=strTest(l1,l2, "\n\tLinkedList.remove() failed on middle removal")
        points += (p * 2); feedback += f
        # remove() nonexistent
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.remove(100)
            feedback += "\n\tNo exception raised by LinkedList.remove(x)"
            feedback += " for 'x' not in list"
        except ValueError as e:
            points += 1; print(e.message)
            p,f = grade(1,
                "\n\tLinkedList.remove(x) failed to report for 'x' not in list")
            points += p; feedback += f;
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
        
    try:    # Problem 4: 15 Points
        feedback += "\n\nProblem 4 (15 points):"
        points = 0
        l1 =   LinkedList()
        l2 = s.LinkedList()
        # insert() empty list
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.insert(1,100)
            feedback += "\n\tNo exception raised by LinkedList.insert()"
            feedback += " on empty list"
        except ValueError as e:
            points += 1; print(e.message)
            p,f = grade(1,
                "\n\tLinkedList.insert() failed to report on empty list")
            points += p; feedback += f;
        # insert() before head
        l1.add(5); l1.insert(3,5); l1.insert(1,3)
        l2.add(5); l2.insert(3,5); l2.insert(1,3)
        p,f=strTest(l1,l2,"\n\tLinkedList.insert() failed on head insertion")
        points += (p * 5); feedback += f
        # insert() in the middle
        l1.insert(2,3); l1.insert(4,5)
        l2.insert(2,3); l2.insert(4,5)
        p,f=strTest(l1,l2,
            "\n\tLinkedList.insert() failed on middle insertion")
        points += (p * 5); feedback += f
        # insert(place, x) on nonexistant place
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.insert(1,100)
            feedback += "\n\tNo exception raised by LinkedList.insert(x, place)"
            feedback += " for 'place' not in list"
        except ValueError as e:
            points += 2; print(e.message)
            p,f = grade(1, "\n\tLinkedList.remove(x, place)" + 
                                " failed to report for 'place' not in list")
            points += p; feedback += f;
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
        
    try:    # Problem 5: 20 points
        feedback += "\n\nProblem 5 (20 points):"
        points = 0
        l1 =   DoublyLinkedList()
        l2 = s.DoublyLinkedList()
        # remove() from empty list
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.remove(100)
            feedback += "\n\tNo exception raised by DoublyLinkedList.remove()"
            feedback += " on empty list"
        except ValueError as e:
            print(e.message)
            p,f = grade(1,
                "\n\tDoublyLinkedList.remove() failed to report on empty list")
            points += p; feedback += f;
        # Test add() (no credit, but vital for other points)
        for i in [1,3,2,5,4,7,6,9,8]:
            l1.add(i); l2.add(i)
        p,f = strTest(l1,l2,"\n\tDoublyLinkedList.add() failed")
        feedback += f
        p,f = testTail(l1,l2,"\n\tDoublyLinkedList.tail failed on add()")
        points += p; feedback += f
        # remove() head
        l1.remove(1); l1.remove(3)
        l2.remove(1); l2.remove(3)
        p,f = strTest(l1,l2,
            "\n\tDoublyLinkedList.remove() failed on head removal")
        points += (p * 2); feedback += f
        p,f = testTail(l1,l2,
            "\n\tDoublyLinkedList.tail failed on head removal")
        points += p; feedback += f
        # remove() end
        l1.remove(8); l1.remove(9)
        l2.remove(8); l2.remove(9)
        p,f = strTest(l1,l2,
            "\n\tDoublyLinkedList.remove() failed on tail removal")
        points += (p * 2); feedback += f
        p,f = testTail(l1,l2,
            "\n\tDoublyLinkedList.tail failed on tail removal")
        points += p; feedback += f
        # remove() from middle
        l1.remove(5); l1.remove(4)
        l2.remove(5); l2.remove(4)
        p,f=strTest(l1,l2,
            "\n\tDoublyLinkedList.remove() failed on middle removal")
        points += (p * 2); feedback += f
        p,f = testTail(l1,l2,
            "\n\tDoublyLinkedList.tail failed on middle removal")
        points += p; feedback += f
        # remove() nonexistent
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.remove(100)
            feedback += "\n\tNo exception raised by DoublyLinkedList.remove(x)"
            feedback += " for 'x' not in list"
        except ValueError as e:
            print(e.message)
            p,f = grade(1, "\n\tDoublyLinkedList.remove(x)" + 
                                " failed to report for 'x' not in list")
            points += p; feedback += f;
        # insert() empty list
        l1.__init__(); l2.__init__()
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.insert(1,100)
            feedback += "\n\tNo exception raised by DoublyLinkedList.insert()"
            feedback += " on empty list"
        except ValueError as e:
            print(e.message)
            p,f = grade(1, "\n\tDoublyLinkedList.insert()" + 
                                " failed to report on empty list")
            points += p; feedback += f;
        # insert() before head
        l1.add(5); l1.insert(3,5); l1.insert(1,3)
        l2.add(5); l2.insert(3,5); l2.insert(1,3)
        p,f=strTest(l1,l2,
            "\n\tDoublyLinkedList.insert() failed on head insertion")
        points += (p * 2); feedback += f
        p,f = testTail(l1,l2,
            "\n\tDoublyLinkedList.tail failed on head insertion")
        points += p; feedback += f
        # insert() in the middle
        l1.insert(2,3); l1.insert(4,5)
        l2.insert(2,3); l2.insert(4,5)
        p,f=strTest(l1,l2,
            "\n\tDoublyLinkedList.insert() failed on middle insertion")
        points += (p * 2); feedback += f
        p,f = testTail(l1,l2,
            "\n\tDoublyLinkedList.tail failed on middle insertion")
        points += p; feedback += f
        # insert(place, x) on nonexistant place
        print("\nCorrect output:\t100 is not in the list.")
        print("Student output:\t"),
        try:
            l2.insert(1,100)
            feedback += "\n\tNo exception raised by"
            feedback += " DoublyLinkedList.insert(x,place)"
            feedback += " for 'place' not in list"
        except ValueError as e:
            print(e.message)
            p,f = grade(1, "\n\tDoublyLinkedList.remove(x,place)" + 
                                " failed to report for 'place' not in list")
            points += p; feedback += f;
        if not issubclass(s.DoublyLinkedList, s.LinkedList):
            points = 0
            feedback += "\n\tDoublyLinkedList must inherit from LinkedList!"
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
        
    try:    # Problem 6: 20 points
        feedback += "\n\nProblem 6 (20 points):"
        points = 0
        l1 =   SortedLinkedList()
        l2 = s.SortedLinkedList()
        # Test sorting (9 points)
        # test 1
        entries = [1,2,3,4,5,6,7,8,9]
        for i in entries:
            l1.add(i); l2.add(i)
        p,f = strTest(l1,l2,"\n\tSortedLinkedList.add() failed")
        points += (p * 3); feedback += f
        # test 2
        l1.__init__(); l2.__init__()
        entries = [9,8,7,6,5,4,2,3,1]
        for i in entries:
            l1.add(i); l2.add(i)
        p,f = strTest(l1,l2,"\n\tSortedLinkedList.add() failed")
        points += (p * 3); feedback += f
        # test 3
        l1.__init__(); l2.__init__()
        entries = [1,3,5,7,9,2,4,6,8]
        for i in entries:
            l1.add(i); l2.add(i)
        p,f = strTest(l1,l2,"\n\tSortedLinkedList.add() failed")
        points += (p * 3); feedback += f
        # Test that insert() was disabled (1 point)
        print("\nCorrect output:\tinsert() has been disabled for this class.")
        print("Student output:\t"),
        try:
            l2.insert(1,2,3,4,5)
            feedback += "\n\tNo ValueError exception raised by"
            feedback += " SortedLinkedList.insert()"
        except NotImplementedError as e:
            print(e.message)
            p,f = grade(1, "\n\tSortedLinkedList.insert()" + 
                                " failed to report as disabled")
            points += p; feedback += f;
        except TypeError:
            feedback += "\n\tSortedLinkedList.insert() not disabled correctly"
            feedback += "\n\t\t(insert() should accept any number of arguments)"
        # 10 points for correct sort_words() output.
        shrink_file("English.txt", "Short.txt")
        word_list = create_word_list("Short.txt")
        word_list.sort()
        out = s.sort_words("Short.txt")
        p,f = strTest(word_list, out, "\n\tsort_words() function failed.")
        points += (p * 10); feedback += f
        # detect cheating
        if out.__doc__ != l2.__doc__:
            points = 0
            feedback += "\n\tA SortedLinkedList object must be "
            feedback += "returned in sort_words()!"
        if not issubclass(s.SortedLinkedList, s.DoublyLinkedList):
            points = 0
            feedback += "\n\tSortedLinkedList must inherit "
            feedback += "from DoublyLinkedList!"
        
        score += points; feedback += "\nScore += " + str(points)
    except Exception as e: feedback += "\nError: " + e.message
    
    if late:    # Late submission penalty
        feedback += "\n\nHalf credit for late submission."
        feedback += "\nRaw score: " + str(score) + "/" + str(total)
        score *= .5
    
    # Report final score.
    feedback += "\n\nTotal score: " + str(score) + "/" + str(total)
    percentage = (100.0 * score) / total
    feedback += " = " + str(percentage) + "%"
    if percentage < 72.0 and not late:
        feedback += "\n\nOn any given problem, if one test fails then"
        feedback += " subsequent tests are likely to fail.\nFix the tests in"
        feedback += " the order that they are mentioned in this feedback file."
    if   percentage >=  98.0: feedback += "\n\nExcellent!"
    elif percentage >=  90.0: feedback += "\n\nGreat job!"
    feedback += "\n\n-------------------------------------------------------\n"
    return score, feedback
Example #7
0
def plot_times(filename="English.txt", start=500, stop=5500, step=500):
    """Vary n from 'start' to 'stop', incrementing by 'step'. At each
    iteration, use the create_word_list() from the 'WordList' module to
    generate a list of n randomized words from the specified file.
    
    Time (separately) how long it takes to load a LinkedList, a BST, and
    an AVL with the data set.
    
    Choose 5 random words from the data set. Time how long it takes to
    find each word in each object. Calculate the average search time for
    each object.
    
    Create one plot with two subplots. In the first subplot, plot the
    number of words in each dataset against the build time for each object.
    In the second subplot, plot the number of words against the search time
    for each object.
    
    Inputs:
        filename (str): the file to use in creating the data sets.
        start (int): the lower bound on the sample interval.
        stop (int): the upper bound on the sample interval.
        step (int): the space between points in the sample interval.
    
    Returns:
        Show the plot, but do not return any values.
    """

    def get_average_time_linked_list(to_search, linked_list, times_left, current_time = 0):
        while times_left > 0:
            start = time.time()
            iterative_search(linked_list, to_search[times_left-1])
            end =time.time()
            current_time +=(end-start)
            times_left -=1
        return current_time/len(to_search)

    def get_average_time_BST(to_search, BST_list, times_left, current_time =0):
        while times_left >0:
            start = time.time()
            BST_list.find(to_search[times_left-1])
            end = time.time()
            current_time +=(end-start)
            times_left -= 1 
        return current_time/len(to_search)
    def get_average_time_AVL(to_search, AVL_list, times_left, current_time = 0):
        while times_left > 0:
            start = time.time()
            AVL_list.find(to_search[times_left-1])
            end = time.time()
            current_time +=(end-start)
            times_left -= 1
        return current_time/len(to_search)


    word_list = create_word_list(filename)
    if (stop-start)%step!=0:
        raise ValueError("Your steps won't get you from start to stop")
    current = start
    time_linked_list = []
    time_BST_list = []
    time_AVL_list = []

    time_linked_list_search = []
    time_BST_list_search = []
    time_AVL_list_search = []

    set_size = []

    while current < stop:
        current_linked_list = LinkedList()
        current_BST = BST()
        current_AVL = AVL()
        current_list = word_list[:current]
        to_search = np.random.permutation(current_list)
        start_linked_time = time.time()

        for x in current_list:
            current_linked_list.add(x)
        end_linked_time = time.time()

        start_BST_time = time.time()
        for y in current_list:
            current_BST.insert(y)
        end_BST_time = time.time()

        start_AVL_time = time.time()
        for z in current_list:
            current_AVL.insert(z)
        end_AVL_time = time.time()

        time_linked_list.append(end_linked_time - start_linked_time)
        time_BST_list.append(end_BST_time - start_BST_time)
        time_AVL_list.append(end_AVL_time- start_AVL_time)

        time_linked_list_search.append(get_average_time_linked_list(to_search,current_linked_list, len(to_search)))
        time_BST_list_search.append(get_average_time_BST(to_search,current_BST, len(to_search)))
        time_AVL_list_search.append(get_average_time_AVL(to_search,current_AVL, len(to_search)))

        set_size.append(current)

        current+=step
    plt.subplot(2,1,1)
    plt.title('Building Data Structures')
    plt.plot(set_size,time_linked_list, label = 'Linked List', linewidth = 3)
    plt.plot(set_size, time_BST_list, label = "BST", linewidth = 3)
    plt.plot(set_size, time_AVL_list, label = "AVL", linewidth = 3)
    plt.legend(loc = 2)

    plt.subplot(2,1,2)
    plt.title("Searching Data Structures")
    plt.plot(set_size, time_linked_list_search, label = 'Linked list', linewidth = 3)
    plt.plot(set_size, time_BST_list_search, label = 'BST', linewidth = 3)
    plt.plot(set_size, time_AVL_list_search, label = 'AVL', linewidth = 3)
    plt.legend(loc = 2)
    plt.show()
Example #8
0
def timings():
    ll = LinkedList()
    bst = BST()
    avl = AVL()

    ll_add = []
    bst_add = []
    avl_add = []

    ll_search = []
    bst_search = []
    avl_search = []

    for items in range(500,5500,500):
        wordlist = create_word_list(items)
        
        ll = LinkedList()
        before = time.time()
        for i in xrange(items):
            ll.add_node(wordlist[i])
        after = time.time()
        ll_add.append(after - before)
    
        random_indices = np.random.random_integers(0,items,5)
        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            iterative_search(ll, wordlist[random_indices[i]]) 
            after = time.time()
            temp.append(after - before)
        ll_search.append(sum(temp)/len(temp))
    
        bst = BST()
        before = time.time()
        for i in xrange(items):
            bst.insert(wordlist[i])
        after = time.time()
        bst_add.append(after - before)

        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            bst.find(wordlist[random_indices[i]])
            after = time.time()
            temp.append(after - before)
        bst_search.append(sum(temp)/len(temp))

        avl = AVL()
        before = time.time()
        for i in xrange(items):
            avl.insert(wordlist[i])
        after = time.time()
        avl_add.append(after - before)
        
        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            avl.find(wordlist[random_indices[i]])
            after = time.time()
            temp.append(after - before)
        avl_search.append(sum(temp)/len(temp))
    
    plt.subplot(1,2,1)
    plt.plot(ll_add, "r")
    plt.plot(bst_add, "g")
    plt.plot(avl_add, "b")
    plt.subplot(1,2,2)
    plt.plot(ll_search, "r")
    plt.plot(bst_search, "g")
    plt.plot(avl_search, "b")
    plt.show()
    plt.close()
    
    return ll_add, ll_search, bst_add, bst_search, avl_add, avl_search
Example #9
0
def plot_times(filename="English.txt", start=500, stop=5500, step=500):
    """Vary n from 'start' to 'stop', incrementing by 'step'. At each
    iteration, use the create_word_list() from the 'WordList' module to
    generate a list of n randomized words from the specified file.
    
    Time (separately) how long it takes to load a LinkedList, a BST, and
    an AVL with the data set.
    
    Choose 5 random words from the data set. Time how long it takes to
    find each word in each object. Calculate the average search time for
    each object.
    
    Create one plot with two subplots. In the first subplot, plot the
    number of words in each dataset against the build time for each object.
    In the second subplot, plot the number of words against the search time
    for each object.
    
    Inputs:
        filename (str): the file to use in creating the data sets.
    
    Returns:
        Show the plot, but do not return any values.
    """
    
    # Initialize lists to hold results
    lls_build, lls_search = list(), list()
    bst_build, bst_search = list(), list()
    avl_build, avl_search = list(), list()
    
    # Get the values [start, start + step, ..., stop - step]
    domain = list()
    for n in xrange(start,stop,step):
    
        # Initialize wordlist and data structures
        word_list = create_word_list(filename)[:n]
        bst = BST()
        avl = AVL()
        lls = LinkedList()
        
        # Time the singly-linked list build
        start = time()
        for word in word_list:
            lls.add(word)
        lls_build.append(time() - start)
        
        # Time the binary search tree build
        start = time()
        for word in word_list:
            bst.insert(word)
        bst_build.append(time() - start)
        
        # Time the AVL tree build
        start = time()
        for word in word_list:
            avl.insert(word)
        avl_build.append(time() - start)
        
        # Search Times
        search1, search2, search3 = list(), list(), list()
        for i in xrange(5):
            target = word_list[randint(0, n-1)]
            
            # Time LinkedList.find
            start = time()
            iterative_search(lls, target)
            search1.append(time() - start)
            
            # Time BST.find
            start = time()
            bst.find(target)
            search2.append(time() - start)
            
            # Time AVL.find
            start = time()
            avl.find(target)
            search3.append(time() - start)
        
        lls_search.append(sum(search1)/len(search1))
        bst_search.append(sum(search2)/len(search2))
        avl_search.append(sum(search3)/len(search3))
        domain.append(n)
    
    # Plot the data
    plt.subplot(121)
    plt.title("Build Times")
    plt.plot(domain,lls_build,label='Singly-Linked List')
    plt.plot(domain,bst_build,label='Binary Search Tree')
    plt.plot(domain,avl_build,label='AVL Tree')
    plt.ylabel("seconds")
    plt.xlabel("data points")
    plt.legend(loc='upper left')
    
    plt.subplot(122)
    plt.title("Search Times")
    plt.plot(domain,lls_search,label='Singly-Linked List')
    plt.plot(domain,bst_search,label='Binary Search Tree')
    plt.plot(domain,avl_search,label='AVL Tree')
    plt.ylabel("seconds")
    plt.xlabel("data points")
    plt.legend(loc='upper left')
    
    plt.show()
Example #10
0
def plot_times(filename="English.txt", start=500, stop=5500, step=500):
    """Vary n from 'start' to 'stop', incrementing by 'step'. At each
    iteration, use the create_word_list() from the 'WordList' module to
    generate a list of n randomized words from the specified file.
    
    Time (separately) how long it takes to load a LinkedList, a BST, and
    an AVL with the data set.
    
    Choose 5 random words from the data set. Time how long it takes to
    find each word in each object. Calculate the average search time for
    each object.
    
    Create one plot with two subplots. In the first subplot, plot the
    number of words in each dataset against the build time for each object.
    In the second subplot, plot the number of words against the search time
    for each object.
    
    Inputs:
        filename (str): the file to use in creating the data sets.
        start (int): the lower bound on the sample interval.
        stop (int): the upper bound on the sample interval.
        step (int): the space between points in the sample interval.
    
    Returns:
        Show the plot, but do not return any values.
    """
    ll = LinkedList()
    bst = BST()
    avl = AVL()

    ll_add = []
    bst_add = []
    avl_add = []

    ll_search = []
    bst_search = []
    avl_search = []

    for items in range(start,stop,step):
        wordlist = create_word_list()[:items]
        
        ll = LinkedList()
        before = time.time()
        for i in xrange(items):
            ll.add(wordlist[i])
        after = time.time()
        ll_add.append(after - before)
    
        random_indices = np.random.random_integers(0,items,5)
        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            iterative_search(ll, wordlist[random_indices[i]]) 
            after = time.time()
            temp.append(after - before)
        ll_search.append(sum(temp)/len(temp))
    
        bst = BST()
        before = time.time()
        for i in xrange(items):
            bst.insert(wordlist[i])
        after = time.time()
        bst_add.append(after - before)

        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            bst.find(wordlist[random_indices[i]])
            after = time.time()
            temp.append(after - before)
        bst_search.append(sum(temp)/len(temp))

        avl = AVL()
        before = time.time()
        for i in xrange(items):
            avl.insert(wordlist[i])
        after = time.time()
        avl_add.append(after - before)
        
        temp = []
        for i in xrange(len(random_indices)):
            before = time.time()
            avl.find(wordlist[random_indices[i]])
            after = time.time()
            temp.append(after - before)
        avl_search.append(sum(temp)/len(temp))
    
    plt.subplot(1,2,1)
    plt.title("Build Times")
    plt.plot(ll_add, "b", label="Single-Linked List")
    plt.plot(bst_add, "g", label="Binary Search Tree")
    plt.plot(avl_add, "r", label ="AVL Tree")
    plt.legend(loc="upper left")
    plt.subplot(1,2,2)
    plt.title("Search Times")
    plt.plot(ll_search, "b", label="Single-Linked List")
    plt.plot(bst_search, "g", label="Binary Search Tree")
    plt.plot(avl_search, "r", label="AVL Tree")
    plt.legend(loc="upper left")
    plt.show()
    plt.close()
    
    return ll_add, ll_search, bst_add, bst_search, avl_add, avl_search