def profileSort(sortFunction, upper = 10):
    """Displays counts of comparisons and swaps
    and running times for the given sort algorithm
    on several data sizes.  The data size starts at 1
    and doubles on each iteration.  An initial run
    shows results for a sorted list of 256 numbers.
    Arguments: the sort function and the number of data sets."""
    comps = Counter()
    swaps = Counter()
    lyst = list(range(1, 257))
    t1 = time.time()
    sortFunction(lyst, comps, swaps)
    t2 = time.time() - t1
    print("Results for a sorted list of 256 numbers:")
    print("Comparisons:    ", comps)
    print("Swaps:          ", swaps)
    print("Time in seconds:", t2)
    size = 1
    print("\n Size    Comparisons     Swaps   Running Time (sec)")
    for i in range(1, upper + 1):
        comps.reset()
        swaps.reset()
        lyst = getRandomList(size)
        t1 = time.time()
        sortFunction(lyst, comps, swaps)
        t2 = time.time() - t1
        print("%5d%12s%12s%15.6f" % (size, comps, swaps, t2))
        size *= 2
Exemple #2
0
def testWithCounters(sort, n=15):
    """Runs some tests on a sort function."""
    comps = Counter()
    swaps = Counter()
    lyst = list(range(1, n + 1))
    print("Sorting", lyst)
    sort(lyst, comps, swaps)
    print("Result", lyst, "Comps:", str(comps), "Swaps:", str(swaps))
    comps.reset()
    swaps.reset()
    lyst = getRandomList(n)
    print("Sorting", lyst)
    sort(lyst, comps, swaps)
    print("Result", lyst, "Comps:", str(comps), "Swaps:", str(swaps))
Exemple #3
0
def testWithCounter(fib):
    """Runs some tests on a fib function."""
    counter = Counter()
    for n in range(1, 11):
        counter.reset()
        print("n:", n, "fib(n):", fib(n, counter), " Number of calls:",
              str(counter))
def profileFib(fibFunction, upper = 20):
    """Displays counts of calls and running times
    of the given fib function on several values of n.
    Arguments: the fib function and the upper bound
    on n.  n increases by 1 on each iteration."""
    n = 1
    counter = Counter()
    print(" n     fib(n)     # Call  Running Time (sec)")
    for i in range(upper):
        counter.reset()
        t1 = time.time()
        result = fibFunction(n, counter)
        t2 = time.time() - t1
        print("%3d%8d%12s%14.6f" % (n, result, counter, t2))
        n += 1 
Exemple #5
0
 def __init__(self, num_players, kingdom_cards):
     self.supply = Counter()
     self.num_players = num_players
     self.players = []
     self.trash = []
     
     for card in kingdom_cards:
         self.supply[card] = 10
     
     self.supply[copper] = 60 - 7*self.num_players
     self.supply[silver] = 40
     self.supply[gold] = 30
     self.supply[estate] = 24 - 3*self.num_players
     self.supply[duchy] = 8 if self.num_players<=2 else 12
     self.supply[province] = 8 if self.num_players<=2 else 12
     self.supply[curse] = 10*(self.num_players - 1)
    def __clusterize(self, bows):
        freqdist = Counter()
        bowids_by_w = deft(set)
        for bowid, bow in enumerate(bows):
            for w in bow:
                bowids_by_w[w].add(bowid)
                freqdist[w] += 1
        covered = set([])
        rankdist = freqdist.most_common()
        clusters = [-1 for _ in bows]
        features = [None for _ in bows]
        clusterid = 0

        if self._features:
            lower_boundary = min(
                [self._freqdist[_feature] for _feature in self._features])
        else:
            lower_boundary = None
        while rankdist and len(covered) < len(bows):
            feature, frequency = rankdist.pop(0)
            if lower_boundary and self._freqdist[feature] > lower_boundary:
                continue
            elif frequency / float(len(self._texts)) >= 0.66:
                continue
            bowids = bowids_by_w[feature]
            unique_bowids = bowids - covered
            if len(unique_bowids) / float(len(bowids)) < 0.5:
                continue
            for bowid in unique_bowids:
                clusters[bowid] = clusterid
                features[bowid] = feature
            covered.update(unique_bowids)
            clusterid += 1
            if clusterid == MAXIMUM_DEGREE - 1:
                break

        for i, cl in enumerate(clusters):
            if cl == -1:
                clusters[i] = MAXIMUM_DEGREE - 1

        return clusters, features
Exemple #7
0
def words_to_freq(words):
    freq = Counter()
    for word in words:
        freq[word] += 1
    return freq
    def split(self):

        self.active = False
        if len(self._texts) < MINIMUM_DOCUMENTS_IN_CLUSTER + 1:
            return [self]

        l = len(self)

        # Extract features
        max_freq = self.__set_max_freq(l)

        freqdist = Counter()
        _bows = []
        for text in self._texts:
            tokens = set([
                w.lower() for w in tokenizer(text)
                #                 if w.isalpha() or [
                #                     char for char in w if not char.isalpha()
                #                 ] == ['_']
            ])
            tokens -= self._consumed
            _bows.append(tokens)
            for w in tokens:
                freqdist[w] += 1 * (1 / float(len(w)))
        vocab = set([
            w for w, freq in freqdist.items() if freq < max_freq
            #             and freq > round(math.log(l, 2.5))
        ])

        bows = []
        for _bow in _bows:
            bow = [w for w in _bow if w in vocab]
            bows.append(bow)

        clusters, features = self.__clusterize(bows)
        if not self._tree:
            self._consumed.update(
                [w for w, freq in freqdist.items() if freq > max_freq])
        self._consumed.update(features)

        clustered = list(zip(clusters, features, self._texts))
        clusters = deft(list)
        cluster_feature = dict([])
        for clid, feature, text in clustered:
            clusters[clid].append(text)
            cluster_feature[clid] = feature

        outgoing_clusters, catchall_cluster = [], []
        available_clids = set([])
        for clid, texts in clusters.items():
            if len(texts) < MINIMUM_DOCUMENTS_IN_CLUSTER \
            or not cluster_feature[clid]:
                catchall_cluster += texts
                available_clids.add(clid)
                continue
            outgoing_cluster = Cluster(self._freqdist,
                                       cp(self._tree) + [clid], texts)
            outgoing_cluster._consumed = cp(self._consumed)
            outgoing_cluster._features = \
                cp(self._features) + [cluster_feature[clid]]
            outgoing_clusters.append(outgoing_cluster)

        if catchall_cluster:
            fallback_clid = min(available_clids)
            __tree = cp(self._tree) + [fallback_clid]

            outgoing_cluster = Cluster(self._freqdist, __tree,
                                       catchall_cluster)
            outgoing_cluster._consumed = cp(self._consumed)
            outgoing_cluster._features = cp(self._features) + ['*']
            outgoing_cluster._type = 'catch_all'
            outgoing_clusters.append(outgoing_cluster)

        if len(outgoing_clusters) == 1:
            outgoing_clusters[0].disable()

        return outgoing_clusters