|
Download tasks/program-execution-tracing/contexts/sample-00030.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-00030.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00030.md
-
curl -L -o sample-00030.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00030.md
381 kB
| ### 001 | |
| ```python | |
| def f1(nodes, edges): | |
| nodes = list(nodes) | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| handled = set() | |
| listing = [] | |
| groups = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else 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 startdegree[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f1(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 002 | |
| ```python | |
| def f2(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 | |
| ``` | |
| ``` | |
| f2([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 003 | |
| ```python | |
| def f3(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: | |
| if key in ratemap and count > 0: | |
| 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 | |
| ``` | |
| ``` | |
| f3([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 004 | |
| ```python | |
| def f4(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * prices[group] | |
| matches.append((code, charge)) | |
| buckets[group] = buckets.get(group, 0) + charge | |
| amount += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f4([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 005 | |
| ```python | |
| def f5(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| 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 = [] | |
| layers = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| 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 nextmap[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f5(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 006 | |
| ```python | |
| def f6(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f6(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 007 | |
| ```python | |
| def f7(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f7([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 008 | |
| ```python | |
| def f8(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {canon(value) for value in left} - excluded | |
| b = {canon(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f8([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 009 | |
| ```python | |
| def f9(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 = [] | |
| 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 | |
| ``` | |
| ``` | |
| f9(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 010 | |
| ```python | |
| def f10(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = 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 | |
| ``` | |
| ``` | |
| f10([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 011 | |
| ```python | |
| def f11(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() | |
| sequence = [] | |
| rounds = [] | |
| 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 | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f11(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 012 | |
| ```python | |
| def f12(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f12([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 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([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 014 | |
| ```python | |
| def f14(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f14(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 015 | |
| ```python | |
| def f15(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f15([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 016 | |
| ```python | |
| def f16(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f16(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 017 | |
| ```python | |
| def f17(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] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f17([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 018 | |
| ```python | |
| def f18(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f18([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 019 | |
| ```python | |
| def f19(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f19(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 020 | |
| ```python | |
| def f20(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: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f20(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 021 | |
| ```python | |
| def f21(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f21([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 022 | |
| ```python | |
| def f22(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f22([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 023 | |
| ```python | |
| def f23(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f23([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 024 | |
| ```python | |
| def f24(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper()[:12] | |
| 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 | |
| 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 | |
| ``` | |
| ``` | |
| f24([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 025 | |
| ```python | |
| def f25(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| ignored = {normalize(value) for value in ignored} | |
| a = {normalize(value) for value in left} - ignored | |
| b = {normalize(value) for value in right if normalize(value) != ''} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f25([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 026 | |
| ```python | |
| def f26(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| complete = set() | |
| listing = [] | |
| layers = [] | |
| 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 | |
| layers.append(current) | |
| for node in current: | |
| complete.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f26(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 027 | |
| ```python | |
| def f27(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows[: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': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f27([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 028 | |
| ```python | |
| def f28(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| if len(found) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f28(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 029 | |
| ```python | |
| def f29(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f29(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 030 | |
| ```python | |
| def f30(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f30([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 031 | |
| ```python | |
| def f31(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f31(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 032 | |
| ```python | |
| def f32(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([0] + windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f32([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 033 | |
| ```python | |
| def f33(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| 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 | |
| ``` | |
| ``` | |
| f33([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 034 | |
| ```python | |
| def f34(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f34([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 035 | |
| ```python | |
| def f35(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| result = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f35(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 036 | |
| ```python | |
| def f36(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 | |
| startdegree = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), 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: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f36(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 037 | |
| ```python | |
| def f37(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f37(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 038 | |
| ```python | |
| def f38(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} and value >= 0: | |
| cost = value * abs(prices[group]) | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f38([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 039 | |
| ```python | |
| def f39(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 and count > 0: | |
| items.append((key, count * ratemap[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f39([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 040 | |
| ```python | |
| def f40(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, 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 | |
| ``` | |
| ``` | |
| f40([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 041 | |
| ```python | |
| def f41(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 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f41(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 042 | |
| ```python | |
| def f42(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[: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 | |
| ``` | |
| ``` | |
| f42([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 043 | |
| ```python | |
| def f43(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| 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 | |
| ``` | |
| ``` | |
| f43(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 044 | |
| ```python | |
| def f44(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| growth = sum((1 for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f44([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 045 | |
| ```python | |
| def f45(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f45([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 046 | |
| ```python | |
| def f46(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f46(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 047 | |
| ```python | |
| def f47(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f47(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 048 | |
| ```python | |
| def f48(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f48(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 049 | |
| ```python | |
| def f49(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').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 | |
| 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 | |
| ``` | |
| ``` | |
| f49([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 050 | |
| ```python | |
| def f50(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f50(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 051 | |
| ```python | |
| def f51(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in factors: | |
| items.append((key, count * factors[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f51([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 052 | |
| ```python | |
| def f52(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f52([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 053 | |
| ```python | |
| def f53(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f53(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 054 | |
| ```python | |
| def f54(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) | |
| spaces = [] | |
| for pos in range(1, len(result)): | |
| left = result[pos][0] | |
| spaces.append(min(100, left - result[pos - 1][1])) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, right - left) for left, right in result)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f54([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 055 | |
| ```python | |
| def f55(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[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) | |
| 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 | |
| ``` | |
| ``` | |
| f55(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 056 | |
| ```python | |
| def f56(rows, keep, ratemap): | |
| rows = list(rows) | |
| matches = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * ratemap[group] | |
| matches.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| grand += charge | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, charge in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f56([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 057 | |
| ```python | |
| def f57(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f57(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 058 | |
| ```python | |
| def f58(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value >= 0: | |
| score = value * factors[group] | |
| kept.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f58([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 059 | |
| ```python | |
| def f59(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f59([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 060 | |
| ```python | |
| def f60(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f60([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 061 | |
| ```python | |
| def f61(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f61(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 062 | |
| ```python | |
| def f62(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 | |
| ``` | |
| ``` | |
| f62(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 063 | |
| ```python | |
| def f63(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(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 | |
| ``` | |
| ``` | |
| f63([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 064 | |
| ```python | |
| def f64(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f64([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 065 | |
| ```python | |
| def f65(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, 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 | |
| ``` | |
| ``` | |
| f65([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 066 | |
| ```python | |
| def f66(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f66([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 067 | |
| ```python | |
| def f67(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not result or left > result[-1][1] + max(0, gap): | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(result[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f67([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 068 | |
| ```python | |
| def f68(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f68(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 069 | |
| ```python | |
| def f69(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] | |
| growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f69([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 070 | |
| ```python | |
| def f70(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices and count > 0: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f70([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 071 | |
| ```python | |
| def f71(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 | |
| 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 | |
| ``` | |
| ``` | |
| f71(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 072 | |
| ```python | |
| def f72(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f72([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 073 | |
| ```python | |
| def f73(board): | |
| board = list(board) | |
| known = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f73(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 074 | |
| ```python | |
| def f74(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[:64]: | |
| 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])) | |
| 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 | |
| ``` | |
| ``` | |
| f74(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 075 | |
| ```python | |
| def f75(lines, stop): | |
| lines = list(lines) | |
| wordmap = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f75(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 076 | |
| ```python | |
| def f76(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in factors: | |
| combined.append((key, count * factors[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f76([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 077 | |
| ```python | |
| def f77(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: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f77([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 078 | |
| ```python | |
| def f78(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \n').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 | |
| 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 | |
| ``` | |
| ``` | |
| f78([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 079 | |
| ```python | |
| def f79(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f79([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 080 | |
| ```python | |
| def f80(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) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available | |
| layers.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': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f80(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 081 | |
| ```python | |
| def f81(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f81([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 082 | |
| ```python | |
| def f82(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {canon(value) for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f82([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 083 | |
| ```python | |
| def f83(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 | |
| ``` | |
| ``` | |
| f83([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 084 | |
| ```python | |
| def f84(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 | |
| startdegree = 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=any((str(node).isupper() for node in nodes))) | |
| 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 startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f84(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 085 | |
| ```python | |
| def f85(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] + min(gap, 2): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f85([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 086 | |
| ```python | |
| def f86(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])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f86([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 087 | |
| ```python | |
| def f87(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) 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 | |
| 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 | |
| ``` | |
| ``` | |
| f87(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 088 | |
| ```python | |
| def f88(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| score = value * factors[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': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f88([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 089 | |
| ```python | |
| def f89(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * prices[group] | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f89([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 090 | |
| ```python | |
| def f90(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f90([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 091 | |
| ```python | |
| def f91(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 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 | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f91([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 092 | |
| ```python | |
| def f92(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[:20]: | |
| if key in factors and count > 0: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in factors)) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f92([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 093 | |
| ```python | |
| def f93(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper()[:12] | |
| banned = {normalize(value) for value in banned} | |
| a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f93([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 094 | |
| ```python | |
| def f94(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f94([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 095 | |
| ```python | |
| def f95(left, right, ignored): | |
| left = list(left) | |
| def canon(value): | |
| return ' '.join(str(value).split()).upper() | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {canon(value) for value in left} - ignored | |
| b = {canon(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f95([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 096 | |
| ```python | |
| def f96(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 = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f96(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 097 | |
| ```python | |
| def f97(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 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 | |
| ``` | |
| ``` | |
| f97([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 098 | |
| ```python | |
| def f98(rows, keep, factors): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| matches.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f98([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 099 | |
| ```python | |
| def f99(board): | |
| board = list(board) | |
| visited = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f99(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 100 | |
| ```python | |
| def f100(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| 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 | |
| ``` | |
| ``` | |
| f100(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 101 | |
| ```python | |
| def f101(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f101([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 102 | |
| ```python | |
| def f102(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(spans): | |
| if not segments or left > segments[-1][1] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(min(100, left - segments[pos - 1][1])) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f102([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 103 | |
| ```python | |
| def f103(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| spaces.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in combined)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f103([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 104 | |
| ```python | |
| def f104(left, right, banned): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - banned | |
| b = {canon(value) for value in right if canon(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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f104([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 105 | |
| ```python | |
| def f105(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f105([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 106 | |
| ```python | |
| def f106(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f106(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 107 | |
| ```python | |
| def f107(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in prices)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f107([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 108 | |
| ```python | |
| def f108(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| 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({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f108([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 109 | |
| ```python | |
| def f109(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f109([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 110 | |
| ```python | |
| def f110(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f110([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 111 | |
| ```python | |
| def f111(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])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f111(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 112 | |
| ```python | |
| def f112(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| ``` | |
| f112(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 113 | |
| ```python | |
| def f113(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in tallies.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f113(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 114 | |
| ```python | |
| def f114(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f114([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 115 | |
| ```python | |
| def f115(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f115([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 116 | |
| ```python | |
| def f116(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(totals[:64]) if value > limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f116([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 117 | |
| ```python | |
| def f117(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').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((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f117([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 118 | |
| ```python | |
| def f118(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f118(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 119 | |
| ```python | |
| def f119(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] | |
| 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 | |
| ``` | |
| ``` | |
| f119([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 120 | |
| ```python | |
| def f120(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| 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 | |
| ``` | |
| ``` | |
| f120(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 121 | |
| ```python | |
| def f121(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap and 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])) | |
| leftover = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f121([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 122 | |
| ```python | |
| def f122(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) | |
| finished = set() | |
| result = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f122(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 123 | |
| ```python | |
| def f123(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(prices[group]) | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f123([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 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)] | |
| 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 | |
| ``` | |
| ``` | |
| f124([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 125 | |
| ```python | |
| def f125(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: | |
| cost = value * ratemap[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f125([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 126 | |
| ```python | |
| def f126(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() | |
| sequence = [] | |
| layers = [] | |
| while len(complete) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in complete), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| 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 rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f126(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 127 | |
| ```python | |
| def f127(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 | |
| startdegree = 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 startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f127(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 128 | |
| ```python | |
| def f128(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f128([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 129 | |
| ```python | |
| def f129(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f129([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 130 | |
| ```python | |
| def f130(board): | |
| board = list(board) | |
| found = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(min(len(board[row]), 10)): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| found.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| pending.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f130(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 131 | |
| ```python | |
| def f131(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + max(0, gap): | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f131([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 132 | |
| ```python | |
| def f132(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 = [] | |
| queue = [(row, col)] | |
| found.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f132(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 133 | |
| ```python | |
| def f133(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 = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| work.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f133(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 134 | |
| ```python | |
| def f134(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([0] + windows), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f134([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 135 | |
| ```python | |
| def f135(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| 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 | |
| ``` | |
| ``` | |
| f135(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 136 | |
| ```python | |
| def f136(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(series) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f136([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 137 | |
| ```python | |
| def f137(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| sequence = [] | |
| groups = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| handled.add(node) | |
| 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 | |
| ``` | |
| ``` | |
| f137(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 138 | |
| ```python | |
| def f138(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] = 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 | |
| ``` | |
| ``` | |
| f138(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 139 | |
| ```python | |
| def f139(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(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': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f139([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 140 | |
| ```python | |
| def f140(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f140([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 141 | |
| ```python | |
| def f141(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) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f141(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 142 | |
| ```python | |
| def f142(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[:64]: | |
| 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 or len(word) < 2: | |
| 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 | |
| ``` | |
| ``` | |
| f142(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 143 | |
| ```python | |
| def f143(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 = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| queue.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f143(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 144 | |
| ```python | |
| def f144(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: | |
| 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 | |
| ``` | |
| ``` | |
| f144(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 145 | |
| ```python | |
| def f145(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, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f145([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 146 | |
| ```python | |
| def f146(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] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f146([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 147 | |
| ```python | |
| def f147(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 if tidy(value) != tidy(value) or tidy(value) != ''} - 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 | |
| ``` | |
| ``` | |
| f147([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 148 | |
| ```python | |
| def f148(left, right, fee): | |
| left = list(left) | |
| factors = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in factors: | |
| factors[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f148([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 149 | |
| ```python | |
| def f149(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f149([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 150 | |
| ```python | |
| def f150(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f150([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 151 | |
| ```python | |
| def f151(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: | |
| 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 | |
| ``` | |
| ``` | |
| f151(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 152 | |
| ```python | |
| def f152(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f152([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 153 | |
| ```python | |
| def f153(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| ignored = {str(value).upper() for value in ignored} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f153([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 154 | |
| ```python | |
| def f154(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] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f154([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 155 | |
| ```python | |
| def f155(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not available: | |
| break | |
| current = available | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| 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 startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f155(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 156 | |
| ```python | |
| def f156(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 | |
| ``` | |
| ``` | |
| f156([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 157 | |
| ```python | |
| def f157(board): | |
| board = list(board) | |
| found = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f157(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 158 | |
| ```python | |
| def f158(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[:20]: | |
| if key in factors: | |
| matched.append((key, count * factors[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| unmatched = sorted({key for key, count in left if key not in factors}) | |
| returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f158([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 159 | |
| ```python | |
| def f159(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' ').upper() | |
| banned = {str(value).upper() for value in banned} | |
| a = {normalize(value) for value in left} - banned | |
| b = {normalize(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f159([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 160 | |
| ```python | |
| def f160(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| 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":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 161 | |
| ```python | |
| def f161(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() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f161(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 162 | |
| ```python | |
| def f162(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| kept.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| grand += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f162([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 163 | |
| ```python | |
| def f163(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices and count > 0: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f163([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 164 | |
| ```python | |
| def f164(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] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f164([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 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 % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f165([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 166 | |
| ```python | |
| def f166(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(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((right - left for left, right in result)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in result)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f166([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 167 | |
| ```python | |
| def f167(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f167([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 168 | |
| ```python | |
| def f168(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: | |
| 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 | |
| ``` | |
| ``` | |
| f168([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 169 | |
| ```python | |
| def f169(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f169([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 170 | |
| ```python | |
| def f170(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 and count > 0: | |
| 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 | |
| ``` | |
| ``` | |
| f170([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 171 | |
| ```python | |
| def f171(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] | |
| ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f171([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 172 | |
| ```python | |
| def f172(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f172([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 173 | |
| ```python | |
| def f173(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap and count > 0: | |
| 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 | |
| ``` | |
| ``` | |
| f173([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 174 | |
| ```python | |
| def f174(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] | |
| ups = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) | |
| returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f174([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 175 | |
| ```python | |
| def f175(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans, key=lambda pair: pair[0]): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(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((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 | |
| ``` | |
| ``` | |
| f175([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 176 | |
| ```python | |
| def f176(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| finished = set() | |
| listing = [] | |
| groups = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| finished.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 | |
| ``` | |
| ``` | |
| f176(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 177 | |
| ```python | |
| def f177(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() | |
| listing = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| rounds.append(current) | |
| for node in current: | |
| finished.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f177(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 178 | |
| ```python | |
| def f178(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] | |
| growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f178([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 179 | |
| ```python | |
| def f179(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 or len(word) < 2: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in frequency.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f179(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 180 | |
| ```python | |
| def f180(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(set(spans)): | |
| if not segments or left > segments[-1][1] + gap % 5: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f180([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 181 | |
| ```python | |
| def f181(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| result = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), 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 routes[node]: | |
| indegree[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 | |
| ``` | |
| ``` | |
| f181(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 182 | |
| ```python | |
| def f182(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) | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f182([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 183 | |
| ```python | |
| def f183(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows) if value >= limit] | |
| increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f183([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 184 | |
| ```python | |
| def f184(left, right, banned): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').upper() | |
| banned = {str(value).upper() 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((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f184([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 185 | |
| ```python | |
| def f185(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f185(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 186 | |
| ```python | |
| def f186(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f186([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 187 | |
| ```python | |
| def f187(values, width, limit): | |
| values = list(values) | |
| size = width if width > 1 or width < 1 else 2 | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(windows) if value > limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f187([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 188 | |
| ```python | |
| def f188(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[group]) | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f188([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 189 | |
| ```python | |
| def f189(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f189(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 190 | |
| ```python | |
| def f190(board): | |
| board = list(board) | |
| known = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| known.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f190(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 191 | |
| ```python | |
| def f191(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] + min(gap, 2): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f191([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 192 | |
| ```python | |
| def f192(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| indexes = [pos for pos, value in enumerate(windows) if value >= limit] | |
| 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': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f192([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 193 | |
| ```python | |
| def f193(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f193(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 194 | |
| ```python | |
| def f194(nodes, edges): | |
| nodes = list(nodes) | |
| incoming = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| handled = set() | |
| listing = [] | |
| layers = [] | |
| while len(handled) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f194(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 195 | |
| ```python | |
| def f195(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| leftover = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f195([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 196 | |
| ```python | |
| def f196(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| gaps.append(left - combined[pos - 1][1]) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f196([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 197 | |
| ```python | |
| def f197(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = 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 | |
| ``` | |
| ``` | |
| f197(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 198 | |
| ```python | |
| def f198(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): | |
| if not segments or left > segments[-1][1] + max(0, gap): | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = right | |
| 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 | |
| ``` | |
| ``` | |
| f198([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 199 | |
| ```python | |
| def f199(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[:20]: | |
| if key in factors: | |
| 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({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f199([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 200 | |
| ```python | |
| def f200(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(len(values) - size + 1): | |
| series.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| matches = [pos for pos, value in enumerate(series[:64]) if value >= limit] | |
| 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 | |
| ``` | |
| ``` | |
| f200([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 201 | |
| ```python | |
| def f201(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper() | |
| excluded = {tidy(value) for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f201([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 202 | |
| ```python | |
| def f202(board): | |
| board = list(board) | |
| found = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f202(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 203 | |
| ```python | |
| def f203(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f203([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 204 | |
| ```python | |
| def f204(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| startdegree = dict(incoming) | |
| complete = set() | |
| sequence = [] | |
| 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 startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f204(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 205 | |
| ```python | |
| def f205(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| 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([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 206 | |
| ```python | |
| def f206(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) > 12: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f206(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 207 | |
| ```python | |
| def f207(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| finished = set() | |
| result = [] | |
| layers = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=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]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f207(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 208 | |
| ```python | |
| def f208(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| 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[:1] if any((str(node).isupper() for node in nodes)) else 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 | |
| ``` | |
| ``` | |
| f208(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 209 | |
| ```python | |
| def f209(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(prices[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': [code for code, cost in matches[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f209([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 210 | |
| ```python | |
| def f210(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| charge = value * abs(factors[group]) | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| sumvalue += charge | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f210([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 211 | |
| ```python | |
| def f211(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(len(values) - size + 1): | |
| totals.append(sum(values[pos:pos + size])) | |
| indexes = [pos for pos, value in enumerate(totals) if value > limit] | |
| ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': indexes, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f211([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 212 | |
| ```python | |
| def f212(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| amount += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f212([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 213 | |
| ```python | |
| def f213(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip().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 | |
| 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 | |
| ``` | |
| ``` | |
| f213([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 214 | |
| ```python | |
| def f214(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(totals) if value >= limit] | |
| increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': matches, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f214([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 215 | |
| ```python | |
| def f215(left, right, ignored): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper()[:12] | |
| 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 | |
| ``` | |
| ``` | |
| f215([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 216 | |
| ```python | |
| def f216(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] | |
| ups = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f216([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 217 | |
| ```python | |
| def f217(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| pending.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f217(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 218 | |
| ```python | |
| def f218(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| 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 nextmap[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 | |
| ``` | |
| ``` | |
| f218(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 219 | |
| ```python | |
| def f219(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(combined[-1][1], right) | |
| breaks = [] | |
| for pos in range(1, len(combined)): | |
| left = combined[pos][0] | |
| breaks.append(min(100, 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 | |
| ``` | |
| ``` | |
| f219([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 220 | |
| ```python | |
| def f220(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) | |
| breaks = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| breaks.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f220([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 221 | |
| ```python | |
| def f221(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 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 | |
| ``` | |
| ``` | |
| f221([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 222 | |
| ```python | |
| def f222(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f222([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 223 | |
| ```python | |
| def f223(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 and count > 0: | |
| combined.append((key, count * ratemap[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted((key for key, count in left if key not in ratemap)) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f223([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 224 | |
| ```python | |
| def f224(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| letters = {} | |
| for word, count in frequency.items(): | |
| letters[word[0]] = letters.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f224(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 225 | |
| ```python | |
| def f225(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| routes = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| routes[a].append(b) | |
| indegree[b] += 1 | |
| rootsource = dict(indegree) | |
| complete = set() | |
| result = [] | |
| groups = [] | |
| while len(complete) < len(nodes) and len(complete) < 20: | |
| available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete)) | |
| if not available: | |
| break | |
| current = available | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| result.append(node) | |
| for node in current: | |
| for nxt in routes[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f225(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 226 | |
| ```python | |
| def f226(board): | |
| board = list(board) | |
| found = set() | |
| pieces = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in found: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| found.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found): | |
| found.add(nxt) | |
| work.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f226(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 227 | |
| ```python | |
| def f227(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) | |
| handled = set() | |
| listing = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| layers.append(current) | |
| for node in current: | |
| handled.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 rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f227(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 228 | |
| ```python | |
| def f228(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| matches = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] | |
| 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 | |
| ``` | |
| ``` | |
| f228([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 229 | |
| ```python | |
| def f229(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 | |
| ``` | |
| ``` | |
| f229([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 230 | |
| ```python | |
| def f230(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper()[:12] | |
| banned = {tidy(value) for value in banned} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - banned | |
| b = {tidy(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 | |
| ``` | |
| ``` | |
| f230([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 231 | |
| ```python | |
| def f231(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f231([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 232 | |
| ```python | |
| def f232(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(prices[group]) | |
| selected.append((code, score)) | |
| buckets[group] = buckets.get(group, 0) + score | |
| amount += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f232([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 233 | |
| ```python | |
| def f233(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({key for key, value in matched})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f233([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 234 | |
| ```python | |
| def f234(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 | |
| 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 | |
| ``` | |
| ``` | |
| f234([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 235 | |
| ```python | |
| def f235(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| rootsource = dict(incoming) | |
| finished = set() | |
| result = [] | |
| rounds = [] | |
| while len(finished) < len(nodes) and len(finished) < 20: | |
| available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) | |
| 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) | |
| result.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f235(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 236 | |
| ```python | |
| def f236(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if state in keep and value != 0: | |
| cost = value * prices[group] | |
| selected.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| sumvalue += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f236([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 237 | |
| ```python | |
| def f237(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip().upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {normalize(value) for value in left} - excluded | |
| b = {normalize(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f237([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 238 | |
| ```python | |
| def f238(spans, gap): | |
| spans = list(spans) | |
| segments = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): | |
| if not segments or left > segments[-1][1] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(segments[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| gaps.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f238([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 239 | |
| ```python | |
| def f239(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = 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 | |
| ``` | |
| ``` | |
| f239([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 240 | |
| ```python | |
| def f240(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| 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 initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f240(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 241 | |
| ```python | |
| def f241(values, width, limit): | |
| values = list(values) | |
| size = width | |
| windows = [] | |
| for pos in range(len(values) - size + 1): | |
| windows.append(sum(values[pos:pos + size])) | |
| positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit and (limit >= 0 or pos > 0)] | |
| ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) | |
| returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f241([4, 12, 7, 5, 8, 7, 12, 10, 14, 24, 23, 23, 13, 15, 10], 3, 25) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,20,20,27,29,36,48,61,70,59,51,38]} | |
| ``` | |
| ### 242 | |
| ```python | |
| def f242(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': min(256, max(totals)), 'hits': matches, 'rises': ups} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f242([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 243 | |
| ```python | |
| def f243(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(set(spans)): | |
| if not combined or left > combined[-1][1] + gap % 5: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| 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 | |
| ``` | |
| ``` | |
| f243([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 244 | |
| ```python | |
| def f244(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * factors[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(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f244([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 245 | |
| ```python | |
| def f245(board): | |
| board = list(board) | |
| known = set() | |
| components = [] | |
| for row in range(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 | |
| ``` | |
| ``` | |
| f245(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 246 | |
| ```python | |
| def f246(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return ' '.join(str(value).split()).upper() | |
| excluded = {normalize(value) for value in excluded} | |
| a = {normalize(value) for value in left} - 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 | |
| ``` | |
| ``` | |
| f246([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 247 | |
| ```python | |
| def f247(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = right | |
| 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 | |
| ``` | |
| ``` | |
| f247([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 248 | |
| ```python | |
| def f248(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 | |
| 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 | |
| ``` | |
| ``` | |
| f248([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 249 | |
| ```python | |
| def f249(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| charge = value * ratemap[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| grand += charge | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f249([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 250 | |
| ```python | |
| def f250(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| visited.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| work.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f250(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 251 | |
| ```python | |
| def f251(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' or (char == "'" and word): | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f251(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 252 | |
| ```python | |
| def f252(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * factors[group] | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| amount += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f252([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 253 | |
| ```python | |
| def f253(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[: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 | |
| ``` | |
| ``` | |
| f253(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 254 | |
| ```python | |
| def f254(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices or rate0 <= rate0 + 0: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left[:20]: | |
| if key in prices: | |
| 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 | |
| ``` | |
| ``` | |
| f254([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 255 | |
| ```python | |
| def f255(board): | |
| board = list(board) | |
| visited = set() | |
| pieces = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if 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 | |
| ``` | |
| ``` | |
| f255(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 256 | |
| ```python | |
| def f256(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 | |
| ``` | |
| ``` | |
| f256([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 257 | |
| ```python | |
| def f257(rows, keep, ratemap): | |
| rows = list(rows) | |
| kept = [] | |
| 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: | |
| score = value * ratemap[group] | |
| kept.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f257([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 258 | |
| ```python | |
| def f258(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 | |
| ``` | |
| ``` | |
| f258([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 259 | |
| ```python | |
| def f259(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right: | |
| if name0 not in prices and rate0 != 0: | |
| prices[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in prices: | |
| matched.append((key, count * prices[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f259([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 260 | |
| ```python | |
| def f260(board): | |
| board = list(board) | |
| visited = set() | |
| components = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[row])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| if len(visited) >= 100: | |
| continue | |
| queue.append(nxt) | |
| components.append((len(cells), min(cells))) | |
| components.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f260(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 261 | |
| ```python | |
| def f261(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}: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f261([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 262 | |
| ```python | |
| def f262(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 | |
| ``` | |
| ``` | |
| f262([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 263 | |
| ```python | |
| def f263(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] + gap: | |
| segments.append([left, right]) | |
| else: | |
| segments[-1][1] = max(0, segments[-1][1], right) | |
| spaces = [] | |
| for pos in range(1, len(segments)): | |
| left = segments[pos][0] | |
| spaces.append(left - segments[pos - 1][1]) | |
| returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f263([(2, 7), (5, 12), (14, 19), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":40,"holes":[2,6,8,5],"longest":11,"segments":[[2,12],[14,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 264 | |
| ```python | |
| def f264(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| 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 str(state).lower() in {str(item).lower() for item in keep} and value >= 0: | |
| cost = value * factors[group] | |
| selected.append((code, cost)) | |
| groupmap[group] = groupmap.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f264([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 265 | |
| ```python | |
| def f265(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(set(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(min(100, left - result[pos - 1][1])) | |
| returnvalue = {'segments': result, 'cover': sum((min(100, 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 | |
| ``` | |
| ``` | |
| f265([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 266 | |
| ```python | |
| def f266(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| for row in range(min(len(board), 10 + (len(board) - len(board)))): | |
| for col in range(len(board[0])): | |
| if board[row][col] != '#' or (row, col) in visited: | |
| continue | |
| cells = [] | |
| queue = [(row, col)] | |
| visited.add((row, col)) | |
| while queue: | |
| cur = queue.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| queue.append(nxt) | |
| regions.append((len(cells), min(cells))) | |
| regions.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f266(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 267 | |
| ```python | |
| def f267(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 | |
| startdegree = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(finished) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f267(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 268 | |
| ```python | |
| def f268(left, right, banned): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper() | |
| banned = {str(value).strip() for value in banned} | |
| a = {tidy(value) for value in left} - banned | |
| b = {tidy(value) for value in right} - banned | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f268([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 269 | |
| ```python | |
| def f269(board): | |
| board = list(board) | |
| visited = 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 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) | |
| 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 | |
| ``` | |
| ``` | |
| f269(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 270 | |
| ```python | |
| def f270(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens[:64]: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| 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 | |
| ``` | |
| ``` | |
| f270(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 271 | |
| ```python | |
| def f271(values, width, limit): | |
| values = list(values) | |
| size = width | |
| series = [] | |
| for pos in range(min(64, 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] | |
| 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': indexes, 'rises': increases} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f271([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### 272 | |
| ```python | |
| def f272(rows, keep, prices): | |
| rows = list(rows) | |
| selected = [] | |
| totals = {} | |
| sumvalue = 0 | |
| for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| score = value * abs(prices[group]) | |
| selected.append((code, score)) | |
| totals[group] = totals.get(group, 0) + score | |
| sumvalue += score | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f272([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 273 | |
| ```python | |
| def f273(left, right, excluded): | |
| left = list(left) | |
| def canon(value): | |
| return str(value).strip(' \n').upper() | |
| excluded = {str(value).upper() 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 | |
| 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 | |
| ``` | |
| ``` | |
| f273([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 274 | |
| ```python | |
| def f274(left, right, fee): | |
| left = list(left) | |
| ratemap = {} | |
| for name0, rate0 in right: | |
| if name0 not in ratemap: | |
| ratemap[name0] = rate0 | |
| matched = [] | |
| for key, count in left: | |
| if key in ratemap: | |
| matched.append((key, count * ratemap[key] + fee)) | |
| matched.sort(key=lambda item: (-item[1], item[0])) | |
| 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(matched)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f274([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 275 | |
| ```python | |
| def f275(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value != 0: | |
| score = value * prices[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 | |
| ``` | |
| ``` | |
| f275([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 276 | |
| ```python | |
| def f276(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| initial = dict(indegree) | |
| finished = set() | |
| sequence = [] | |
| layers = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| finished.add(node) | |
| 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 | |
| ``` | |
| ``` | |
| f276(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 277 | |
| ```python | |
| def f277(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): | |
| if not result or left > result[-1][1] + gap % 5: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = max(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 | |
| ``` | |
| ``` | |
| f277([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 278 | |
| ```python | |
| def f278(board): | |
| board = list(board) | |
| visited = set() | |
| regions = [] | |
| 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) | |
| 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 | |
| ``` | |
| ``` | |
| f278(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 279 | |
| ```python | |
| def f279(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): | |
| nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) | |
| if not nextbatch: | |
| break | |
| current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch | |
| groups.append(current) | |
| for node in current: | |
| complete.add(node) | |
| 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 | |
| ``` | |
| ``` | |
| f279(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 280 | |
| ```python | |
| def f280(spans, gap): | |
| spans = list(spans) | |
| result = [] | |
| for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])): | |
| if not result or left > result[-1][1] + gap: | |
| result.append([left, right]) | |
| else: | |
| result[-1][1] = right | |
| gaps = [] | |
| for pos in range(1, min(50, len(result))): | |
| left = result[pos][0] | |
| gaps.append(left - result[pos - 1][1]) | |
| returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f280([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 281 | |
| ```python | |
| def f281(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[: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 | |
| tallies[word] = tallies.get(word, 0) + 1 | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f281(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 282 | |
| ```python | |
| def f282(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f282(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 283 | |
| ```python | |
| def f283(board): | |
| board = list(board) | |
| known = set() | |
| regions = [] | |
| for row in range(len(board)): | |
| for col in range(len(board[row])): | |
| if col >= 12 or board[row][col] != '#' or (row, col) in known: | |
| continue | |
| cells = [] | |
| work = [(row, col)] | |
| known.add((row, col)) | |
| while work: | |
| cur = work.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known): | |
| known.add(nxt) | |
| if len(known) >= 100: | |
| continue | |
| work.append(nxt) | |
| 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 | |
| ``` | |
| ``` | |
| f283(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 284 | |
| ```python | |
| def f284(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| 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)) | |
| 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 nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f284(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 285 | |
| ```python | |
| def f285(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| initial = dict(needs) | |
| handled = set() | |
| sequence = [] | |
| rounds = [] | |
| while len(handled) < len(nodes): | |
| available = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not available: | |
| break | |
| current = available[:1] if any((str(node).isupper() for node in nodes)) else available | |
| rounds.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[node]: | |
| needs[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f285(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 286 | |
| ```python | |
| def f286(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' ').upper() | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f286([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 287 | |
| ```python | |
| def f287(spans, gap): | |
| spans = list(spans) | |
| combined = [] | |
| for left, right in sorted(spans): | |
| if not combined or left > combined[-1][1] + gap: | |
| combined.append([left, right]) | |
| else: | |
| combined[-1][1] = max(0, combined[-1][1], right) | |
| gaps = [] | |
| for pos in range(1, min(50, len(combined))): | |
| left = combined[pos][0] | |
| gaps.append(min(100, left - combined[pos - 1][1])) | |
| returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f287([(2, 8), (5, 12), (14, 16), (18, 22), (28, 35), (33, 35), (43, 49), (48, 54), (59, 63)], 1) | |
| ``` | |
| ``` | |
| {"cover":38,"holes":[2,2,6,8,5],"longest":11,"segments":[[2,12],[14,16],[18,22],[28,35],[43,54],[59,63]]} | |
| ``` | |
| ### 288 | |
| ```python | |
| def f288(left, right, excluded): | |
| left = list(left) | |
| def normalize(value): | |
| return str(value).strip(' \r').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 | |
| ``` | |
| ``` | |
| f288([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 289 | |
| ```python | |
| def f289(left, right, fee): | |
| left = list(left) | |
| prices = {} | |
| for name0, rate0 in right[:20]: | |
| if name0 not in prices: | |
| prices[name0] = rate0 | |
| items = [] | |
| for key, count in left: | |
| if key in prices: | |
| items.append((key, count * prices[key] + fee)) | |
| items.sort(key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f289([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 4) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",85],["BR",84],["FP",74],["EV",52],["CX",34]],"missing":["AN","GQ"],"total":357} | |
| ``` | |
| ### 290 | |
| ```python | |
| def f290(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f290([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 291 | |
| ```python | |
| def f291(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) > 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': min(256, sum((len(word) * count for word, count in frequency.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f291(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 292 | |
| ```python | |
| def f292(rows, keep, prices): | |
| rows = list(rows) | |
| matches = [] | |
| buckets = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| cost = value * prices[group] | |
| matches.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| sumvalue += cost | |
| matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(matches), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f292([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 293 | |
| ```python | |
| def f293(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| indegree = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| children[a].append(b) | |
| indegree[b] += 1 | |
| startdegree = dict(indegree) | |
| handled = set() | |
| sequence = [] | |
| layers = [] | |
| while len(handled) < len(nodes) and len(handled) < 20: | |
| nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| layers.append(current) | |
| for node in current: | |
| handled.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| indegree[nxt] -= 1 | |
| returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f293(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 294 | |
| ```python | |
| def f294(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 | |
| ``` | |
| ``` | |
| f294(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 295 | |
| ```python | |
| def f295(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop: | |
| continue | |
| tallies[word] = min(9, tallies.get(word, 0) + 1) | |
| ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) | |
| starts = {} | |
| for word, count in tallies.items(): | |
| starts[word[0]] = starts.get(word[0], 0) + count | |
| returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in tallies.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f295(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 296 | |
| ```python | |
| def f296(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 = [] | |
| pending = [(row, col)] | |
| visited.add((row, col)) | |
| while pending: | |
| cur = pending.pop() | |
| cells.append(cur) | |
| for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)): | |
| nxt = (cur[0] + dr, cur[1] + dc) | |
| if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited): | |
| visited.add(nxt) | |
| pending.append(nxt) | |
| pieces.append((len(cells), min(cells))) | |
| pieces.sort(key=lambda item: (-item[0], item[1])) | |
| returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f296(['###...', '##.###', '#.####', '......', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[1,3],[0,0],[4,3]],"count":3,"filled":19,"sizes":[7,6,6]} | |
| ``` | |
| ### 297 | |
| ```python | |
| def f297(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: | |
| 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': min(256, sum((len(word) * count for word, count in wordmap.items())))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f297(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 298 | |
| ```python | |
| def f298(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) | |
| finished = set() | |
| sequence = [] | |
| 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 | |
| layers.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': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f298(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 299 | |
| ```python | |
| def f299(rows, keep, factors): | |
| rows = list(rows) | |
| selected = [] | |
| buckets = {} | |
| grand = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| cost = value * abs(factors[group]) | |
| selected.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| grand += cost | |
| selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f299([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 300 | |
| ```python | |
| def f300(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| 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 | |
| ``` | |
| ``` | |
| f300(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 301 | |
| ```python | |
| def f301(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 | |
| ``` | |
| ``` | |
| f301(['###...', '##.###', '######', '..#...', '...#.#', '..####']) | |
| ``` | |
| ``` | |
| {"anchors":[[0,0],[4,3]],"count":2,"filled":21,"sizes":[15,6]} | |
| ``` | |
| ### 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 | |
| combined = [] | |
| for key, count in left: | |
| if key in prices: | |
| combined.append((key, count * prices[key] + fee)) | |
| combined.sort(key=lambda item: (-item[1], item[0])) | |
| absent = sorted({key for key, count in left if key not in prices}) | |
| returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f302([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 303 | |
| ```python | |
| def f303(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {word.lower() for word in stop} | |
| terms = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| terms.append(word) | |
| word = '' | |
| if word: | |
| terms.append(word) | |
| for word in terms: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| heads = {} | |
| for word, count in frequency.items(): | |
| heads[word[0]] = heads.get(word[0], 0) + count | |
| returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f303(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 304 | |
| ```python | |
| def f304(lines, stop): | |
| lines = list(lines) | |
| frequency = {} | |
| stop = {str(word) for word in stop} | |
| items = [] | |
| for line in lines[:32]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| items.append(word) | |
| word = '' | |
| if word: | |
| items.append(word) | |
| for word in items: | |
| if word in stop: | |
| continue | |
| frequency[word] = frequency.get(word, 0) + 1 | |
| ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) | |
| 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 | |
| ``` | |
| ``` | |
| f304(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 305 | |
| ```python | |
| def f305(nodes, edges): | |
| nodes = list(nodes) | |
| nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] | |
| incoming = {node: 0 for node in nodes} | |
| children = {node: [] for node in nodes} | |
| for before, after in edges: | |
| if before == after: | |
| continue | |
| a, b = (before, after) | |
| children[a].append(b) | |
| incoming[b] += 1 | |
| initial = dict(incoming) | |
| finished = set() | |
| listing = [] | |
| 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) | |
| 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 initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f305(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 306 | |
| ```python | |
| def f306(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip().upper()[:12] | |
| ignored = {tidy(value) for value in ignored} | |
| a = {tidy(value) for value in left if tidy(value) != tidy(value) or tidy(value) != ''} - ignored | |
| b = {tidy(value) for value in right if tidy(value) != ''} - ignored | |
| both = a & b | |
| result = sorted | |
| returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f306([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 307 | |
| ```python | |
| def f307(rows, keep, ratemap): | |
| rows = list(rows) | |
| selected = [] | |
| groupmap = {} | |
| sumvalue = 0 | |
| for code, group, state, value in rows: | |
| if state in keep and value >= 0: | |
| charge = value * abs(ratemap[group]) | |
| selected.append((code, charge)) | |
| groupmap[group] = groupmap.get(group, 0) + charge | |
| sumvalue += charge | |
| selected.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f307([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 308 | |
| ```python | |
| def f308(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}: | |
| cost = value * abs(prices[group]) | |
| matches.append((code, cost)) | |
| totals[group] = totals.get(group, 0) + cost | |
| amount += cost | |
| matches.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(matches), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in matches)))[:3]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f308([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 309 | |
| ```python | |
| def f309(left, right, ignored): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \n').upper()[:12] | |
| ignored = {str(value).strip() for value in ignored} | |
| a = {tidy(value) for value in left} - ignored | |
| b = {tidy(value) for value in right} - ignored | |
| both = a & b | |
| sequence = sorted | |
| returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f309([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 310 | |
| ```python | |
| def f310(lines, stop): | |
| lines = list(lines) | |
| tallies = {} | |
| stop = {word.lower() for word in stop} | |
| tokens = [] | |
| for line in lines[:20]: | |
| text = line.lower() | |
| word = '' | |
| for char in text[:64]: | |
| if 'a' <= char <= 'z': | |
| word += char | |
| elif word: | |
| tokens.append(word) | |
| word = '' | |
| if word: | |
| tokens.append(word) | |
| for word in tokens: | |
| if word in stop or len(word) < 2: | |
| 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 | |
| ``` | |
| ``` | |
| f310(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 311 | |
| ```python | |
| def f311(nodes, edges): | |
| nodes = list(nodes) | |
| needs = {node: 0 for node in nodes} | |
| nextmap = {node: [] for node in nodes} | |
| for before, after in edges: | |
| a, b = (before, after) | |
| nextmap[a].append(b) | |
| needs[b] += 1 | |
| rootsource = dict(needs) | |
| finished = set() | |
| sequence = [] | |
| groups = [] | |
| while len(finished) < len(nodes): | |
| nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20) | |
| if not nextbatch: | |
| break | |
| current = nextbatch | |
| groups.append(current) | |
| for node in current: | |
| finished.add(node) | |
| sequence.append(node) | |
| for node in current: | |
| for nxt in nextmap[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 | |
| ``` | |
| ``` | |
| f311(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth')]) | |
| ``` | |
| ``` | |
| {"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]} | |
| ``` | |
| ### 312 | |
| ```python | |
| def f312(left, right, excluded): | |
| left = list(left) | |
| def tidy(value): | |
| return str(value).strip(' \r').upper() | |
| excluded = {str(value).strip() for value in excluded} | |
| a = {tidy(value) for value in left} - excluded | |
| b = {tidy(value) for value in right} - excluded | |
| both = a & b | |
| 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 | |
| ``` | |
| ``` | |
| f312([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUEx', 'delta', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUEX","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","DELTA","GARNET","HELIUM"],"left":["AMBER","IVORY"],"right":["BLUEX","FROST"],"score":21} | |
| ``` | |
| ### 313 | |
| ```python | |
| def f313(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 | |
| ``` | |
| ``` | |
| f313([('AN', 3), ('BR', 10), ('CX', 3), ('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9)], [('BR', 8), ('CX', 10), ('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9)], 3) | |
| ``` | |
| ``` | |
| {"count":6,"items":[["HR",84],["BR",83],["FP",73],["EV",51],["CX",33]],"missing":["AN","GQ"],"total":351} | |
| ``` | |
| ### 314 | |
| ```python | |
| def f314(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])) | |
| 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 | |
| ``` | |
| ``` | |
| f314(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['delta']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"ember":5,"frost":3,"garnet":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"e":5,"f":3,"g":3,"h":3},"size":126,"top":["amber","ember","blue","frost","garnet"]} | |
| ``` | |
| ### 315 | |
| ```python | |
| def f315(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 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 | |
| ``` | |
| ``` | |
| f315([' Amber', 'helium', 'ivory', 'GARNET', 'cyan ', 'delta', 'Ember'], ['helium', 'BLUE', 'deltax', 'cyan ', 'frost', 'GARNET', 'Ember'], ['EMBER']) | |
| ``` | |
| ``` | |
| {"all":["AMBER","BLUE","CYAN","DELTA","DELTAX","FROST","GARNET","HELIUM","IVORY"],"both":["CYAN","GARNET","HELIUM"],"left":["AMBER","DELTA","IVORY"],"right":["BLUE","DELTAX","FROST"],"score":16} | |
| ``` | |
| ### 316 | |
| ```python | |
| def f316(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() | |
| listing = [] | |
| 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) | |
| listing.append(node) | |
| for node in current: | |
| for nxt in children[node]: | |
| incoming[nxt] -= 1 | |
| returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f316(['proxy', 'ui', 'cache', 'auth', 'db', 'worker', 'api', 'jobs'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) | |
| ``` | |
| ``` | |
| {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} | |
| ``` | |
| ### 317 | |
| ```python | |
| def f317(rows, keep, factors): | |
| rows = list(rows) | |
| kept = [] | |
| buckets = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if str(state).lower() in {str(item).lower() for item in keep}: | |
| cost = value * factors[group] | |
| kept.append((code, cost)) | |
| buckets[group] = buckets.get(group, 0) + cost | |
| amount += cost | |
| kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) | |
| returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': amount, 'groups': buckets, 'top': [code for code, cost in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f317([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":15,"north":357,"south":135},"top":["AN45","AN60","DL30"],"total":507} | |
| ``` | |
| ### 318 | |
| ```python | |
| def f318(rows, keep, prices): | |
| rows = list(rows) | |
| kept = [] | |
| totals = {} | |
| amount = 0 | |
| for code, group, state, value in rows: | |
| if state in keep: | |
| charge = value * abs(prices[group]) | |
| kept.append((code, charge)) | |
| totals[group] = totals.get(group, 0) + charge | |
| amount += charge | |
| kept.sort(key=lambda item: (-item[1], item[0])) | |
| returnvalue = {'accepted': len(kept), 'total': amount, 'groups': totals, 'top': [code for code, charge in kept[:3]]} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f318([('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15), ('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 3}) | |
| ``` | |
| ``` | |
| {"accepted":8,"groups":{"edge":27,"north":357,"south":130},"top":["AN45","AN60","DL30"],"total":514} | |
| ``` | |
| ### 319 | |
| ```python | |
| def f319(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 | |
| 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 | |
| ``` | |
| ``` | |
| f319(['Delta, blue. blue amber', 'Frost, delta. delta cyan ember', 'Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber'], ['garnet']) | |
| ``` | |
| ``` | |
| {"counts":{"amber":6,"blue":3,"cyan":2,"delta":6,"ember":5,"frost":3,"helium":3},"initials":{"a":6,"b":3,"c":2,"d":6,"e":5,"f":3,"h":3},"size":138,"top":["amber","delta","ember","blue","frost"]} | |
| ``` | |
| ### 320 | |
| ```python | |
| def f320(values, width, limit): | |
| values = list(values) | |
| size = width | |
| totals = [] | |
| for pos in range(min(64, len(values) - size + 1)): | |
| totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) | |
| positions = [pos for pos, value in enumerate(totals) if value >= limit] | |
| growth = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) | |
| returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} | |
| output = dict() | |
| output.update(returnvalue) | |
| return output | |
| ``` | |
| ``` | |
| f320([4, 12, 7, 5, 13, 7, 12, 10, 14, 24, 23, 23, 13, 11, 10], 3, 24) | |
| ``` | |
| ``` | |
| {"best":70,"hits":[1,2,3,4,5,6,7,8,9,10,11,12],"rises":7,"sums":[23,24,25,25,32,29,36,48,61,70,59,47,34]} | |
| ``` | |
| ### Final Query | |
| Give me the output of the final query without executing it! | |
| ```python | |
| import json, re | |
| from collections import deque | |
| out = {} | |
| left = list((('AN', 3), ('BR', 10), ('CX', 3)) + (('DL', 4), ('EV', 8), ('FP', 10), ('GQ', 6), ('HR', 9))) | |
| right = list((('BR', 8), ('CX', 10)) + (('DL', 6), ('EV', 6), ('FP', 7), ('HR', 9))) | |
| fee = 3 | |
| 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)} | |
| badge = record['bb'] | |
| nodes = list(('proxy', 'ui', 'cache', 'auth', 'db') + ('worker', 'api', 'jobs')) | |
| route = badge | |
| cases = { | |
| 351: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db'))), | |
| 357: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('db', 'auth'))), | |
| } | |
| 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)} | |
| permit = record['aa'][0] | |
| width = 3 | |
| route = badge | |
| cases = { | |
| 357: (list((4, 12, 7, 5, 8, 7, 12) + (10, 14, 24, 23, 23, 13, 15, 10)), 25), | |
| 351: (list((4, 12, 7, 5, 13, 7, 12) + (10, 14, 24, 23, 23, 13, 11, 10)), 24), | |
| } | |
| values, limit = cases[route] | |
| current = sum(values[:width]) | |
| sums = [current] | |
| for pos in range(width, len(values)): | |
| current += values[pos] - values[pos - width] | |
| sums.append(current) | |
| hits = [] | |
| rises = 0 | |
| for pos, value in enumerate(sums): | |
| if value >= limit: | |
| hits.append(pos) | |
| if pos and value > sums[pos - 1]: | |
| rises += 1 | |
| record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} | |
| signal = record['aa'][0] | |
| keep = list(('ready',) + ('checked',)) | |
| rates = dict((('north', 7),) + (('south', 5), ('core', 11), ('edge', 3))) | |
| route = (permit, signal) | |
| cases = { | |
| ('auth', 1): list((('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 13), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15)) + (('AN71', 'edge', 'checked', 5), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10))), | |
| ('auth', 4): list((('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15)) + (('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10))), | |
| ('cache', 1): list((('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15)) + (('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10))), | |
| ('cache', 4): list((('AN49', 'edge', 'void', 19), ('BR86', 'edge', 'hold', 21), ('CX23', 'south', 'checked', 12), ('AN60', 'north', 'ready', 14), ('DL97', 'south', 'ready', 11), ('AN34', 'edge', 'void', 15)) + (('AN71', 'edge', 'checked', 9), ('BR08', 'north', 'hold', 15), ('AN45', 'north', 'checked', 21), ('BR82', 'south', 'ready', 3), ('DL19', 'south', 'hold', 8), ('DL56', 'north', 'ready', 6), ('AN93', 'core', 'void', 22), ('DL30', 'north', 'ready', 10))), | |
| } | |
| rows = 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'] | |
| gap = 1 | |
| route = token | |
| cases = { | |
| 514: list(((2, 8), (5, 12), (14, 16), (18, 22), (28, 35)) + ((33, 35), (43, 49), (48, 54), (59, 63))), | |
| 507: list(((2, 7), (5, 12), (14, 19), (18, 22), (28, 35)) + ((33, 35), (43, 49), (48, 54), (59, 63))), | |
| } | |
| spans = cases[route] | |
| spans.sort(key=lambda pair: (pair[0], pair[1])) | |
| parts = [] | |
| pos = 0 | |
| while pos < len(spans): | |
| left, right = spans[pos] | |
| pos += 1 | |
| while pos < len(spans) and spans[pos][0] <= right + gap: | |
| right = max(right, spans[pos][1]) | |
| pos += 1 | |
| parts.append([left, right]) | |
| holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))] | |
| record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))} | |
| memo = record['cc'] | |
| left = list((' Amber', 'helium', 'ivory') + ('GARNET', 'cyan ', 'delta', 'Ember')) | |
| blocked = ['EMBER'] | |
| route = memo | |
| cases = { | |
| 40: list(('helium', 'BLUEx', 'delta') + ('cyan ', 'frost', 'GARNET', 'Ember')), | |
| 38: list(('helium', 'BLUE', 'deltax') + ('cyan ', 'frost', 'GARNET', 'Ember')), | |
| } | |
| right = 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))} | |
| clue = record['ee'] | |
| lines = list(('Delta, blue. blue amber', 'Frost, delta. delta cyan ember') + ('Amber, garnet. delta amber', 'Frost, amber. garnet helium ember', 'Delta, delta. amber cyan', 'Blue, ember. ember helium frost', 'Helium, ember. garnet amber')) | |
| route = clue | |
| cases = { | |
| 16: ['garnet'], | |
| 21: ['delta'], | |
| } | |
| stop = cases[route] | |
| normalized = set() | |
| for item in stop: | |
| normalized.add(item.lower()) | |
| stop = normalized | |
| words = [] | |
| for line in lines: | |
| words.extend(re.findall('[a-z]+', line.lower())) | |
| words = [word for word in words if word not in stop] | |
| counts = {} | |
| initials = {} | |
| for word in words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| initials[word[0]] = initials.get(word[0], 0) + 1 | |
| top = sorted(counts, key=lambda word: (-counts[word], word))[:5] | |
| record = {'aa': top, 'bb': counts, 'cc': initials, 'dd': sum((len(word) for word in words))} | |
| stamp = record['dd'] | |
| route = stamp | |
| cases = { | |
| 126: list(('###...', '##.###', '######', '..#...') + ('...#.#', '..####')), | |
| 138: list(('###...', '##.###', '#.####', '......') + ('...#.#', '..####')), | |
| } | |
| board = cases[route] | |
| cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#'] | |
| parent = {cell: cell for cell in cells} | |
| def root(cell): | |
| while parent[cell] != cell: | |
| parent[cell] = parent[parent[cell]] | |
| cell = parent[cell] | |
| return cell | |
| def merge(a, b): | |
| a = root(a) | |
| b = root(b) | |
| if a != b: | |
| parent[b] = a | |
| for row, col in cells: | |
| for other in ((row - 1, col), (row, col - 1)): | |
| if other in parent: | |
| merge((row, col), other) | |
| groups = {} | |
| for cell in cells: | |
| groups.setdefault(root(cell), []).append(cell) | |
| parts = [(len(group), min(group)) for group in groups.values()] | |
| parts.sort(key=lambda item: (-item[0], item[1])) | |
| record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)} | |
| out['aa'] = record['aa'] | |
| out['bb'] = record['bb'] | |
| out['cc'] = record['cc'] | |
| out['dd'] = record['dd'] | |
| print(json.dumps(out, separators=(",", ":"), sort_keys=True)) | |
| ``` | |