# mode_finder.py -- V.1.0, Feb 14 2007, by leonardo maffi. def mode_finder(seq, verbose=False): """mode_finder(data, verbose=False): return the most frequent element of an iterable seq (the mode). This algorithm works only if freq(mode) > ndata/2 """ seq = iter(seq) candidate = seq.next() count = 1 if verbose: print "Inside mode_finder, verbose=True:" print (candidate, count), for el in seq: # The first one is already used if el == candidate: count += 1 else: count -= 1 if count == 0: candidate = el count = 1 if verbose: print (candidate, count), if verbose: print print "Mode:", candidate print "count:", count print "End of mode_finder." return candidate if __name__ == "__main__": from random import choice, shuffle, seed def gen_data(ndata=20, verbose=False): values = "abcdefgh" data = [values[0]] * (ndata // 2 + 1) if verbose: print data, "\n" data += [choice(values[1:]) for _ in xrange(ndata-len(data))] if verbose: print data, "\n" shuffle(data) return "".join(data) seed(2) data = gen_data(verbose=True) print "data:", data, "\n" m = mode_finder(data, verbose=True)