|
Download tasks/program-execution-tracing/contexts/sample-00006.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 390 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00006.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00006.md
-
curl -L -o sample-00006.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00006.md
390 kB
| ### 001 | |
| ```python | |
| def f1(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| 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: | |
| ``` | |
| f1(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f2(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| 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) | |
| 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: | |
| ``` | |
| f3(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(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) | |
| 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: | |
| ``` | |
| f4(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f5([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(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 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=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 nextmap[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: | |
| ``` | |
| f6(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| 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) | |
| 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: | |
| ``` | |
| f7(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f8([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[: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 routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f9(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f10(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f11(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| 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: | |
| ``` | |
| f12([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.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: | |
| ``` | |
| f13([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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 {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| 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: | |
| ``` | |
| f14([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f15([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f16(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f17([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f18(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(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]))) | |
| indexes = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f19([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right if tidy(value) != ''} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f20(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| work.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, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f21([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(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 = [] | |
| 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: | |
| ``` | |
| f22(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f23(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f24(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| 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 | |
| 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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f25(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in steps and (not nxt.startswith('Z')): | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| result = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f26([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(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, min(65, 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: | |
| ``` | |
| f27([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(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: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f28(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f29([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| queue.append(nxt) | |
| result = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f30([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(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]))) | |
| matches = [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([0] + totals), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f31([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f32(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f33(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * factors[group] | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f34([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(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] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f35([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f36([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(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 + 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': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f37([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(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] = 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: | |
| ``` | |
| f38([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(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() | |
| 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 nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f39(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(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) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[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: | |
| ``` | |
| f40(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f41([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| 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) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f42(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[: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: | |
| ``` | |
| f43(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(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((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: | |
| ``` | |
| f44([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(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] + gap % 5: | |
| 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: | |
| ``` | |
| f45([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(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) | |
| 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: | |
| ``` | |
| f46(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = 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': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f47([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| 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: | |
| ``` | |
| f48(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(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 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: | |
| ``` | |
| f49(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f50(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(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) | |
| 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: | |
| ``` | |
| f51(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| 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 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) | |
| 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: | |
| ``` | |
| f52(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f53(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(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} | |
| queue = [start] | |
| while queue: | |
| cur = queue.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 | |
| queue.append(nxt) | |
| sequence = list(steps) | |
| lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f54([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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] | |
| increases = sum((1 for pos in range(1, min(65, 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: | |
| ``` | |
| f55([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(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 = [] | |
| 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) | |
| 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: | |
| ``` | |
| f56(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(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) | |
| handled = set() | |
| result = [] | |
| 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), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| 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: | |
| ``` | |
| f57(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f58([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| 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 | |
| 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, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f59([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * ratemap[group] | |
| matches.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f60([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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] < min(limit, 3): | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.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: | |
| ``` | |
| f61([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f62(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f63([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f64([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(board): | |
| board = list(board) | |
| found = 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 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 (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.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: | |
| ``` | |
| f65(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f66(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(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] < limit: | |
| for nxt in sorted(nextmap.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: | |
| ``` | |
| f67([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f68([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| 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: | |
| ``` | |
| f69([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(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]))) | |
| indexes = [pos for pos, value in enumerate(series) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f70([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(board): | |
| board = list(board) | |
| found = 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 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: | |
| ``` | |
| f71(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(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} | |
| work = [start] | |
| while work: | |
| cur = work.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 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f72([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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: | |
| ``` | |
| f73(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| banned = {canon(value) for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f74(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f75(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f76([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f77([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(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 + 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(min(100, 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: | |
| ``` | |
| f78([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges[:16]: | |
| 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, []))[:20]: | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.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: | |
| ``` | |
| f79([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(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] | |
| ups = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f80([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f81([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(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 | |
| startdegree = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| 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: | |
| ``` | |
| f82(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(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([0] + windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f83([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f84(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f85(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) 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 | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f86(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(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])) | |
| 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': min(256, max(totals)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f87([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| work.append(nxt) | |
| listing = list(steps) | |
| edge = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': listing, 'depth': steps, 'frontier': edge[:3], 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f88([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(ratemap[group]) | |
| matches.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f89([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| 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: | |
| ``` | |
| f90([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f91(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(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: | |
| ``` | |
| f92(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| listing = [] | |
| layers = [] | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else available | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f93(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(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[: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': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f94(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| complete = set() | |
| listing = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f95(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| 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 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) | |
| 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: | |
| ``` | |
| f96(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f97([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(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: | |
| 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': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f98(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(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: | |
| ``` | |
| f99(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = 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 sorteditems[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f100(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(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] | |
| growth = 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': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f101([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f102([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| 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 | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f103(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(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 | |
| rootsource = dict(needs) | |
| complete = set() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete), 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: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f104(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(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] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f105([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(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: | |
| 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: | |
| ``` | |
| f106(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value > limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f107([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f108(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| 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: | |
| ``` | |
| f109(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(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[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f110(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f111(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in (edge for edge in edges if len(edge[0]) < 2): | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} 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(routes.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f112([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(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: | |
| 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': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f113([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f114(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 1: | |
| 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: | |
| ``` | |
| f115([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| levels = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f116([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 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((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: | |
| ``` | |
| f117([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(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} 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 and (not nxt.startswith('Z')): | |
| 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: | |
| ``` | |
| f118([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(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) | |
| steps = {start: 0} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| result = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': result, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f119([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f120(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| 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 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: | |
| ``` | |
| f121(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) 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 | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f122(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| result = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f123(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(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() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f124(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f125([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 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":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(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) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(steps) | |
| furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) | |
| returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f127([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(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 (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f128([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(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[: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([0] + windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f129([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(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] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(min(100, 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: | |
| ``` | |
| f130([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| 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: | |
| ``` | |
| f131(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(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 or len(word) < 2: | |
| 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: | |
| ``` | |
| f132(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f133([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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() | |
| result = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| 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 rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f134(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f135(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(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) 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': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f136([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f137([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) 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: | |
| ``` | |
| f138([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * ratemap[group] | |
| selected.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f139([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| charge = value * prices[group] | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f140([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {canon(value) for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f141(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| 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 = [] | |
| 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 (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 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: | |
| ``` | |
| f142(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f143([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(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) | |
| 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: | |
| ``` | |
| f144([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| 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: | |
| ``` | |
| f145([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(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] | |
| 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: | |
| ``` | |
| f146([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(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, []))[: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: | |
| ``` | |
| f147([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(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: | |
| ``` | |
| f148([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f149(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(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 = [] | |
| 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 (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: | |
| ``` | |
| f150(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(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] | |
| 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: | |
| ``` | |
| f151([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f152([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(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) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| 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: | |
| ``` | |
| f153(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| 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 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: | |
| ``` | |
| f154(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| 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 | |
| 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 rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f155(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(board): | |
| board = list(board) | |
| known = 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 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) | |
| 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: | |
| ``` | |
| f156(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| result = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f157(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f158([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(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 | |
| 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: | |
| ``` | |
| f159(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| 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) | |
| 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: | |
| ``` | |
| f160(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| 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: | |
| ``` | |
| f161([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(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: | |
| 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: | |
| ``` | |
| f162(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f163([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[: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])) | |
| 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: | |
| ``` | |
| f164(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f165(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(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) | |
| 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: | |
| ``` | |
| f166([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(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] = 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: | |
| ``` | |
| f167(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(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 = [] | |
| 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) | |
| 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":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f169([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(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) | |
| 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: | |
| ``` | |
| f170([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f171([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| 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: | |
| ``` | |
| f172([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[: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])) | |
| 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: | |
| ``` | |
| f173(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not result or left > result[-1][1] + gap + 1: | |
| 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: | |
| ``` | |
| f174([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| 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 = [] | |
| 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) | |
| 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: | |
| ``` | |
| f175(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(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]))) | |
| matches = [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': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f176([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(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] | |
| growth = 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': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f177([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in edges: | |
| nextmap.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(nextmap.get(cur, [])) if len(str(node)) == 1): | |
| 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, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f178([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {canon(value) for value in excluded} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - excluded | |
| b = {canon(value) for value in right if canon(value) != ''} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f179(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(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: | |
| ``` | |
| f180([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(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 {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| listing = list(distance) | |
| furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f181([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(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[: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: | |
| ``` | |
| f182(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(ratemap[group]) | |
| matches.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': groupmap, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f183([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f184([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f185(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(factors[group]) | |
| kept.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| grand += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': buckets, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f186([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * factors[group] | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f187([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(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])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f188([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| 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: | |
| ``` | |
| f189(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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: | |
| ``` | |
| f190(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| 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 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) | |
| 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: | |
| ``` | |
| f191(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f192([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(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])) | |
| 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: | |
| ``` | |
| f193(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| 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: | |
| 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: | |
| ``` | |
| f194(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(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) | |
| 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: | |
| ``` | |
| f195([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) 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) < 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: | |
| ``` | |
| f196(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f197(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * abs(factors[group]) | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f198([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 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 % 5: | |
| 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, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(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 | |
| 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: | |
| ``` | |
| f200([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f201(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| 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: | |
| ``` | |
| f202([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap + 1: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| 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: | |
| ``` | |
| f203([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(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': min(256, max(series)), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f204([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f205(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(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: | |
| ``` | |
| f206(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(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) | |
| 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: | |
| ``` | |
| f207(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(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([0] + windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f208([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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: | |
| ``` | |
| f209(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(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[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| 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: | |
| ``` | |
| f210([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(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[: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])) | |
| 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: | |
| ``` | |
| f211(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 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))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| 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 | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f212(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(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] = 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: | |
| ``` | |
| f213([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges[:16]: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if any((start in edge for edge in edges)) else {} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.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: | |
| ``` | |
| f214([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(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: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f215(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f216([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f217(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(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 = [] | |
| 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 (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.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: | |
| ``` | |
| f218(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| queue.append(nxt) | |
| 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: | |
| ``` | |
| f219([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f220(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| 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 | |
| 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: | |
| ``` | |
| f221(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(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 | |
| startdegree = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| 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 | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f222(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f223(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(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: | |
| 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 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: | |
| ``` | |
| f224(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(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 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) | |
| 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: | |
| ``` | |
| f225(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| 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) | |
| 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: | |
| ``` | |
| f226(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f227([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(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 | |
| 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: | |
| ``` | |
| f228(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f229(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(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) | |
| 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: | |
| ``` | |
| f230([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(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 = [] | |
| 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: | |
| ``` | |
| f231(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| 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) | |
| 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: | |
| ``` | |
| f232(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f233(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| 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() | |
| sequence = [] | |
| groups = [] | |
| 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 | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f234(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(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 | |
| 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: | |
| ``` | |
| f235([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(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) | |
| 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 nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f236(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(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: | |
| ``` | |
| f237(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = 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: | |
| ``` | |
| f238([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(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 = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| 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]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f239(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(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) | |
| 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: | |
| ``` | |
| f240([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| score = value * abs(ratemap[group]) | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f241([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * prices[group] | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f242([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| 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) | |
| 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: | |
| ``` | |
| f243(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper()[:12] | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f244(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * abs(ratemap[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f245([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| charge = value * ratemap[group] | |
| kept.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f246([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| 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 = [] | |
| 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) | |
| if len(found) >= 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: | |
| ``` | |
| f247(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(prices[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f248([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(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 = [] | |
| groups = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| 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]: | |
| 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: | |
| ``` | |
| f249(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.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: | |
| ``` | |
| f250([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f251(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(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) | |
| 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: | |
| ``` | |
| f252(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(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) > 12: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f253(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| 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: | |
| ``` | |
| f254([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f255(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| children.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| work.append(nxt) | |
| 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: | |
| ``` | |
| f256([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f257(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in (edge for edge in edges if edge[0] <= edge[1]): | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, []))[:20]: | |
| 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, 'count': len(steps)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f258([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * prices[group] | |
| matches.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f259([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f260([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(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] < 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: | |
| ``` | |
| f261([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * ratemap[group] | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f262([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(edges, start, limit): | |
| edges = list(edges) | |
| nextmap = {} | |
| for a, b in (edge for edge in edges if len(edge[1]) < 2): | |
| nextmap.setdefault(a, []).append(b) | |
| levels = {start: 0} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if levels[cur] < min(limit, 3): | |
| for nxt in sorted(nextmap.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| queue.append(nxt) | |
| listing = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f263([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| distance = {start: 0} if limit > 0 or limit < 0 else {} | |
| work = [start] | |
| while work: | |
| cur = work.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in distance: | |
| distance[nxt] = distance[cur] + 1 | |
| work.append(nxt) | |
| sequence = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f264([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(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[: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 | |
| 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: | |
| ``` | |
| f265(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(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: | |
| 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): | |
| 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 | |
| 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: | |
| ``` | |
| f266(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| matches.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| sumvalue += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f267([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(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 + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f268([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| 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: | |
| ``` | |
| f269(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f270(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(ratemap[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f271([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap + 1: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = 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: | |
| ``` | |
| f272([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(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, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, 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: | |
| ``` | |
| f273([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f274([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| listing = sorted | |
| returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f275(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f276([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(segments))): | |
| left = segments[pos][0] | |
| spaces.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f277([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(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 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) | |
| 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: | |
| ``` | |
| f278(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f279([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f280(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, 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: | |
| ``` | |
| f281([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":40,"holes":[6,5,8],"longest":20,"segments":[[3,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(edges, start, limit): | |
| edges = list(edges) | |
| routes = {} | |
| for a, b in edges: | |
| routes.setdefault(a, []).append(b) | |
| distance = {start: 0} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if distance[cur] < limit: | |
| for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): | |
| if nxt not in distance and (not nxt.startswith('Z')): | |
| distance[nxt] = distance[cur] + 1 | |
| pending.append(nxt) | |
| result = list(distance) | |
| edge = sorted((node for node in distance if distance[node] == max(distance.values()))) | |
| returnvalue = {'order': result, 'depth': distance, 'frontier': edge, 'count': len(distance)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f282([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(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]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f283([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| 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 = [] | |
| 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) | |
| if len(known) >= 100: | |
| continue | |
| pending.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: | |
| ``` | |
| f284(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(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 | |
| startdegree = dict(indegree) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.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 startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f285(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(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) | |
| 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: | |
| ``` | |
| f286([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f287([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f288(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(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) | |
| complete = set() | |
| listing = [] | |
| layers = [] | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[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: | |
| ``` | |
| f289(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(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 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| groups.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': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f290(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(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] = 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: | |
| ``` | |
| f291([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(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} | |
| pending = [start] | |
| while pending: | |
| cur = pending.pop(0) | |
| if levels[cur] < limit: | |
| for nxt in sorted(routes.get(cur, [])): | |
| if nxt not in levels: | |
| levels[nxt] = levels[cur] + 1 | |
| pending.append(nxt) | |
| sequence = list(levels) | |
| furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) | |
| returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f292([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'F', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":2,"depth":{"F":0,"H":1},"frontier":["H"],"order":["F","H"]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(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: | |
| ``` | |
| f293([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(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) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f294(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| rounds = [] | |
| 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 | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f295(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')]) | |
| ``` | |
| Output example: | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(spans)): | |
| 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) | |
| 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: | |
| ``` | |
| f296([(3, 6), (6, 12), (15, 22), (19, 23), (29, 33), (34, 39), (44, 50), (49, 52), (60, 62)], 1) | |
| ``` | |
| Output example: | |
| ``` | |
| {"cover":37,"holes":[3,6,5,8],"longest":10,"segments":[[3,12],[15,23],[29,39],[44,52],[60,62]]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if 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 (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 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: | |
| ``` | |
| f297(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f298([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['ready'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":5,"groups":{"core":473,"edge":48,"south":80},"top":["AN85","BR07","AN48"],"total":601} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' 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 | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f299(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(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) > 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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f300(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f301(['GARNET', 'deltax', 'ivory', 'helium', ' Amber', 'frost', 'Ember'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["GARNET","HELIUM","IVORY"],"left":["AMBER","DELTAX","FROST"],"right":["BLUE","CYAN","DELTA"],"score":17} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(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) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f302(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"quartz":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"q":1},"size":156,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(ratemap[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f303([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * prices[group] | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| grand += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f304([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(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() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[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]: | |
| 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: | |
| ``` | |
| f305(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f306(['GARNET', 'delta', 'ivory', 'helium', ' Amber', 'frost', 'Emberx'], ['BLUE', 'delta', 'helium', 'cyan ', 'GARNET', 'ivory', 'Ember'], ['EMBER']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["DELTA","GARNET","HELIUM","IVORY"],"left":["AMBER","EMBERX","FROST"],"right":["BLUE","CYAN"],"score":22} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| 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: | |
| ``` | |
| f307([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(board): | |
| board = list(board) | |
| found = 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 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) | |
| 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: | |
| ``` | |
| f308(['##...#', '#.###.', '#.####', '#....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[0,0],[4,3],[0,5]],"count":4,"filled":21,"sizes":[8,7,5,1]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f309([('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23), ('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8)], ['checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| Output example: | |
| ``` | |
| {"accepted":4,"groups":{"edge":24,"north":378},"top":["CX70","BR96","DL37"],"total":402} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(edges, start, limit): | |
| edges = list(edges) | |
| children = {} | |
| for a, b in edges: | |
| children.setdefault(a, []).append(b) | |
| steps = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} | |
| queue = [start] | |
| while queue: | |
| cur = queue.pop(0) | |
| if steps[cur] < limit: | |
| for nxt in sorted(children.get(cur, [])): | |
| if nxt not in steps: | |
| steps[nxt] = steps[cur] + 1 | |
| queue.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: | |
| ``` | |
| f310([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(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 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: | |
| ``` | |
| f311([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(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 = [] | |
| 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: | |
| ``` | |
| f312(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(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 (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): | |
| 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: | |
| ``` | |
| f313([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A')], 'C', 3) | |
| ``` | |
| Output example: | |
| ``` | |
| {"count":3,"depth":{"C":0,"F":1,"H":2},"frontier":["H"],"order":["C","F","H"]} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| 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) | |
| 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: | |
| ``` | |
| f314(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| 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 = [] | |
| 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: | |
| ``` | |
| f315(['##...#', '#.###.', '..####', '.....#', '#..##.', '#..###']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"anchors":[[1,2],[4,3],[0,0],[4,0],[0,5]],"count":5,"filled":19,"sizes":[8,5,3,2,1]} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(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])) | |
| 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': max(totals), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f316([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 4, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":70,"hits":[1,2,3],"rises":4,"sums":[55,68,70,66,58,42,40,36,39,35,55,47]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f317([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(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: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| startdegree = dict(needs) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| 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 startdegree[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f318(['jobs', 'db', 'worker', 'proxy', 'api', 'auth', 'cache', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('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"]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| 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: | |
| ``` | |
| f319(['Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid', 'Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta'], ['garnet']) | |
| ``` | |
| Output example: | |
| ``` | |
| {"counts":{"amber":7,"blue":2,"cyan":3,"delta":11,"frost":2,"helium":5,"vivid":1},"initials":{"a":7,"b":2,"c":3,"d":11,"f":2,"h":5,"v":1},"size":155,"top":["delta","amber","helium","cyan","blue"]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = 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': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| Example call: | |
| ``` | |
| f320([11, 4, 20, 20, 24, 6, 16, 12, 8, 4, 12, 15, 4, 24, 4], 3, 64) | |
| ``` | |
| Output example: | |
| ``` | |
| {"best":64,"hits":[2],"rises":5,"sums":[35,44,64,50,46,34,36,24,24,31,31,43,32]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| values = list((11, 4, 20, 20, 24, 6, 16, 12) + (8, 4, 12, 15, 4, 24, 4)) | |
| width = 3 | |
| limit = 64 | |
| 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} | |
| spans = list(((3, 6), (6, 12), (15, 22), (19, 23)) + ((29, 33), (34, 39), (44, 50), (49, 52), (60, 62))) | |
| route = record['bb'] | |
| cases = { | |
| 70: 3, | |
| 64: 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))} | |
| nodes = list(('jobs', 'db', 'worker', 'proxy', 'api') + ('auth', 'cache', 'ui')) | |
| route = record['cc'] | |
| cases = { | |
| 40: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth'))), | |
| 37: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db'))), | |
| } | |
| edges = cases[route] | |
| remaining = set(nodes) | |
| done = set() | |
| order = [] | |
| batches = [] | |
| while remaining: | |
| ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node)))) | |
| if not ready: | |
| break | |
| batches.append(ready) | |
| order.extend(ready) | |
| done.update(ready) | |
| remaining.difference_update(ready) | |
| roots = sorted((node for node in nodes if all((after != node for before, after in edges)))) | |
| record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)} | |
| route = record['aa'][0] | |
| cases = { | |
| 'auth': list(('##...#', '#.###.', '..####', '.....#') + ('#..##.', '#..###')), | |
| 'cache': 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)} | |
| stop = ['garnet'] | |
| route = record['cc'] | |
| cases = { | |
| 19: list(('Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue vivid') + ('Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta')), | |
| 21: list(('Cyan, delta. garnet helium', 'Blue, helium. amber delta delta', 'Delta, delta. helium blue') + ('Amber, delta. cyan delta helium', 'Frost, amber. delta delta', 'Amber, cyan. amber amber amber', 'Delta, frost. helium delta quartz')), | |
| } | |
| lines = 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))} | |
| right = list(('BLUE', 'delta', 'helium') + ('cyan ', 'GARNET', 'ivory', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = record['dd'] | |
| cases = { | |
| 155: list(('GARNET', 'delta', 'ivory') + ('helium', ' Amber', 'frost', 'Emberx')), | |
| 156: list(('GARNET', 'deltax', 'ivory') + ('helium', ' Amber', 'frost', 'Ember')), | |
| } | |
| left = cases[route] | |
| a = set() | |
| b = set() | |
| ban = set() | |
| for value in blocked: | |
| ban.add(value.strip().upper()) | |
| for value in left: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| a.add(value) | |
| for value in right: | |
| value = value.strip().upper() | |
| if value not in ban: | |
| b.add(value) | |
| both = a.intersection(b) | |
| record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))} | |
| edges = list((('A', 'C'), ('A', 'B'), ('B', 'D')) + (('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'))) | |
| limit = 3 | |
| route = record['ee'] | |
| cases = { | |
| 17: 'C', | |
| 22: 'F', | |
| } | |
| 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)} | |
| rows = list((('DL63', 'north', 'hold', 14), ('AN00', 'edge', 'hold', 11), ('DL37', 'north', 'checked', 11), ('DL74', 'core', 'hold', 5), ('AN11', 'south', 'void', 14), ('AN48', 'south', 'ready', 12), ('AN85', 'core', 'ready', 23)) + (('DL22', 'edge', 'ready', 16), ('DL59', 'core', 'hold', 19), ('BR96', 'north', 'checked', 19), ('BR33', 'south', 'ready', 4), ('CX70', 'north', 'checked', 24), ('BR07', 'core', 'ready', 20), ('AN44', 'edge', 'checked', 8))) | |
| rates = dict((('north', 7), ('south', 5)) + (('core', 11), ('edge', 3))) | |
| route = record['dd'] | |
| cases = { | |
| 3: ['checked'], | |
| 2: ['ready'], | |
| } | |
| keep = cases[route] | |
| chosen = [(code, group, value) for code, group, state, value in rows if state in keep] | |
| priced = [(code, group, value * rates[group]) for code, group, value in chosen] | |
| groups = {} | |
| for code, group, price in priced: | |
| groups[group] = groups.get(group, 0) + price | |
| ranked = sorted(priced, key=lambda row: (-row[2], row[0])) | |
| record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} | |
| out['aa'] = record['aa'] | |
| out['cc'] = record['cc'] | |
| out['bb'] = record['bb'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |