Ejemplo n.º 1
0
    def problem1(self, s):
        """Test recursive_search(). 5 points."""

        points = 0

        lls = SinglyLinkedList()
        # Check recursive_search() on empty list (1 point)
        try:
            s.recursive_search(lls, 1)
            self.feedback += "\n\trecursive_search() failed on empty list"
        except ValueError:
            points += 1

        # Check recursive_search() for items in list (3 points)
        lls.append(1)
        lls.append('a')
        lls.append(2)
        points += self._strTest(iterative_search(lls, 1),
                                s.recursive_search(lls, 1),
                                "recursive_search(x) failed for x in list")
        points += self._strTest(iterative_search(lls, 'a'),
                                s.recursive_search(lls, 'a'),
                                "recursive_search(x) failed for x in list")
        points += self._strTest(iterative_search(lls, 2),
                                s.recursive_search(lls, 2),
                                "recursive_search(x) failed for x in list")

        # Check recursive_search() for items not in list (1 point)
        try:
            s.recursive_search(lls, 3)
            self.feedback += "\n\trecursive_search(x) failed for x not in list"
        except ValueError:
            points += 1

        # Check that recursion is used somewhere.
        self._printCode(s.recursive_search)
        points *= self._grade(1, "recursive_search() must use recursion!")
        return points
Ejemplo n.º 2
0
def prob4_plots(N=12, verbose=False):
    """At each iteration, take n random items from a pre-determined subset.

    Time (separately) how long it takes to load a SinglyLinkedList, a BST, and
    an AVL with the data set of n items.

    Choose 5 random items from the data set. Time (separately) how long it
    takes to find all 5 items in each object.

    Create two log-log figures.
    The first figure plots the number of items in each dataset against the
    build time for each object.
    The second figure, plots the number of items against the search time for
    each object.
    """

    # Initialize lists to hold results
    lls_build, lls_search = [], []
    bst_build, bst_search = [], []
    avl_build, avl_search = [], []

    data = np.random.random(2**(N + 1))
    domain = 2**np.arange(3, N + 1)

    # Get the values [start, start + step, ..., stop - step]
    for n in domain:
        if verbose:
            print("\rn = {}".format(n)),
            stdout.flush()

        # Initialize wordlist and data structures
        subset = data[:n]
        bst = BST()
        avl = AVL()
        lls = SinglyLinkedList()

        # Time the singly-linked list build
        begin = time()
        for item in subset:
            lls.append(item)
        lls_build.append(time() - begin)

        # Time the binary search tree build
        begin = time()
        for item in subset:
            bst.insert(item)
        bst_build.append(time() - begin)

        # Time the AVL tree build
        begin = time()
        for item in subset:
            avl.insert(item)
        avl_build.append(time() - begin)

        random_subset = np.random.choice(subset, size=5, replace=False)

        # Time the singly-linked list search
        begin = time()
        for target in random_subset:
            iterative_search(lls, target)
        lls_search.append(time() - begin)

        # Time the binary search tree search
        begin = time()
        for target in random_subset:
            bst.find(target)
        bst_search.append(time() - begin)

        # Time the AVL tree search
        begin = time()
        for target in random_subset:
            avl.find(target)
        avl_search.append(time() - begin)

    # Plot the data
    plt.clf()
    plt.title("Build Times")
    plt.loglog(domain,
               lls_build,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='Singly Linked List')
    plt.loglog(domain,
               bst_build,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='Binary Search Tree')
    plt.loglog(domain,
               avl_build,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='AVL Tree')
    plt.xlabel("n")
    plt.ylabel("Seconds")
    plt.legend(loc='upper left')
    plt.savefig("BuildTimes.pdf")

    plt.clf()
    plt.title("Search Times")
    plt.loglog(domain,
               lls_search,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='Singly Linked List')
    plt.loglog(domain,
               bst_search,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='Binary Search Tree')
    plt.loglog(domain,
               avl_search,
               '.-',
               lw=2,
               ms=10,
               basex=2,
               basey=2,
               label='AVL Tree')
    plt.xlabel("n")
    plt.legend(loc='upper left')
    plt.savefig("SearchTimes.pdf")
    plt.clf()