|
Download tasks/program-execution-tracing/contexts/sample-00003.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 391 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00003.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00003.md
-
curl -L -o sample-00003.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00003.md
391 kB
| ### 001 | |
| ```python | |
| def f1(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap and count > 0: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.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 matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f1([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| 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, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f2([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {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) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| handled.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 initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f3(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(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(min(count, 64)): | |
| 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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f4([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(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: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| startdegree = dict(needs) | |
| complete = set() | |
| result = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f5(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(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[: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([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f6([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.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 combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f7([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(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[:64]) 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f8([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {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) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = 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 nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f9(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(nodes, edges): | |
| nodes = list(nodes) | |
| 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() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f10(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| 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(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f11([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f12([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(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) < 2: | |
| continue | |
| frequency[word] = min(9, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f13(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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 and (limit >= 0 or pos > 0)] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f14([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(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[:64]: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = 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 ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f15(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not result or left > result[-1][1] + max(0, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f16([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(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() | |
| result = [] | |
| layers = [] | |
| 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), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f17(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(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 | |
| startdegree = dict(indegree) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f18(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(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: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f19([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f20(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f21(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(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) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[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 nextmap[node]: | |
| incoming[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f22(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f23([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f24([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) 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: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f25(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(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 or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = 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 ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f26(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(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 % 100): | |
| x += dx | |
| y = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f27([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(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 % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f28([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f29(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f30([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(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) if value > limit] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f31([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f32(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(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: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f33([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.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 | |
| pending.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f34([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f35(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f36(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(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] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f37([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(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: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f38(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(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])) | |
| 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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f39([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| queue.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f40([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(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[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f41([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(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 = max(-64, min(64, 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 = (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': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f42([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f43(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f44(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f45([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f46([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(nodes, edges): | |
| nodes = list(nodes) | |
| 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 | |
| initial = dict(incoming) | |
| complete = set() | |
| result = [] | |
| groups = [] | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f47(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| 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, []))[:20]: | |
| if nxt not in distance: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f48([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(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] < min(limit, 3): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f49([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(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] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f50([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {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) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| 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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f51(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(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] | |
| ups = 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': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f52([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f53([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f54([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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 | |
| initial = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| 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), 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 nextmap[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f55(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f56([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f57([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(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': | |
| 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) | |
| 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': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f58(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f59([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(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] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f60([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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])) | |
| positions = [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': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f61([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(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} | |
| 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) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| 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[: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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f62(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(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 = min(64, 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 = (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 | |
| ``` | |
| Example call: | |
| ``` | |
| f63([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(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) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f64([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(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 += dx | |
| y = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f65([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(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 | |
| 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)) | |
| 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]: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f66(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f67(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.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 items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f68([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(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, min(65, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f69([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(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]))) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f70([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f71([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(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 = max(-64, min(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f72([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(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 and count > 0: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f73([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(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] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f74([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(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])) | |
| leftover = 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': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f75([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(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() | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f76(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f77(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f78([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f79(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(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, min(50, len(segments))): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((min(100, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f80([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| 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 | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f81(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 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 | |
| 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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f82([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(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[: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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f83(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(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 | |
| ordered = 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 ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f84(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(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' 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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f85(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f86([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(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 | |
| initial = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f87(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(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[: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([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f88([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| 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])) | |
| absent = 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': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f89([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f90([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(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 = 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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f91([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f92([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(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: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = 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 ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f93(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f94([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(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: | |
| continue | |
| frequency[word] = min(9, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f95(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f96(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 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) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| 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[: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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f97(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"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(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) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f98(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(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) | |
| finished = set() | |
| sequence = [] | |
| 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) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f99(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f100(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f101([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| 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 or len(word) > 12: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = 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 ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f102(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(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: | |
| 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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f103([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f104([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f105(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {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) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| 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), 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 nextmap[node]: | |
| indegree[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f106(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(nodes, edges): | |
| nodes = list(nodes) | |
| 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) | |
| complete = set() | |
| result = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f107(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(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] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f108([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f109(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(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 % 5: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f110([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 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 | |
| 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 = 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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f111([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(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 = 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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f112([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(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 = [] | |
| 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 nextmap[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f113(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(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] < limit: | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f114([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(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} | |
| 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) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[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]: | |
| indegree[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f115(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(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 = (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 | |
| ``` | |
| Example call: | |
| ``` | |
| f116([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(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 % 100): | |
| x = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f117([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(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])) | |
| indexes = [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': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f118([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(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 | |
| startdegree = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[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 nextmap[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f119(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(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: | |
| dx, dy = step[direction] | |
| for turn in range(count): | |
| x = max(-64, min(64, x + dx)) | |
| 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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f120([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| 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) | |
| edge = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f121([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap % 5 + 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f122([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(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((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 | |
| ``` | |
| Example call: | |
| ``` | |
| f123([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f124([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(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: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| result = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f125(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f126(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(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 = [] | |
| 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) | |
| 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 initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f127(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(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 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = 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 available: | |
| break | |
| current = available | |
| 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 initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f128(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(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() | |
| result = [] | |
| 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 | |
| layers.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': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f129(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(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: | |
| 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 | |
| tallies[word] = 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': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f130(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) 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: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = 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 ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f131(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f132([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + max(0, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f133([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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: | |
| 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f134(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f135(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 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 | |
| known = {(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 known and list(point) not in repeats: | |
| repeats.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': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f136([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(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 % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f137([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(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) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f138([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(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) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < limit: | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f139([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(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] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| result = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f140([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(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(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f141([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(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) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f142([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(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])) | |
| 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 ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f143(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.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(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f144([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f145([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f146([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f147(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(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 += 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f148([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(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(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f149([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f150(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(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': min(256, max(windows)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f151([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(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 += 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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f152([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f153(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f154([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f155([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(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} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| 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[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f156([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(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 += 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f157([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f158([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(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])) | |
| 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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f159([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f160([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(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) | |
| steps = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f161([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(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((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [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': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f162([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| 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])) | |
| leftover = 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': leftover, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f163([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| 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[: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 | |
| ``` | |
| Example call: | |
| ``` | |
| f164(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f165(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f166([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(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) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| 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]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f167(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f168(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(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 {} | |
| 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) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': edge[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f169([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(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} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f170([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges[:16]: | |
| routes.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(routes.get(cur, [])) if len(str(node)) == 1): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f171([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f172(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(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 = max(-64, min(64, x + dx)) | |
| 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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f173([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 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)} | |
| 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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f174([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f175([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(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, 64)): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f176([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(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 {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in distance: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f177([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(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[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f178([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f179(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(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' 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 or len(word) < 2: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f180(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() 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 | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = 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 ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f181(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(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(0, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f182([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {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) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| 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 nextmap[node]: | |
| indegree[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f183(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f184(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(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) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f185([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f186([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(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[: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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f187(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + max(0, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f188([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f189([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors or rate0 <= 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])) | |
| 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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f190([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f191([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| if not result or left > result[-1][1] + gap % 5: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f192([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(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 = 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 = (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 | |
| ``` | |
| Example call: | |
| ``` | |
| f193([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(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[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f194([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.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 items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f195([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f196([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f197(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f198(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f199([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f200(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f201([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(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): | |
| 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f202(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| nextmap.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(nextmap.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f203([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(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[:64]: | |
| if word in stop: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f204(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(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 = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f205([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f206(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f207(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 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] = 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': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f208([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.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 combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f209([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f210(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f211([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(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 | |
| initial = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[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 | |
| layers.append(current) | |
| for node in current: | |
| handled.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f212(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(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 | |
| rootsource = dict(needs) | |
| complete = set() | |
| listing = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| complete.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 rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f213(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(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) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| 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 initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f214(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(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) | |
| 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)) | |
| 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 nextmap[node]: | |
| incoming[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f215(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f216([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| 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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f217([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f218([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(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 and (limit >= 0 or pos > 0)] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f219([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| 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])) | |
| absent = 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': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f220([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(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 += 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f221([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(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]))) | |
| matches = [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([0] + totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f222([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(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] | |
| 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': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f223([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(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 % 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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f224([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| 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, []))[:20]: | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| queue.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f225([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f226(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.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[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f227([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(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(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f228([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(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: | |
| 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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f229(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(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} | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f230([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(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[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f231(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(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) | |
| 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=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]: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f232(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(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(0, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f233([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(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])) | |
| 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([0] + series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f234([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f235([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 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: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f236([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(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] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f237([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f238(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(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' 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 or len(word) < 2: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f239(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f240([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(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': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f241([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 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' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f242(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {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) | |
| needs[b] += 1 | |
| rootsource = 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 children[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f243(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f244(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(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 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f245(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(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] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f246([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(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, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f247([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(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 | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f248([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f249([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f250([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(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) | |
| gaps = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f251([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f252(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(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] = min(9, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f253(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f254(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, 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] | |
| increases = 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': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f255([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(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: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f256(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f257(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(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] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f258([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(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(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f259([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(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] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f260([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + min(gap, 2): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f261([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| 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, [])): | |
| if nxt not in distance: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f262([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(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 = max(-64, min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f263([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f264(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f265([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(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] + gap % 5: | |
| 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(min(100, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f266([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(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: | |
| 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(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f267([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f268(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.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 matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f269([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f270([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(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) | |
| breaks = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f271([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels and (not nxt.startswith('Z')): | |
| levels[nxt] = levels[cur] + 1 | |
| pending.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f272([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(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): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f273([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f274(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(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 | |
| startdegree = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| 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 | |
| groups.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': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f275(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(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) 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f276([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f277([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(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 | |
| rootsource = 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 rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f278(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(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 or len(word) > 12: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f279(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(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) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f280([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(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) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f281([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f282(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 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: | |
| 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 known and list(point) not in crossings: | |
| crossings.append(list(point)) | |
| known.add(point) | |
| far = max(far, min(20, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f283([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f284(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| 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[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f285([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(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 % 100): | |
| 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 = (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 | |
| ``` | |
| Example call: | |
| ``` | |
| f286([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f287([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f288(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(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] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f289([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(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(min(count, 64)): | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f290([('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[0,-6,10,0],"end":[7,-3],"far":16,"revisits":[[4,0],[3,0],[2,0],[4,-1]],"steps":38} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(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) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in distance: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f291([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(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[: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(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f292([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f293(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(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((min(100, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f294([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f295([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":39,"holes":[7,7,4,5],"longest":11,"segments":[[3,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(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 = 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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f296([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(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] < 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f297([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(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: | |
| dx, dy = step[direction] | |
| for turn in range(count % 100): | |
| x = max(-64, min(64, 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, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f298([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f299(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f300([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(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])) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f301([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(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() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f302(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(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] | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f303([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(routes.get(cur, []))[:20]: | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| work.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 | |
| ``` | |
| Example call: | |
| ``` | |
| f304([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(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) | |
| 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 children[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 | |
| ``` | |
| Example call: | |
| ``` | |
| f305(['api', 'jobs', 'db', 'ui', 'proxy', 'cache', 'auth', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache","db"],["api","jobs","proxy"],["ui","worker"]],"count":8,"order":["auth","cache","db","api","jobs","proxy","ui","worker"],"roots":["auth","cache","db"]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(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 % 100): | |
| x += dx | |
| y += dy | |
| used = min(64, 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f306([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(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 | |
| ``` | |
| Example call: | |
| ``` | |
| f307([(3, 5), (6, 8), (15, 19), (19, 22), (29, 35), (34, 40), (44, 50), (49, 55), (60, 65)], 0) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":38,"holes":[1,7,7,4,5],"longest":11,"segments":[[3,5],[6,8],[15,22],[29,40],[44,55],[60,65]]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f308(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(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': max(series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f309([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 8, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":147,"hits":[0,1,2,3,4,5,6,7],"rises":1,"sums":[147,147,140,138,135,128,125,131]} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(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])) | |
| 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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f310([('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["AN",30]],"missing":[],"total":316} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(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: | |
| 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 or len(word) < 2: | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f311(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(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 = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f312([('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1), ('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3)]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"bounds":[-1,-4,7,0],"end":[4,-1],"far":11,"revisits":[[4,0],[3,0],[2,0],[1,0],[0,0]],"steps":39} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f313([('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6), ('FP', 11), ('GQ', 5), ('HR', 4)], [('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5), ('FP', 5), ('GQ', 8), ('HR', 4)], 2) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":8,"items":[["DL",98],["FP",57],["CX",42],["GQ",42],["EV",32]],"missing":[],"total":331} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f314(['####..', '#..#..', '..#.#.', '..###.', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[2,2],[0,0],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f315(['####..', '#..##.', '..#.#.', '..##..', '##.#.#', '#..##.']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[0,0],[2,2],[4,0],[4,5]],"count":4,"filled":18,"sizes":[8,6,3,1]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| 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) | |
| 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f316([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'G', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":7,"depth":{"A":1,"B":2,"C":2,"D":3,"E":3,"F":3,"G":0},"frontier":["D","E","F"],"order":["G","A","B","C","D","E","F"]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(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 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f317([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'B', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":5,"depth":{"A":3,"B":0,"D":1,"E":1,"G":2},"frontier":["A"],"order":["B","D","E","G","A"]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(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' 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 | |
| frequency[word] = 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 | |
| ``` | |
| Example call: | |
| ``` | |
| f318(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['helium']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"cyan":5,"delta":4,"ember":5,"frost":3,"garnet":3},"initials":{"a":5,"b":3,"c":5,"d":4,"e":5,"f":3,"g":3},"size":135,"top":["amber","cyan","ember","delta","blue"]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| 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[: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 | |
| ``` | |
| Example call: | |
| ``` | |
| f319(['Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber', 'Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost'], ['cyan']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":5,"blue":3,"delta":4,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":5,"b":3,"d":4,"e":5,"f":3,"g":3,"h":3},"size":133,"top":["amber","ember","delta","blue","frost"]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(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] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f320([22, 20, 22, 21, 21, 16, 15, 10, 22, 13, 20, 18, 14, 13, 21], 1, 80) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":22,"hits":[],"rises":4,"sums":[22,20,22,21,21,16,15,10,22,13,20,18,14,13,21]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| nodes = list(('api', 'jobs', 'db', 'ui', 'proxy') + ('cache', 'auth', 'worker')) | |
| edges = list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'))) | |
| 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)} | |
| route = record['aa'][0] | |
| cases = { | |
| 'auth': list((('E', 3), ('E', 2), ('W', 3), ('S', 2), ('E', 2), ('N', 2), ('S', 1)) + (('E', 3), ('E', 2), ('E', 1), ('S', 5), ('W', 3), ('W', 3), ('N', 3), ('E', 3))), | |
| 'cache': list((('E', 3), ('E', 2), ('W', 6), ('S', 2), ('E', 2), ('N', 2), ('S', 1)) + (('E', 3), ('E', 2), ('E', 1), ('S', 3), ('W', 3), ('W', 3), ('N', 3), ('E', 3))), | |
| } | |
| 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} | |
| lines = list(('Frost, amber. blue garnet', 'Cyan, ember. garnet delta amber') + ('Delta, cyan. helium ember', 'Helium, ember. delta ember helium', 'Amber, frost. blue cyan', 'Garnet, cyan. blue delta amber', 'Amber, ember. cyan frost')) | |
| route = record['bb'] | |
| cases = { | |
| 16: ['cyan'], | |
| 11: ['helium'], | |
| } | |
| 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))} | |
| edges = list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'))) | |
| limit = 3 | |
| route = record['dd'] | |
| cases = { | |
| 135: 'G', | |
| 133: 'B', | |
| } | |
| start = 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)} | |
| spans = list(((3, 5), (6, 8), (15, 19), (19, 22), (29, 35)) + ((34, 40), (44, 50), (49, 55), (60, 65))) | |
| route = record['dd'] | |
| cases = { | |
| 5: 0, | |
| 7: 1, | |
| } | |
| gap = 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))} | |
| route = record['cc'] | |
| cases = { | |
| 39: list(('####..', '#..##.', '..#.#.') + ('..##..', '##.#.#', '#..##.')), | |
| 38: list(('####..', '#..#..', '..#.#.') + ('..###.', '##.#.#', '#..##.')), | |
| } | |
| board = cases[route] | |
| cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] | |
| parent = {cell: cell for cell in cells} | |
| def root(cell): | |
| while parent[cell] != cell: | |
| parent[cell] = parent[parent[cell]] | |
| cell = parent[cell] | |
| return cell | |
| def merge(a, b): | |
| a = root(a) | |
| b = root(b) | |
| if a != b: | |
| parent[b] = a | |
| for row, col in cells: | |
| for other in ((row - 1, col), (row, col - 1)): | |
| if other in parent: | |
| merge((row, col), other) | |
| groups = {} | |
| for cell in cells: | |
| groups.setdefault(root(cell), []).append(cell) | |
| parts = [(len(group), min(group)) for group in groups.values()] | |
| parts.sort(key=lambda item: (-item[0], item[1])) | |
| record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} | |
| values = list((22, 20, 22, 21, 21, 16) + (15, 10, 22, 13, 20, 18, 14, 13, 21)) | |
| limit = 80 | |
| route = record['bb'][0][0] | |
| cases = { | |
| 2: 1, | |
| 0: 8, | |
| } | |
| width = 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} | |
| right = list((('AN', 7), ('BR', 5), ('CX', 5), ('DL', 8), ('EV', 5)) + (('FP', 5), ('GQ', 8), ('HR', 4))) | |
| fee = 2 | |
| route = record['bb'] | |
| cases = { | |
| 22: list((('AN', 4), ('BR', 2), ('CX', 8), ('DL', 12), ('EV', 6)) + (('FP', 11), ('GQ', 5), ('HR', 4))), | |
| 147: list((('AN', 4), ('BR', 3), ('CX', 8), ('DL', 12), ('EV', 2)) + (('FP', 11), ('GQ', 5), ('HR', 4))), | |
| } | |
| 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)} | |
| out['aa'] = record['aa'] | |
| out['cc'] = record['cc'] | |
| out['bb'] = record['bb'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |