def search_postfix(prefix, zeroes=5): """ >>> search_postfix('pqrstuv') 1048970 """ rex = re.compile(r'^0{'+str(zeroes)+r'}') matches = p.parmap(ft.partial(match, prefix, rex), it.count(1)) filtered = (a for a, b in matches if b) return next(filtered)
def best_seating(lookup): """ >>> best_seating(get_lookup('test.txt')) 330 """ guests = set(a for a, _ in lookup.keys()) # '<=' without loss of generality (problem is symmetric) perms = (p for p in it.permutations(guests) if p[0] <= p[-1]) happiness = p.parmap(ft.partial(calc_happiness, lookup), perms) return max(happiness)
def distances(strings): """ >>> sorted(list(d for (_, d) in distances(read_file('test.txt')))) [605, 605, 659, 659, 982, 982] """ lookup = get_lookup(strings) cities = set(a for a, _ in lookup.keys()) paths = it.permutations(cities) segss = ((p, segment(p)) for p in paths) dists = p.parmap(ft.partial(calc, lookup), segss) return list(dists)