|
Download tasks/program-execution-tracing/contexts/sample-00043.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 381 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00043.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00043.md
-
curl -L -o sample-00043.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00043.md
381 kB
| ### 001 | |
| ```python | |
| def f1(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f1(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(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 | |
| 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)) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f2(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f3(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[: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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f4(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').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 | |
| 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 | |
| ``` | |
| ``` | |
| f5(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f6([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(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] | |
| growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {str(value).upper() 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f9(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(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) | |
| 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 | |
| ``` | |
| ``` | |
| f10(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 013 | |
| ```python | |
| def f13(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], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f13([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(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() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f14(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f16([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(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) | |
| 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 | |
| ``` | |
| ``` | |
| f17(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(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 | |
| ``` | |
| ``` | |
| f18([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f19([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| 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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f20(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| score = value * ratemap[group] | |
| kept.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f21([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(ratemap[group]) | |
| kept.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f24([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 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 | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f25([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop or len(word) < 2: | |
| 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 | |
| ``` | |
| ``` | |
| f26(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f28([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| grand = 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: | |
| charge = value * factors[group] | |
| kept.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| grand += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f30(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * ratemap[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(matches), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f31([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| complete = set() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[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) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f33([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(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[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| 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 | |
| ``` | |
| ``` | |
| f35([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| score = value * prices[group] | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| listing = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f37(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(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) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=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]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f40(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap + 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 | |
| ``` | |
| ``` | |
| f41([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f42(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f43([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(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], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f44([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(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] = 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 | |
| ``` | |
| ``` | |
| f45([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(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: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + min(gap, 2): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(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[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f49([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f50([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).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 | |
| 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 | |
| ``` | |
| ``` | |
| f52(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(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: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| breaks.append(min(100, left - result[pos - 1][1])) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f54(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f55([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(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} - 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 | |
| ``` | |
| ``` | |
| f56(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f57([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f59(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(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] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * factors[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f61([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f62(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + min(gap, 2): | |
| 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': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f63([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(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() | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f64(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and rate0 != 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f65([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(board): | |
| board = list(board) | |
| found = 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 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 | |
| ``` | |
| ``` | |
| f66(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(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 | |
| 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 | |
| ``` | |
| ``` | |
| f67([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| breaks.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(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) | |
| 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': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f69([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f70(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {str(value).upper() for value in left} - ignored | |
| b = {canon(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 | |
| ``` | |
| ``` | |
| f71(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(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] + max(0, 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((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 | |
| ``` | |
| ``` | |
| f72([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(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: | |
| 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 | |
| ``` | |
| ``` | |
| f73([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f74(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f75([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(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 | |
| 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 | |
| ``` | |
| ``` | |
| f76(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f77([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state 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 | |
| ``` | |
| ``` | |
| f78([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {tidy(value) 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| 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 | |
| ``` | |
| ``` | |
| f80(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f81(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f82([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| 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) | |
| if len(visited) >= 100: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f83(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(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 | |
| ``` | |
| ``` | |
| f84(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f86([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f87(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(rows, keep, prices): | |
| 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 * 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 | |
| ``` | |
| ``` | |
| f88([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f89([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f90([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(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 | |
| ``` | |
| ``` | |
| f93([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(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} - 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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(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 | |
| ``` | |
| ``` | |
| f95([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) < 2: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f96(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {str(value).upper() for value in left} - banned | |
| b = {canon(value) for value in right} - banned | |
| 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 | |
| ``` | |
| ``` | |
| f97(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in 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 | |
| ``` | |
| ``` | |
| f98(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| score = value * abs(ratemap[group]) | |
| kept.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f99([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f100([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(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': | |
| 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 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| ``` | |
| f102(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(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 = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f103(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 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))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| 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 | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| wordmap[word] = min(9, wordmap.get(word, 0) + 1) | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f106(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * factors[group] | |
| kept.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| grand += charge | |
| 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, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f107([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| 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 | |
| ``` | |
| ``` | |
| f108([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f109(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| 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 | |
| ``` | |
| ``` | |
| f110([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = min(9, 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 | |
| ``` | |
| ``` | |
| f111(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| 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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f112(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(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} - 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 | |
| ``` | |
| ``` | |
| f114(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * abs(factors[group]) | |
| kept.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f115([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(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], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f116([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f117(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=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 routes[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 | |
| ``` | |
| ``` | |
| f118(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| 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])) | |
| 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 | |
| ``` | |
| ``` | |
| f119(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f120(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| 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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f121(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f123([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 124 | |
| ```python | |
| def f124(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f124([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 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])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(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 * 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 | |
| ``` | |
| ``` | |
| f126([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f127(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f128(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in 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 | |
| ``` | |
| ``` | |
| f129(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(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) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| complete.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 | |
| ``` | |
| ``` | |
| f130(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {str(value).upper() for value in left} - banned | |
| b = {tidy(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f132(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if 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 | |
| ``` | |
| ``` | |
| f134(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f135([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 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 | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f137(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(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] = max(result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, 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 | |
| ``` | |
| ``` | |
| f138([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(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) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| 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]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f140(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f141([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| 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 | |
| ``` | |
| ``` | |
| f142(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f143([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(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: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f144(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(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 | |
| ``` | |
| ``` | |
| f145(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(board): | |
| board = list(board) | |
| known = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f146(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f147([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(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[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f148([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(board): | |
| board = list(board) | |
| known = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f149(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 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[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) | |
| 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 | |
| ``` | |
| ``` | |
| f150(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * factors[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 | |
| ``` | |
| ``` | |
| f151([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(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(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 | |
| ``` | |
| ``` | |
| f152([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f153(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| 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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(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] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').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 | |
| 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 | |
| ``` | |
| ``` | |
| f156(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(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 | |
| ``` | |
| ``` | |
| f157(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(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[: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 | |
| 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 | |
| ``` | |
| ``` | |
| f158(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(board): | |
| board = list(board) | |
| found = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f159(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f160(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(rows, keep, prices): | |
| 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}: | |
| cost = value * abs(prices[group]) | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f162([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(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 | |
| startdegree = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f163(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in ratemap and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 165 | |
| ```python | |
| def f165(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 | |
| ``` | |
| ``` | |
| f165([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f166(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f167([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if 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 | |
| ``` | |
| ``` | |
| f168(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| 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) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f170(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f172([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(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[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f173(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| 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 | |
| ``` | |
| ``` | |
| f174(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f175([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| 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': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f176([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(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]))) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f177([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f178(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f179(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(board): | |
| board = list(board) | |
| known = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f180(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f181([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(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(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 | |
| ``` | |
| ``` | |
| f182([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(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': min(256, max(totals)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(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] = 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 | |
| ``` | |
| ``` | |
| f184([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f185(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(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 | |
| rootsource = dict(needs) | |
| complete = set() | |
| listing = [] | |
| rounds = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.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': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f186(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {canon(value) 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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| banned = {tidy(value) for value in banned} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f188(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(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 = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f189(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in wordmap.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f190(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {canon(value) for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right if canon(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f193(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = 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((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 | |
| ``` | |
| ``` | |
| f194([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(prices[group]) | |
| selected.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f196(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f197([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(ratemap[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': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f198([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f199(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(series) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f200([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(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) | |
| complete = set() | |
| result = [] | |
| groups = [] | |
| 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 | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f201(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f202([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored | |
| b = {normalize(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 | |
| ``` | |
| ``` | |
| f203(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(0, result[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f205([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| listing = [] | |
| rounds = [] | |
| 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 | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(board): | |
| board = list(board) | |
| visited = 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 visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f208(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in wordmap.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + min(gap, 2): | |
| 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 | |
| ``` | |
| ``` | |
| f211([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(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[: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 | |
| 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 | |
| ``` | |
| ``` | |
| f212(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = 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 | |
| ``` | |
| ``` | |
| f213([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right if normalize(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f214(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f215([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| 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), 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) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f216(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * ratemap[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f218([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(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) | |
| 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 | |
| ``` | |
| ``` | |
| f219(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(factors[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 | |
| ``` | |
| ``` | |
| f221([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(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 | |
| ``` | |
| ``` | |
| f222([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(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 | |
| ``` | |
| ``` | |
| f224(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(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 if normalize(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 | |
| ``` | |
| ``` | |
| f226(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {str(value).strip() for value in banned} | |
| a = {str(value).upper() for value in left} - banned | |
| b = {canon(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5 + 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 | |
| ``` | |
| ``` | |
| f228([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, min(65, 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 | |
| ``` | |
| ``` | |
| f229([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f230([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| 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 | |
| ``` | |
| ``` | |
| f231(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f232([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(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] | |
| 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 | |
| ``` | |
| ``` | |
| f233([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 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 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| 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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f234(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"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(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f236([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(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: | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f237(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f238([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' 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 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f239(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| groups = [] | |
| while len(complete) < len(nodes): | |
| available = 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 available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f240(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[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 | |
| ``` | |
| ``` | |
| f241(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(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) | |
| 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 | |
| ``` | |
| ``` | |
| f242([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(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 | |
| rootsource = dict(indegree) | |
| handled = set() | |
| listing = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[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 | |
| ``` | |
| ``` | |
| f243(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f244(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f245(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f246([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap or rate0 <= rate0 + 0: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f247([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f248(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in ratemap}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(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(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 | |
| ``` | |
| ``` | |
| f250([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f251([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {str(value).upper() 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 | |
| ``` | |
| ``` | |
| f252(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(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, 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 | |
| ``` | |
| ``` | |
| f253([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(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(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((min(100, right - left) for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f254([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| startdegree = dict(needs) | |
| complete = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if needs[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 | |
| rounds.append(current) | |
| for node in current: | |
| complete.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f255(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(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 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| result = [] | |
| rounds = [] | |
| while len(complete) < len(nodes): | |
| 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 | |
| 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 rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f256(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| growth = sum((1 for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f257([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f258([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * abs(ratemap[group]) | |
| matches.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| grand += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': totals, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f259([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(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] = max(0, result[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 0) | |
| ``` | |
| ``` | |
| {"cover":41,"holes":[6,1,6,4,1,5],"longest":11,"segments":[[3,9],[15,18],[19,23],[29,40],[44,48],[49,55],[60,67]]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f262([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| score = value * abs(prices[group]) | |
| matches.append((code, score)) | |
| groupmap[group] = groupmap.get(group, 0) + score | |
| amount += score | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': groupmap, 'top': [code for code, score in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f263([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":341,"edge":180,"north":161,"south":117},"top":["DL45","CX56","DL60"],"total":799} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = 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 | |
| ``` | |
| ``` | |
| f264([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f265(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f266(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(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 | |
| ``` | |
| ``` | |
| f267([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap + 1: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f268([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).upper() for value in excluded} | |
| a = {str(value).upper() for value in left} - excluded | |
| b = {tidy(value) for value in right if tidy(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f269(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f270([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([(3, 9), (6, 9), (15, 18), (19, 23), (29, 34), (34, 40), (44, 48), (49, 55), (60, 67)], 2) | |
| ``` | |
| ``` | |
| {"cover":43,"holes":[6,6,4,5],"longest":11,"segments":[[3,9],[15,23],[29,40],[44,55],[60,67]]} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).strip() 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 | |
| ``` | |
| ``` | |
| f272(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(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 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 | |
| ``` | |
| ``` | |
| f273(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f274(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().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 | |
| ``` | |
| ``` | |
| f275(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| sorteditems = 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 sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f276(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(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 | |
| ``` | |
| ``` | |
| f277(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper()[:12] | |
| 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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f278(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(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 | |
| ``` | |
| ``` | |
| f279(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(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 = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f280(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f282(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.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': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f283(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f284(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f285([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop or len(word) > 12: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f286(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| 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': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f287([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| 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 | |
| ``` | |
| ``` | |
| f288(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f289(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(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': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in 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 | |
| ``` | |
| ``` | |
| f291(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| 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 children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(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 | |
| ``` | |
| ``` | |
| f293(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').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 | |
| ``` | |
| ``` | |
| f294(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(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[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in tallies.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right if normalize(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f296(['delta', 'GARNET', 'Ember', 'ivoryx', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY","IVORYX"],"both":["FROST","GARNET"],"left":["DELTA","HELIUM","IVORYX"],"right":["AMBER","BLUE","CYAN","IVORY"],"score":11} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(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 | |
| ``` | |
| ``` | |
| f298([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f299([24, 13, 23, 17, 22, 12, 21, 25, 15, 19, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,58,61,59,40,30,15,32,32]} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f300([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| 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 | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f301(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 302 | |
| ```python | |
| def f302(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors or rate0 <= rate0 + 0: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f304([24, 13, 23, 17, 22, 12, 21, 22, 15, 24, 6, 5, 4, 23, 5], 3, 62) | |
| ``` | |
| ``` | |
| {"best":62,"hits":[2],"rises":5,"sums":[60,53,62,51,55,55,58,61,45,35,15,32,32]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(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: | |
| if word in stop: | |
| continue | |
| wordmap[word] = wordmap.get(word, 0) + 1 | |
| ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in wordmap.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(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() | |
| result = [] | |
| rounds = [] | |
| 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 | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f306(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| complete = set() | |
| listing = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f307(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| known.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f308(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors and count > 0: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f310([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in ratemap: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f311([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| handled = set() | |
| result = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f312(['jobs', 'proxy', 'auth', 'api', 'ui', 'cache', 'worker', 'db'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {str(value).upper() for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| 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 | |
| ``` | |
| ``` | |
| f313(['delta', 'GARNET', 'Emberx', 'ivory', 'helium', 'frost', 'Ember'], ['GARNET', ' Amber', 'cyan ', 'ivory', 'BLUE', 'frost', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["FROST","GARNET","IVORY"],"left":["DELTA","EMBERX","HELIUM"],"right":["AMBER","BLUE","CYAN"],"score":16} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f314([('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["FP",41],["HR",38],["CX",35],["BR",12]],"missing":["AN","DL"],"total":185} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item 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 | |
| ``` | |
| ``` | |
| f315([('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20), ('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"core":372,"edge":180,"north":161,"south":65},"top":["DL45","CX56","DL60"],"total":778} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316([('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4), ('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12)], [('BR', 5), ('CX', 3), ('EV', 5), ('FP', 3), ('GQ', 9), ('HR', 3)], 2) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["GQ",47],["HR",38],["CX",35],["FP",29],["BR",12]],"missing":["AN","DL"],"total":173} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = min(9, frequency.get(word, 0) + 1) | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317(['Garnet, cyan. helium delta quartz', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"quartz":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"q":1},"size":131,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(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) | |
| 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 | |
| ``` | |
| ``` | |
| f318(['......', '.###.#', '##....', '#.##.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[3,2],[1,5],[3,5],[5,0],[5,4]],"count":6,"filled":13,"sizes":[6,3,1,1,1,1]} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| 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 | |
| ``` | |
| ``` | |
| f319(['Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta', 'Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":3,"blue":3,"cyan":3,"delta":7,"ember":3,"helium":6,"vivid":1},"initials":{"a":3,"b":3,"c":3,"d":7,"e":3,"h":6,"v":1},"size":130,"top":["delta","helium","amber","blue","cyan"]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(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 | |
| ``` | |
| ``` | |
| f320(['......', '.###.#', '.#....', '####.#', '...#..', '#...#.']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,1],[1,5],[3,5],[5,0],[5,4]],"count":5,"filled":13,"sizes":[9,1,1,1,1]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| board = list(('......', '.###.#', '.#....', '####.#') + ('...#..', '#...#.')) | |
| 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)} | |
| clue = record['dd'] | |
| lines = list(('Garnet, cyan. helium delta', 'Helium, blue. blue amber amber', 'Ember, cyan. helium delta') + ('Garnet, delta. garnet garnet ember', 'Helium, delta. amber cyan', 'Delta, helium. ember garnet helium vivid', 'Blue, delta. delta garnet')) | |
| stop = ['garnet'] | |
| 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))} | |
| stamp = record['dd'] | |
| right = list(('GARNET', ' Amber') + ('cyan ', 'ivory', 'BLUE', 'frost', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = clue | |
| cases = { | |
| 5: list(('delta', 'GARNET') + ('Emberx', 'ivory', 'helium', 'frost', 'Ember')), | |
| 6: list(('delta', 'GARNET') + ('Ember', 'ivoryx', 'helium', '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))} | |
| ticket = record['ee'] | |
| nodes = list(('jobs', 'proxy', 'auth', 'api', 'ui') + ('cache', 'worker', 'db')) | |
| route = stamp | |
| cases = { | |
| 131: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('cache', 'auth'))), | |
| 130: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('db', 'cache'))), | |
| } | |
| 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)} | |
| badge = record['aa'][0] | |
| spans = list(((3, 9), (6, 9), (15, 18), (19, 23)) + ((29, 34), (34, 40), (44, 48), (49, 55), (60, 67))) | |
| route = (clue, stamp) | |
| cases = { | |
| (5, 130): 2, | |
| (5, 131): 0, | |
| (6, 130): 0, | |
| (6, 131): 0, | |
| } | |
| 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))} | |
| permit = record['cc'] | |
| width = 3 | |
| limit = 62 | |
| route = (ticket, permit) | |
| cases = { | |
| (11, 41): list((24, 13, 23, 17, 22, 12) + (21, 25, 15, 19, 6, 5, 4, 23, 5)), | |
| (11, 43): list((24, 13, 23, 17, 22, 12) + (21, 25, 15, 19, 6, 5, 4, 23, 5)), | |
| (16, 41): list((24, 13, 23, 17, 22, 12) + (21, 25, 15, 19, 6, 5, 4, 23, 5)), | |
| (16, 43): list((24, 13, 23, 17, 22, 12) + (21, 22, 15, 24, 6, 5, 4, 23, 5)), | |
| } | |
| values = cases[route] | |
| current = sum(values[:width]) | |
| sums = [current] | |
| for pos in range(width, len(values)): | |
| current += values[pos] - values[pos - width] | |
| sums.append(current) | |
| hits = [] | |
| rises = 0 | |
| for pos, value in enumerate(sums): | |
| if value >= limit: | |
| hits.append(pos) | |
| if pos and value > sums[pos - 1]: | |
| rises += 1 | |
| record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} | |
| signal = record['dd'][5] | |
| rows = list((('BR49', 'north', 'hold', 21), ('BR86', 'south', 'checked', 13), ('AN23', 'south', 'hold', 11), ('DL60', 'core', 'ready', 12), ('DL97', 'core', 'checked', 7), ('AN34', 'north', 'hold', 4), ('AN71', 'edge', 'ready', 22), ('CX08', 'edge', 'ready', 20)) + (('DL45', 'north', 'ready', 23), ('DL82', 'edge', 'hold', 13), ('CX19', 'edge', 'checked', 18), ('CX56', 'core', 'checked', 12), ('AN93', 'north', 'hold', 13), ('AN30', 'core', 'hold', 14))) | |
| keep = list(('ready',) + ('checked',)) | |
| route = (badge, signal) | |
| cases = { | |
| ('auth', 55): dict((('north', 7), ('south', 5), ('core', 12)) + (('edge', 3),)), | |
| ('auth', 58): dict((('north', 7), ('south', 9), ('core', 11)) + (('edge', 3),)), | |
| ('cache', 55): dict((('north', 7), ('south', 9), ('core', 11)) + (('edge', 3),)), | |
| ('cache', 58): dict((('north', 7), ('south', 9), ('core', 11)) + (('edge', 3),)), | |
| } | |
| rates = 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} | |
| token = record['bb'] | |
| right = list((('BR', 5), ('CX', 3), ('EV', 5)) + (('FP', 3), ('GQ', 9), ('HR', 3))) | |
| fee = 2 | |
| route = token | |
| cases = { | |
| 799: list((('AN', 14), ('BR', 2), ('CX', 11), ('DL', 4)) + (('EV', 2), ('FP', 9), ('GQ', 5), ('HR', 12))), | |
| 778: list((('AN', 12), ('BR', 2), ('CX', 11), ('DL', 4)) + (('EV', 2), ('FP', 13), ('GQ', 5), ('HR', 12))), | |
| } | |
| left = cases[route] | |
| joined = [] | |
| for key, count in left: | |
| matches = [rate for name, rate in right if name == key] | |
| if matches: | |
| joined.append([key, count * matches[0] + fee]) | |
| joined.sort(key=lambda item: (-item[1], item[0])) | |
| rightkeys = {key for key, rate in right} | |
| record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |