|
Download tasks/program-execution-tracing/contexts/sample-00041.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 379 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00041.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00041.md
-
curl -L -o sample-00041.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00041.md
379 kB
| ### 001 | |
| ```python | |
| def f1(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f1([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value > limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f3([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f4(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f6([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f9([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f10([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f13([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f14(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| handled = set() | |
| result = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f16(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f18([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value > limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f19([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f20([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f21([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f24(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f25([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap and count > 0: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f28(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f30(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f31([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f33(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(segments))): | |
| left = segments[pos][0] | |
| gaps.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f35([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value > limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f37([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f40([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f41([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f42(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f43([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f44([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f45(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(series))) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f49([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f50([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| listing = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f54([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f56(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f57([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| result = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f59(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f61([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f62([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f63(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f64([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f65([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f66([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f67([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f69([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f71([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f72([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f73([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f74(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(prices[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f76([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f77([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(ratemap[group]) | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f78([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f80([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(ratemap[group]) | |
| matches.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| grand += charge | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': groupmap, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f81([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f82([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f83([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f84([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f86([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f87([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f88(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f89([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f90([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5 + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f93([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f95(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f96([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f97(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f99(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f100([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f102([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f103([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| kept.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f106([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f107([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f108(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f109([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f110([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f111([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value > limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f112([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f114([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| listing = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f115(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f116([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f117(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f118(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f119([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| result = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f120([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f121(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f123(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f124([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f126(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f127([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f128(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| listing = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f130(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f132([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f134([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * ratemap[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f137(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f138([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f141([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f142([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f143([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f144([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f146([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f148(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(segments))): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f149([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f150([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * abs(ratemap[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f151([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f152([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f153([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * ratemap[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f156(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + max(0, gap) + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f157([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f158([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f159([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f160([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f162([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f163([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f165(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + min(gap, 2): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f167([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f168(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f170([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f172([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f173([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f174([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| handled = set() | |
| result = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f177([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f178([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f179(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f181([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors and rate0 != 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f182([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| handled = set() | |
| listing = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f184([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f185([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f186([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f188([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f189([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f190([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f193(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f194([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f196([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f197([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals[:64]) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f199([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f200(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(ratemap[group]) | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f202([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f203([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f205(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f208([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) < 2: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f211([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f212(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f213([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f214(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f215([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f216([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f218([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f219([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value > limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f221([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f222([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f224(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f226([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f228([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f229(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f230([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| startdegree = dict(needs) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f232(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f233([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f236([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f237([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f238([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f239([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f240([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(factors[group]) | |
| matches.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f241([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f242(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f243([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f244([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f245([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f246(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| result = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f247([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f248([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f250(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f251(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f252([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f253([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in visited and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f255(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f256([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f258([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f259([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.append(nxt) | |
| result = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f263([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| found.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f264([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f265([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f266([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":2,"H":3},"frontier":["H"],"order":["A","B","C","D","E","G","F","H"]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in returns: | |
| returns.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f267([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f268([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f269([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f270([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * ratemap[group] | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f272([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f273([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(ratemap[group]) | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f274([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f275([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f276(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * ratemap[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f277([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 3 + (count - count))): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f279([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| repeats = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in repeats: | |
| repeats.append(list(point)) | |
| visited.add(point) | |
| far = min(64, max(far, abs(x) + abs(y))) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f280([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f282([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x += dx | |
| y = max(-64, min(64, y + dy)) | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f283([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f284(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| known = {(0, 0)} | |
| crossings = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f285([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f286([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(ratemap[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f287([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f288([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f289([('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4), ('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",100],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":233} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f291([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| result = list(levels) | |
| lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': result, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f294([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f296(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":5,"groups":{"core":264,"edge":18,"north":154,"south":25},"top":["AN28","AN98","BR39"],"total":461} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + max(0, gap) + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298([(3, 5), (6, 13), (15, 24), (19, 25), (29, 32), (34, 40), (44, 47), (49, 54), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":46,"holes":[4,4,6],"longest":22,"segments":[[3,25],[29,40],[44,54],[60,63]]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f299(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f300([(3, 5), (6, 13), (15, 21), (19, 25), (29, 32), (34, 40), (44, 47), (49, 56), (60, 63)], 2) | |
| ``` | |
| ``` | |
| {"cover":48,"holes":[4,4,4],"longest":22,"segments":[[3,25],[29,40],[44,56],[60,63]]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f301([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f304([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * factors[group] | |
| selected.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| startdegree = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f306(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f307(['db', 'proxy', 'auth', 'jobs', 'api', 'worker', 'ui', 'cache'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f308(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309([13, 6, 14, 10, 9, 10, 22, 27, 17, 4, 10, 21, 15, 20, 4], 3, 38) | |
| ``` | |
| ``` | |
| {"best":66,"hits":[4,5,6,7,10,11,12],"rises":7,"sums":[33,30,33,29,41,59,66,48,31,35,46,56,39]} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['ember', 'frost']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"blue":3,"cyan":3,"delta":6,"garnet":5,"helium":2},"initials":{"a":4,"b":3,"c":3,"d":6,"g":5,"h":2},"size":116,"top":["delta","garnet","amber","blue","cyan"]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| visited = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| y = max(-64, min(64, y + dy)) | |
| used += 1 | |
| point = (x, y) | |
| if point in visited and list(point) not in returns: | |
| returns.append(list(point)) | |
| visited.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f311([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f312([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f313([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used += 1 | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f314([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-1,-4],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":31} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f315(['Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta', 'Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan'], ['blue', 'cyan']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":4,"delta":6,"ember":2,"frost":6,"garnet":5,"helium":2},"initials":{"a":4,"d":6,"e":2,"f":6,"g":5,"h":2},"size":132,"top":["delta","frost","garnet","amber","ember"]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E')], 'A', 3) | |
| ``` | |
| ``` | |
| {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317([('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10), ('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":3,"groups":{"core":33,"north":203},"top":["DL61","DL17","DL54"],"total":236} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f318([('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4), ('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3)], [('AN', 8), ('BR', 3), ('EV', 3), ('FP', 10), ('GQ', 3), ('HR', 8)], 0) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["FP",120],["AN",64],["HR",24],["BR",18],["EV",15]],"missing":["CX","DL"],"total":253} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f319([13, 10, 14, 10, 9, 10, 22, 22, 17, 4, 10, 21, 15, 20, 4], 3, 39) | |
| ``` | |
| ``` | |
| {"best":61,"hits":[4,5,6,7,10,11,12],"rises":6,"sums":[37,34,33,29,41,54,61,43,31,35,46,56,39]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(moves): | |
| moves = list(moves) | |
| step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| x = 0 | |
| y = 0 | |
| found = {(0, 0)} | |
| returns = [] | |
| far = 0 | |
| lowx = highx = lowy = highy = 0 | |
| used = 0 | |
| for direction, count in moves[:20]: | |
| dx, dy = step[direction] | |
| for turn in range(min(count, 64)): | |
| x += dx | |
| y += dy | |
| used = min(64, used + 1) | |
| point = (x, y) | |
| if point in found and list(point) not in returns: | |
| returns.append(list(point)) | |
| found.add(point) | |
| far = max(far, abs(x) + abs(y)) | |
| lowx, highx = (min(lowx, x), max(highx, x)) | |
| lowy, highy = (min(lowy, y), max(highy, y)) | |
| returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f320([('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1), ('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2)]) | |
| ``` | |
| ``` | |
| {"bounds":[-2,-4,2,2],"end":[-2,-2],"far":6,"revisits":[[1,-3],[0,-3],[-1,-1],[-2,2],[-2,1]],"steps":32} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| rows = list((('DL17', 'north', 'checked', 14), ('DL54', 'core', 'checked', 3), ('DL91', 'south', 'void', 6), ('AN28', 'core', 'ready', 14), ('DL65', 'edge', 'ready', 6), ('DL02', 'north', 'void', 11), ('BR39', 'core', 'ready', 10)) + (('CX76', 'core', 'void', 22), ('DL13', 'south', 'hold', 21), ('BR50', 'edge', 'hold', 4), ('BR87', 'south', 'ready', 5), ('AN24', 'core', 'void', 3), ('DL61', 'north', 'checked', 15), ('AN98', 'north', 'ready', 22))) | |
| keep = ['checked'] | |
| rates = dict((('north', 7), ('south', 5)) + (('core', 11), ('edge', 3))) | |
| chosen = [(code, group, value) for code, group, state, value in rows if state in keep] | |
| priced = [(code, group, value * rates[group]) for code, group, value in chosen] | |
| groups = {} | |
| for code, group, price in priced: | |
| groups[group] = groups.get(group, 0) + price | |
| ranked = sorted(priced, key=lambda row: (-row[2], row[0])) | |
| record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} | |
| token = record['bb'] | |
| route = token | |
| cases = { | |
| 461: list((('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1)) + (('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('N', 2))), | |
| 236: list((('W', 1), ('S', 3), ('E', 3), ('W', 1), ('W', 1), ('N', 2), ('W', 1)) + (('W', 1), ('N', 3), ('E', 1), ('W', 1), ('S', 3), ('E', 3), ('W', 3), ('S', 3), ('E', 1))), | |
| } | |
| moves = cases[route] | |
| delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} | |
| points = [[0, 0]] | |
| for direction, count in moves: | |
| dx, dy = delta[direction] | |
| for turn in range(count): | |
| points.append([points[-1][0] + dx, points[-1][1] + dy]) | |
| seen = set() | |
| revisits = [] | |
| for point in points: | |
| key = tuple(point) | |
| if key in seen and point not in revisits: | |
| revisits.append(point) | |
| seen.add(key) | |
| record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} | |
| memo = record['ee'] | |
| gap = 2 | |
| route = token | |
| cases = { | |
| 236: list(((3, 5), (6, 13), (15, 24), (19, 25), (29, 32)) + ((34, 40), (44, 47), (49, 54), (60, 63))), | |
| 461: list(((3, 5), (6, 13), (15, 21), (19, 25), (29, 32)) + ((34, 40), (44, 47), (49, 56), (60, 63))), | |
| } | |
| spans = cases[route] | |
| spans.sort(key=lambda pair: (pair[0], pair[1])) | |
| parts = [] | |
| pos = 0 | |
| while pos < len(spans): | |
| left, right = spans[pos] | |
| pos += 1 | |
| while pos < len(spans) and spans[pos][0] <= right + gap: | |
| right = max(right, spans[pos][1]) | |
| pos += 1 | |
| parts.append([left, right]) | |
| holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] | |
| record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} | |
| clue = record['cc'] | |
| width = 3 | |
| options = { | |
| 236: ('near', memo), | |
| 461: ('far', clue), | |
| } | |
| route = options[token] | |
| cases = { | |
| ('near', 31): (list((13, 6, 14, 10, 9, 10, 22) + (27, 17, 4, 10, 21, 15, 20, 4)), 38), | |
| ('near', 32): (list((13, 10, 14, 10, 9, 10, 22) + (22, 17, 4, 10, 21, 15, 20, 4)), 39), | |
| ('far', 46): (list((13, 10, 14, 10, 9, 10, 22) + (22, 17, 4, 10, 21, 15, 20, 4)), 39), | |
| ('far', 48): (list((13, 10, 14, 10, 9, 10, 22) + (22, 17, 4, 10, 21, 15, 20, 4)), 39), | |
| } | |
| values, limit = cases[route] | |
| current = sum(values[:width]) | |
| sums = [current] | |
| for pos in range(width, len(values)): | |
| current += values[pos] - values[pos - width] | |
| sums.append(current) | |
| hits = [] | |
| rises = 0 | |
| for pos, value in enumerate(sums): | |
| if value >= limit: | |
| hits.append(pos) | |
| if pos and value > sums[pos - 1]: | |
| rises += 1 | |
| record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} | |
| stamp = record['bb'] | |
| nodes = list(('db', 'proxy', 'auth') + ('jobs', 'api', 'worker', 'ui', 'cache')) | |
| route = clue | |
| cases = { | |
| 46: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth'))), | |
| 48: list((('auth', 'api'), ('db', 'api'), ('cache', 'api')) + (('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('cache', 'db'))), | |
| } | |
| edges = cases[route] | |
| remaining = set(nodes) | |
| done = set() | |
| order = [] | |
| batches = [] | |
| while remaining: | |
| ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node)))) | |
| if not ready: | |
| break | |
| batches.append(ready) | |
| order.extend(ready) | |
| done.update(ready) | |
| remaining.difference_update(ready) | |
| roots = sorted((node for node in nodes if all((after != node for before, after in edges)))) | |
| record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)} | |
| ticket = record['aa'][0] | |
| lines = list(('Garnet, blue. delta helium', 'Cyan, garnet. frost amber amber', 'Frost, delta. cyan frost', 'Amber, ember. blue frost delta') + ('Frost, blue. garnet frost', 'Garnet, delta. delta ember helium', 'Delta, amber. garnet cyan')) | |
| route = (stamp, ticket) | |
| cases = { | |
| (61, 'auth'): list(('ember',) + ('frost',)), | |
| (61, 'cache'): list(('ember',) + ('frost',)), | |
| (66, 'auth'): list(('ember',) + ('frost',)), | |
| (66, 'cache'): list(('blue',) + ('cyan',)), | |
| } | |
| stop = cases[route] | |
| normalized = set() | |
| for item in stop: | |
| normalized.add(item.lower()) | |
| stop = normalized | |
| words = [] | |
| for line in lines: | |
| words.extend(re.findall('[a-z]+', line.lower())) | |
| words = [word for word in words if word not in stop] | |
| counts = {} | |
| initials = {} | |
| for word in words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| initials[word[0]] = initials.get(word[0], 0) + 1 | |
| top = sorted(counts, key=lambda word: (-counts[word], word))[:5] | |
| record = {'aa': top, 'bb': counts, 'cc': initials, 'dd': sum((len(word) for word in words))} | |
| badge = record['dd'] | |
| right = list((('AN', 8), ('BR', 3), ('EV', 3)) + (('FP', 10), ('GQ', 3), ('HR', 8))) | |
| fee = 0 | |
| route = badge | |
| cases = { | |
| 132: list((('AN', 8), ('BR', 6), ('CX', 14), ('DL', 4)) + (('EV', 5), ('FP', 10), ('GQ', 4), ('HR', 3))), | |
| 116: list((('AN', 8), ('BR', 6), ('CX', 10), ('DL', 4)) + (('EV', 5), ('FP', 12), ('GQ', 4), ('HR', 3))), | |
| } | |
| left = cases[route] | |
| joined = [] | |
| for key, count in left: | |
| matches = [rate for name, rate in right if name == key] | |
| if matches: | |
| joined.append([key, count * matches[0] + fee]) | |
| joined.sort(key=lambda item: (-item[1], item[0])) | |
| rightkeys = {key for key, rate in right} | |
| record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} | |
| permit = record['bb'] | |
| start = 'A' | |
| limit = 3 | |
| route = permit | |
| cases = { | |
| 253: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('B', 'G'))), | |
| 233: list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('D', 'E'))), | |
| } | |
| edges = cases[route] | |
| links = {} | |
| for edge in edges: | |
| links.setdefault(edge[0], []).append(edge[1]) | |
| distance = {start: 0} | |
| queue = deque([start]) | |
| while queue: | |
| node = queue.popleft() | |
| if distance[node] == limit: | |
| continue | |
| for child in sorted(links.get(node, [])): | |
| if child in distance: | |
| continue | |
| distance[child] = distance[node] + 1 | |
| queue.append(child) | |
| furthest = max(distance.values()) | |
| record = {'aa': list(distance), 'bb': distance, 'cc': sorted((node for node in distance if distance[node] == furthest)), 'dd': len(distance)} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |