### 001 ```python def f1(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} terms = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f1(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 002 ```python def f2(left, right, banned): left = list(left) def canon(value): return str(value).strip(' ').upper()[:12] banned = {str(value).upper() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f2(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 003 ```python def f3(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 ``` Example call: ``` f3(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 004 ```python def f4(left, right, ignored): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f4(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 005 ```python def f5(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]))) positions = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f5([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 006 ```python def f6(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * ratemap[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f6([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 007 ```python def f7(rows, keep, prices): rows = list(rows) kept = [] buckets = {} amount = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: cost = value * prices[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': list(dict.fromkeys((code for code, cost in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f7([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 008 ```python def f8(rows, keep, factors): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: cost = value * factors[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f8([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 009 ```python def f9(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) 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 ``` Example call: ``` f9(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 010 ```python def f10(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) handled = set() sequence = [] groups = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) if not available: break current = available groups.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f10(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 011 ```python def f11(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 ``` Example call: ``` f11([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 012 ```python def f12(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap + 1: segments.append([left, right]) else: segments[-1][1] = right breaks = [] for pos in range(1, len(segments)): left = segments[pos][0] breaks.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f12([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 013 ```python def f13(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}: charge = value * prices[group] selected.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f13([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 014 ```python def f14(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop or len(word) < 2: continue 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 ``` Example call: ``` f14(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 015 ```python def f15(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) if value >= limit and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f15([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 016 ```python def f16(left, right, ignored): left = list(left) def canon(value): return str(value).strip(' \r').upper() ignored = {str(value).upper() for value in ignored} a = {canon(value) for value in left} - ignored b = {canon(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f16(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 017 ```python def f17(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])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f17(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 018 ```python def f18(rows, keep, prices): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * abs(prices[group]) selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f18([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 019 ```python def f19(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' ').upper() excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f19(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 020 ```python def f20(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in tallies.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f20(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 021 ```python def f21(rows, keep, factors): rows = list(rows) matches = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: charge = value * factors[group] matches.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f21([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 022 ```python def f22(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(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f22([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 023 ```python def f23(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f23([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 024 ```python def f24(rows, keep, factors): 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 str(state).lower() in {str(item).lower() for item in keep}: score = value * factors[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(dict.fromkeys(kept)), 'total': sumvalue, 'groups': totals, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f24([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 025 ```python def f25(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 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 ``` Example call: ``` f25(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 026 ```python def f26(spans, gap): spans = list(spans) segments = [] for left, right in sorted(set(spans)): if not segments or left >= segments[-1][1] + max(0, gap): segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f26([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 027 ```python def f27(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] ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f27([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 028 ```python def f28(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + max(0, gap): result.append([left, right]) else: result[-1][1] = right spaces = [] for pos in range(1, len(result)): left = result[pos][0] spaces.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f28([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 029 ```python def f29(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): routes.setdefault(a, []).append(b) steps = {start: 0} pending = [start] while pending: cur = pending.pop(0) if steps[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 pending.append(nxt) sequence = list(steps) lastlevel = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': lastlevel, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f29([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 030 ```python def f30(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(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f30([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 031 ```python def f31(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])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f31(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 032 ```python def f32(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f32([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 033 ```python def f33(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(nextmap.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f33([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 034 ```python def f34(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * prices[group] selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost amount += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f34([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 035 ```python def f35(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' ').upper() ignored = {normalize(value) for value in ignored} a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - ignored b = {normalize(value) for value in right} - ignored both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f35(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 036 ```python def f36(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f36([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 037 ```python def f37(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): routes.setdefault(a, []).append(b) steps = {start: 0} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) result = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': result, 'depth': steps, 'frontier': furthest[:3], 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f37([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 038 ```python def f38(left, right, banned): left = list(left) def canon(value): return str(value).strip(' \r').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} - 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 ``` Example call: ``` f38(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 039 ```python def f39(rows, keep, prices): rows = list(rows) matches = [] groupmap = {} 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: score = value * abs(prices[group]) matches.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score amount += score matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': groupmap, 'top': [code for code, score in matches[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f39([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 040 ```python def f40(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): 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) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f40(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 041 ```python def f41(left, right, ignored): left = list(left) def normalize(value): return str(value).strip(' \n').upper() ignored = {str(value).strip() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f41(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 042 ```python def f42(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f42([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 043 ```python def f43(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper() ignored = {str(value).strip() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f43(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 044 ```python def f44(rows, keep, prices): rows = list(rows) matches = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] matches.append((code, charge)) totals[group] = totals.get(group, 0) + charge grand += charge matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, charge in matches[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f44([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 045 ```python def f45(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] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f45([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 046 ```python def f46(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((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f46(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 047 ```python def f47(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f47([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 048 ```python def f48(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, min(65, 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 ``` Example call: ``` f48([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 049 ```python def f49(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): nextmap.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f49([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 050 ```python def f50(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f50([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 051 ```python def f51(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f51([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 052 ```python def f52(left, right, banned): left = list(left) def canon(value): return ' '.join(str(value).split()).upper() banned = {str(value).strip() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f52(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 053 ```python def f53(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f53([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 054 ```python def f54(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) handled = set() listing = [] layers = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available layers.append(current) for node in current: handled.add(node) 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 ``` Example call: ``` f54(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 055 ```python def f55(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f55(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 056 ```python def f56(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 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f56(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 057 ```python def f57(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(totals[: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': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f57([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 058 ```python def f58(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(totals) if value > limit] growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': max(totals), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f58([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 059 ```python def f59(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f59([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 060 ```python def f60(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f60([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 061 ```python def f61(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f61(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 062 ```python def f62(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] indegree = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) indegree[b] += 1 initial = dict(indegree) finished = set() listing = [] rounds = [] 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 rounds.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f62(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 063 ```python def f63(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] rounds = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available rounds.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in 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 ``` Example call: ``` f63(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 064 ```python def f64(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in tallies.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f64(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 065 ```python def f65(values, width, limit): values = list(values) size = width series = [] for pos in range(min(64, len(values) - size + 1)): series.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(series[:64]) if value >= limit and (limit >= 0 or pos > 0)] ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f65([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 066 ```python def f66(spans, gap): spans = list(spans) combined = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) breaks = [] for pos in range(1, len(combined)): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f66([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 067 ```python def f67(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(prices[group]) selected.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f67([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 068 ```python def f68(rows, keep, factors): rows = list(rows) matches = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: cost = value * factors[group] matches.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f68([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 069 ```python def f69(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f69([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 070 ```python def f70(lines, stop): lines = list(lines) wordmap = {} 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 wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f70(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 071 ```python def f71(left, right, banned): left = list(left) def canon(value): return str(value).strip().upper() banned = {str(value).upper() for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f71(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 072 ```python def f72(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] layers = [] 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 layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f72(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 073 ```python def f73(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f73([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 074 ```python def f74(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans): if not result or left > result[-1][1] + gap % 5: result.append([left, right]) else: result[-1][1] = max(0, result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f74([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 075 ```python def f75(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f75(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 076 ```python def f76(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 or len(word) < 2: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f76(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 077 ```python def f77(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f77(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 078 ```python def f78(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f78([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 079 ```python def f79(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap % 5 + 1: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f79([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 080 ```python def f80(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 initial = dict(indegree) finished = set() listing = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in routes[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f80(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 081 ```python def f81(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f81([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 082 ```python def f82(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f82([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 083 ```python def f83(left, right, banned): left = list(left) def canon(value): return str(value).strip(' \r').upper() banned = {canon(value) for value in banned} a = {canon(value) for value in left} - banned b = {canon(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f83(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 084 ```python def f84(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] 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 children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f84(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 085 ```python def f85(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) finished = set() listing = [] groups = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f85(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 086 ```python def f86(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap % 5 + 1: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f86([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 087 ```python def f87(lines, stop): lines = list(lines) tallies = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' 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 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': min(256, sum((len(word) * count for word, count in tallies.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f87(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 088 ```python def f88(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 pending.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f88([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 089 ```python def f89(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f89([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 090 ```python def f90(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop or len(word) > 12: continue frequency[word] = frequency.get(word, 0) + 1 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f90(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 091 ```python def f91(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): nextmap.setdefault(a, []).append(b) steps = {start: 0} pending = [start] while pending: cur = pending.pop(0) if steps[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in steps: steps[nxt] = steps[cur] + 1 pending.append(nxt) listing = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': listing, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f91([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 092 ```python def f92(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], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f92([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 093 ```python def f93(left, right, excluded): left = list(left) def tidy(value): return str(value).strip(' \n').upper() excluded = {str(value).strip() for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f93(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 094 ```python def f94(rows, keep, prices): rows = list(rows) selected = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge sumvalue += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(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 ``` Example call: ``` f94([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 095 ```python def f95(rows, keep, ratemap): rows = list(rows) selected = [] groupmap = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value != 0: charge = value * ratemap[group] selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge sumvalue += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f95([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 096 ```python def f96(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(series) if value >= limit] ups = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f96([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 097 ```python def f97(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f97([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 098 ```python def f98(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) levels = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) sequence = list(levels) lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f98([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 099 ```python def f99(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f99(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 100 ```python def f100(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper() ignored = {str(value).strip() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f100(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 101 ```python def f101(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x = max(-64, min(64, x + dx)) y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f101([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 102 ```python def f102(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep and value >= 0: score = value * abs(ratemap[group]) selected.append((code, score)) totals[group] = totals.get(group, 0) + score grand += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f102([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 103 ```python def f103(rows, keep, ratemap): rows = list(rows) selected = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: cost = value * ratemap[group] selected.append((code, cost)) buckets[group] = buckets.get(group, 0) + cost sumvalue += cost selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f103([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 104 ```python def f104(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() result = [] groups = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available groups.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f104(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 105 ```python def f105(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) needs[b] += 1 startdegree = dict(needs) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not available: break current = available rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in routes[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f105(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 106 ```python def f106(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) result = list(levels) edge = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': result, 'depth': levels, 'frontier': edge, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f106([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 107 ```python def f107(left, right, ignored): left = list(left) def canon(value): return str(value).strip().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 ``` Example call: ``` f107(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 108 ```python def f108(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + max(0, gap) + 1: 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((min(100, right - left) for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f108([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 109 ```python def f109(left, right, excluded): left = list(left) def tidy(value): return ' '.join(str(value).split()).upper() excluded = {str(value).strip() 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} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f109(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 110 ```python def f110(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() result = [] layers = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available layers.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in 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 ``` Example call: ``` f110(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 111 ```python def f111(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] groups = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch groups.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f111(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 112 ```python def f112(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f112([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 113 ```python def f113(edges, start, limit): edges = list(edges) routes = {} for a, b in edges: routes.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} if limit > 0 or limit < 0 else {} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f113([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 114 ```python def f114(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f114([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 115 ```python def f115(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 listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f115(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 116 ```python def f116(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] increases = 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': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f116([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 117 ```python def f117(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not combined or left > combined[-1][1] + max(0, gap): combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in combined)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f117([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 118 ```python def f118(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 sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f118(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 119 ```python def f119(rows, keep, factors): rows = list(rows) kept = [] buckets = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep: cost = value * factors[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': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f119([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 120 ```python def f120(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) edge = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': edge, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f120([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 121 ```python def f121(left, right, ignored): left = list(left) def tidy(value): return str(value).strip(' \r').upper() ignored = {str(value).strip() for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f121(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 122 ```python def f122(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f122([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 123 ```python def f123(spans, gap): spans = list(spans) result = [] for left, right in sorted(set(spans)): if not result or left > result[-1][1] + gap + 1: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) breaks = [] for pos in range(1, len(result)): left = result[pos][0] breaks.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f123([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 124 ```python def f124(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y = max(-64, min(64, y + dy)) used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f124([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 125 ```python def f125(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] 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) complete = set() sequence = [] groups = [] while len(complete) < len(nodes) and len(complete) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch groups.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f125(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 126 ```python def f126(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() result = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f126(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 127 ```python def f127(rows, keep, prices): rows = list(rows) selected = [] groupmap = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: charge = value * abs(prices[group]) selected.append((code, charge)) groupmap[group] = groupmap.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f127([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 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(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f128([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 129 ```python def f129(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(series[:64]) if value >= limit] growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] >= series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f129([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 130 ```python def f130(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) breaks = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f130([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 131 ```python def f131(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f131([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 132 ```python def f132(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum((max(item, 0) for item in values[pos:pos + size]))) positions = [pos for pos, value in enumerate(totals) if value >= limit] 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': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f132([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 133 ```python def f133(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 sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f133(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 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] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f134([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 135 ```python def f135(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + gap + 1: segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, min(50, len(segments))): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f135([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 136 ```python def f136(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f136([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 137 ```python def f137(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f137([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 138 ```python def f138(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans, key=lambda pair: (pair[1], pair[0])): if not result or left > result[-1][1] + min(gap, 2): result.append([left, right]) else: result[-1][1] = max(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 ``` Example call: ``` f138([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 139 ```python def f139(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * abs(factors[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(selected), 'total': amount, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f139([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 140 ```python def f140(values, width, limit): values = list(values) size = width windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f140([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 141 ```python def f141(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in known and list(point) not in crossings: crossings.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f141([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 142 ```python def f142(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop or len(word) < 2: continue wordmap[word] = min(9, wordmap.get(word, 0) + 1) ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f142(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 143 ```python def f143(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(series[:64]) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1])) returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f143([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 144 ```python def f144(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[: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': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f144([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 145 ```python def f145(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f145(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 146 ```python def f146(rows, keep, ratemap): 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} and value >= 0: cost = value * ratemap[group] matches.append((code, cost)) totals[group] = totals.get(group, 0) + cost amount += cost matches.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, cost in matches[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f146([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 147 ```python def f147(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, len(combined)): left = combined[pos][0] gaps.append(min(100, left - combined[pos - 1][1])) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f147([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 148 ```python def f148(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) complete = set() sequence = [] layers = [] while len(complete) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch layers.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f148(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 149 ```python def f149(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) indegree[b] += 1 initial = dict(indegree) finished = set() listing = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=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 nextmap[node]: indegree[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f149(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 150 ```python def f150(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() result = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete)) if not available: break current = available layers.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f150(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 151 ```python def f151(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(0, combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f151([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 152 ```python def f152(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() listing = [] groups = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20) if not available: break current = available groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f152(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 153 ```python def f153(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) incoming[b] += 1 rootsource = dict(incoming) finished = set() listing = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in 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 ``` Example call: ``` f153(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 154 ```python def f154(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f154(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 155 ```python def f155(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] = right spaces = [] for pos in range(1, len(result)): left = result[pos][0] spaces.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f155([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 156 ```python def f156(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms[:64]: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f156(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 157 ```python def f157(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': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f157([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 158 ```python def f158(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f158([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 159 ```python def f159(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 work.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f159([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 160 ```python def f160(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < min(limit, 3): for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) sequence = list(steps) edge = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': sequence, 'depth': steps, 'frontier': edge, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f160([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 161 ```python def f161(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() listing = [] groups = [] 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 groups.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': listing, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f161(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 162 ```python def f162(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(min(64, len(values) - size + 1)): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f162([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 163 ```python def f163(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f163([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 164 ```python def f164(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 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 ``` Example call: ``` f164(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 165 ```python def f165(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() listing = [] layers = [] while len(handled) < len(nodes) and len(handled) < 20: available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled)) if not available: break current = available layers.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': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f165(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 166 ```python def f166(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) needs[b] += 1 startdegree = dict(needs) finished = set() sequence = [] layers = [] while len(finished) < len(nodes) and len(finished) < 20: nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f166(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 167 ```python def f167(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, min(50, len(combined))): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f167([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 168 ```python def f168(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f168([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 169 ```python def f169(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap % 5: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f169([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 170 ```python def f170(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \n').upper() excluded = {str(value).upper() for value in excluded} a = {normalize(value) for value in left} - excluded b = {normalize(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f170(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 171 ```python def f171(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f171([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 172 ```python def f172(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in levels and (not nxt.startswith('Z')): levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f172([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 173 ```python def f173(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, min(65, len(series))) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f173([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 174 ```python def f174(rows, keep, prices): rows = list(rows) kept = [] totals = {} amount = 0 for code, group, state, value in rows: 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(kept), 'total': amount, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f174([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 175 ```python def f175(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f175([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 176 ```python def f176(values, width, limit): values = list(values) size = width series = [] for pos in range(len(values) - size + 1): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value >= limit] growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f176([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 177 ```python def f177(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(selected), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f177([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 178 ```python def f178(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f178([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 179 ```python def f179(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 ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f179(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 180 ```python def f180(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \n').upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f180(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 181 ```python def f181(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f181([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 182 ```python def f182(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in returns: returns.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f182([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 183 ```python def f183(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f183([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 184 ```python def f184(left, right, ignored): left = list(left) def tidy(value): return str(value).strip().upper() ignored = {tidy(value) for value in ignored} a = {tidy(value) for value in left} - ignored b = {tidy(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f184(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 185 ```python def f185(values, width, limit): values = list(values) size = width totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(totals) if value >= limit] ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f185([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 186 ```python def f186(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) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f186([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 187 ```python def f187(left, right, excluded): left = list(left) def canon(value): return ' '.join(str(value).split()).upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left if canon(value) != canon(value) or canon(value) != ''} - 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 ``` Example call: ``` f187(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 188 ```python def f188(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue frequency[word] = min(9, 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 ``` Example call: ``` f188(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 189 ```python def f189(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f189([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 190 ```python def f190(rows, keep, factors): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value >= 0: score = value * factors[group] kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f190([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 191 ```python def f191(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) complete = set() sequence = [] groups = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available groups.append(current) for node in current: complete.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f191(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 192 ```python def f192(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + max(0, gap) + 1: combined.append([left, right]) else: combined[-1][1] = right breaks = [] for pos in range(1, len(combined)): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f192([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 193 ```python def f193(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f193([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 194 ```python def f194(left, right, excluded): left = list(left) def tidy(value): return ' '.join(str(value).split()).upper() excluded = {str(value).upper() for value in excluded} a = {tidy(value) for value in left} - excluded b = {tidy(value) for value in right} - excluded both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f194(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 195 ```python def f195(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f195([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 196 ```python def f196(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() listing = [] rounds = [] 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[:1] if any((str(node).isupper() for node in nodes)) else nextbatch rounds.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: 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 ``` Example call: ``` f196(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 197 ```python def f197(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + min(gap, 2): segments.append([left, right]) else: segments[-1][1] = max(segments[-1][1], right) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((min(100, right - left) for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f197([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 198 ```python def f198(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() result = [] groups = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[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 children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f198(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 199 ```python def f199(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] increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1])) returnvalue = {'sums': series, 'best': max(series), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f199([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 200 ```python def f200(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 work.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f200([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 201 ```python def f201(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): nextmap.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in (node for node in sorted(nextmap.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f201([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 202 ```python def f202(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 if tidy(value) != ''} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f202(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 203 ```python def f203(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: charge = value * prices[group] selected.append((code, charge)) buckets[group] = buckets.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f203([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 204 ```python def f204(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in found and list(point) not in crossings: crossings.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f204([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 205 ```python def f205(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f205([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 206 ```python def f206(left, right, excluded): left = list(left) def canon(value): return ' '.join(str(value).split()).upper() excluded = {str(value).upper() for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f206(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 207 ```python def f207(rows, keep, prices): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: cost = value * abs(prices[group]) kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f207([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 208 ```python def f208(left, right, excluded): left = list(left) def normalize(value): return str(value).strip(' \n').upper() excluded = {str(value).strip() 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 ``` Example call: ``` f208(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 209 ```python def f209(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans, key=lambda pair: pair[0]): 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, min(50, len(result))): left = result[pos][0] gaps.append(min(100, left - result[pos - 1][1])) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f209([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 210 ```python def f210(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 totals = [] for pos in range(len(values) - size + 1): totals.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(totals) if value >= limit] ups = sum((1 for pos in range(1, min(65, len(totals))) if totals[pos] > totals[pos - 1])) returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f210([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 211 ```python def f211(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.setdefault(a, []).append(b) levels = {start: 0} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, []))[:20]: if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) sequence = list(levels) edge = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': edge, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f211([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 212 ```python def f212(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items[:64]: if word in stop or len(word) > 12: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f212(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 213 ```python def f213(nodes, edges): nodes = list(nodes) nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))] 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() result = [] groups = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) if not available: break current = available[:1] if any((str(node).isupper() for node in nodes)) else available groups.append(current) for node in current: complete.add(node) result.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f213(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 214 ```python def f214(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f214(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 215 ```python def f215(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) indegree[b] += 1 initial = dict(indegree) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes) and len(handled) < 20: nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: indegree[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f215(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 216 ```python def f216(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f216([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 217 ```python def f217(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) handled = set() result = [] rounds = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: handled.add(node) result.append(node) for node in current: for nxt in routes[node]: indegree[nxt] -= 1 returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(result)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f217(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 218 ```python def f218(nodes, edges): nodes = list(nodes) indegree = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) indegree[b] += 1 rootsource = dict(indegree) finished = set() listing = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished)) if not nextbatch: break current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in children[node]: indegree[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 ``` Example call: ``` f218(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 219 ```python def f219(lines, stop): lines = list(lines) wordmap = {} stop = {str(word) for word in stop} tokens = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in wordmap.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f219(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 220 ```python def f220(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])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f220(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 221 ```python def f221(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: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f221(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 222 ```python def f222(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep and value != 0: score = value * 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(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f222([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 223 ```python def f223(spans, gap): spans = list(spans) result = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not result or left > result[-1][1] + gap % 5 + 1: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f223([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 224 ```python def f224(values, width, limit): values = list(values) size = width if width > 1 or width < 1 else 2 windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit 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': min(256, max(windows)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f224([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 225 ```python def f225(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 if normalize(value) != normalize(value) or normalize(value) != ''} - 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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f225(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 226 ```python def f226(rows, keep, prices): rows = list(rows) kept = [] totals = {} sumvalue = 0 for code, group, state, value in rows: if state in keep and value >= 0: charge = value * prices[group] kept.append((code, charge)) totals[group] = totals.get(group, 0) + charge sumvalue += charge kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f226([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 227 ```python def f227(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) gaps = [] for pos in range(1, len(segments)): left = segments[pos][0] gaps.append(left - segments[pos - 1][1]) returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f227([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 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])) indexes = [pos for pos, value in enumerate(windows[:64]) if value > limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f228([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 229 ```python def f229(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f229([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 230 ```python def f230(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} routes = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) routes[a].append(b) incoming[b] += 1 initial = dict(incoming) finished = set() sequence = [] groups = [] while len(finished) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not available: break current = available groups.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in routes[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f230(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 231 ```python def f231(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f231([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 232 ```python def f232(spans, gap): spans = list(spans) combined = [] for left, right in sorted(spans): if not combined or left > combined[-1][1] + gap + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f232([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 233 ```python def f233(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' \r').upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f233(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 234 ```python def f234(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f234([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 235 ```python def f235(spans, gap): spans = list(spans) combined = [] for left, right in sorted(set(spans)): if not combined or left > combined[-1][1] + max(0, gap) + 1: combined.append([left, right]) else: combined[-1][1] = max(combined[-1][1], right) breaks = [] for pos in range(1, len(combined)): left = combined[pos][0] breaks.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f235([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 236 ```python def f236(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 64)): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f236([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 237 ```python def f237(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in known and list(point) not in repeats: repeats.append(list(point)) known.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f237([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 238 ```python def f238(spans, gap): spans = list(spans) segments = [] for left, right in sorted(spans): if not segments or left > segments[-1][1] + max(0, gap) + 1: 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 ``` Example call: ``` f238([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 239 ```python def f239(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}: cost = value * abs(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': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f239([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 240 ```python def f240(lines, stop): lines = list(lines) tallies = {} stop = {str(word) for word in stop} items = [] for line in lines[:20]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue tallies[word] = tallies.get(word, 0) + 1 ranking = sorted(tallies.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in tallies.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': tallies, 'top': [word for word, count in ranking[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f240(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 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) if value > limit] ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups} output = dict() output.update(returnvalue) return output ``` Example call: ``` f241([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 242 ```python def f242(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) indexes = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f242([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 243 ```python def f243(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' ').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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f243(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 244 ```python def f244(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f244([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 245 ```python def f245(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text[:64]: if 'a' <= char <= 'z' 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) > 12: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f245(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 246 ```python def f246(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 ranking = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f246(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 247 ```python def f247(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f247([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 248 ```python def f248(rows, keep, prices): rows = list(rows) selected = [] buckets = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * prices[group] selected.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score 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, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f248([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 249 ```python def f249(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if len(edge[0]) < 2): routes.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < limit: for nxt in sorted(routes.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) listing = list(levels) lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': listing, 'depth': levels, 'frontier': lastlevel, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f249([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 250 ```python def f250(values, width, limit): values = list(values) size = width series = [] for pos in range(min(64, len(values) - size + 1)): series.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(series) if value >= limit] increases = sum((1 for pos in range(1, 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 ``` Example call: ``` f250([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 251 ```python def f251(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens[:64]: 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 ``` Example call: ``` f251(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 252 ```python def f252(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f252([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 253 ```python def f253(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) indexes = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f253([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 254 ```python def f254(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if edge[0] <= edge[1]): routes.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f254([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 255 ```python def f255(left, right, excluded): left = list(left) def canon(value): return str(value).strip().upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f255(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 256 ```python def f256(rows, keep, factors): rows = list(rows) selected = [] totals = {} amount = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: charge = value * factors[group] selected.append((code, charge)) totals[group] = totals.get(group, 0) + charge amount += charge selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': [code for code, charge in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f256([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 257 ```python def f257(left, right, banned): left = list(left) def normalize(value): return str(value).strip(' \r').upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b sequence = sorted returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f257(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 258 ```python def f258(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 breaks = [] for pos in range(1, len(result)): left = result[pos][0] breaks.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': breaks, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f258([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 259 ```python def f259(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop or len(word) > 12: continue frequency[word] = min(9, frequency.get(word, 0) + 1) ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f259(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 260 ```python def f260(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) incoming[b] += 1 startdegree = dict(incoming) finished = set() sequence = [] rounds = [] while len(finished) < len(nodes) and len(finished) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished)) if not available: break current = available rounds.append(current) for node in current: finished.add(node) sequence.append(node) for node in current: for nxt in nextmap[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f260(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 261 ```python def f261(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((min(len(value), 8) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f261(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 262 ```python def f262(rows, keep, factors): rows = list(rows) kept = [] groupmap = {} sumvalue = 0 for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))): if state in keep and value != 0: cost = value * factors[group] kept.append((code, cost)) groupmap[group] = groupmap.get(group, 0) + cost sumvalue += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f262([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 263 ```python def f263(rows, keep, factors): rows = list(rows) kept = [] buckets = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: score = value * factors[group] kept.append((code, score)) buckets[group] = buckets.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': grand, 'groups': buckets, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f263([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 264 ```python def f264(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) needs[b] += 1 initial = dict(needs) finished = set() listing = [] layers = [] while len(finished) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes))) if not nextbatch: break current = nextbatch layers.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f264(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 265 ```python def f265(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, min(20, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f265([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 266 ```python def f266(nodes, edges): nodes = list(nodes) incoming = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: if before == after: continue a, b = (before, after) children[a].append(b) incoming[b] += 1 initial = dict(incoming) handled = set() listing = [] rounds = [] while len(handled) < len(nodes): available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not available: break current = available rounds.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': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f266(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 267 ```python def f267(edges, start, limit): edges = list(edges) children = {} for a, b in edges[:16]: children.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f267([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 268 ```python def f268(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(windows) if value > limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f268([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 269 ```python def f269(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop or len(word) > 12: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in frequency.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f269(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 270 ```python def f270(spans, gap): spans = list(spans) result = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not result or left > result[-1][1] + gap: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) 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': min(100, max((right - left for left, right in result)))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f270([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 271 ```python def f271(left, right, banned): left = list(left) def tidy(value): return str(value).strip(' ').upper() banned = {str(value).strip() 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 listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f271(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 272 ```python def f272(left, right, excluded): left = list(left) def canon(value): return str(value).strip(' ').upper() excluded = {canon(value) for value in excluded} a = {canon(value) for value in left} - excluded b = {canon(value) for value in right} - excluded both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f272(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 273 ```python def f273(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} terms = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z' or (char == "'" and word): word += char elif word: terms.append(word) word = '' if word: terms.append(word) for word in terms: if word in stop: continue wordmap[word] = min(9, wordmap.get(word, 0) + 1) sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in wordmap.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f273(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 274 ```python def f274(rows, keep, prices): rows = list(rows) kept = [] groupmap = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * abs(prices[group]) kept.append((code, score)) groupmap[group] = groupmap.get(group, 0) + score grand += score kept.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': groupmap, 'top': [code for code, score in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f274([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 10, 'south': 5, 'core': 11, 'edge': 3}) ``` Output example: ``` {"accepted":10,"groups":{"edge":183,"north":230,"south":355},"top":["CX86","AN12","BR75"],"total":768} ``` ### 275 ```python def f275(lines, stop): lines = list(lines) frequency = {} stop = {str(word) for word in stop} items = [] for line in lines[:32]: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) heads = {} for word, count in frequency.items(): heads[word[0]] = heads.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f275(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 276 ```python def f276(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel[:3], 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f276([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 277 ```python def f277(rows, keep, factors): rows = list(rows) kept = [] totals = {} grand = 0 for code, group, state, value in rows: if str(state).lower() in {str(item).lower() for item in keep}: cost = value * factors[group] kept.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost kept.sort(key=lambda item: (-item[1], item[0])) returnvalue = {'accepted': len(kept), 'total': grand, 'groups': totals, 'top': [code for code, cost in kept[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f277([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 278 ```python def f278(edges, start, limit): edges = list(edges) nextmap = {} for a, b in edges: nextmap.setdefault(a, []).append(b) steps = {start: 0} queue = [start] while queue: cur = queue.pop(0) if steps[cur] < min(limit, 3): for nxt in sorted(nextmap.get(cur, [])): if nxt not in steps: steps[nxt] = steps[cur] + 1 queue.append(nxt) result = list(steps) furthest = sorted((node for node in steps if steps[node] == max(steps.values()))) returnvalue = {'order': result, 'depth': steps, 'frontier': furthest, 'count': len(steps)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f278([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 279 ```python def f279(left, right, banned): left = list(left) def tidy(value): return ' '.join(str(value).split()).upper()[:12] banned = {tidy(value) for value in banned} a = {tidy(value) for value in left} - banned b = {tidy(value) for value in right if tidy(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((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f279(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 280 ```python def f280(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]))) positions = [pos for pos, value in enumerate(windows) if value >= limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': positions, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f280([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 281 ```python def f281(left, right, ignored): left = list(left) def normalize(value): return str(value).strip().upper() ignored = {str(value).upper() for value in ignored} a = {normalize(value) for value in left} - ignored b = {normalize(value) for value in right} - ignored both = a & b result = sorted returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f281(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frost', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","FROST","GARNET"],"left":["DELTA","IVORY"],"right":["HELIUM"],"score":19} ``` ### 282 ```python def f282(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f282([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 283 ```python def f283(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 found = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves[:20]: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in found and list(point) not in repeats: repeats.append(list(point)) found.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (max(-64, min(lowx, x)), min(64, max(highx, x))) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f283([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 284 ```python def f284(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} children = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) children[a].append(b) needs[b] += 1 initial = dict(needs) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes): nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled), reverse=len(nodes) > 20) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in children[node]: needs[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f284(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 285 ```python def f285(edges, start, limit): edges = list(edges) routes = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): routes.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < min(limit, 3): for nxt in (node for node in sorted(routes.get(cur, [])) if len(str(node)) == 1): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) listing = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f285([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### 286 ```python def f286(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used = min(64, used + 1) point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f286([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 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(combined[-1][1], right) gaps = [] for pos in range(1, len(combined)): left = combined[pos][0] gaps.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f287([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 288 ```python def f288(left, right, banned): left = list(left) def normalize(value): return ' '.join(str(value).split()).upper() banned = {normalize(value) for value in banned} a = {normalize(value) for value in left} - banned b = {normalize(value) for value in right} - banned both = a & b listing = sorted returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f288(['ivory', 'frost', 'cyan ', 'GARNET', 'BLUE', 'delta', 'Ember'], ['Ember', 'GARNET', 'cyan ', 'BLUE', 'helium', 'frostx', 'Ember'], ['EMBER']) ``` Output example: ``` {"all":["BLUE","CYAN","DELTA","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["BLUE","CYAN","GARNET"],"left":["DELTA","FROST","IVORY"],"right":["FROSTX","HELIUM"],"score":14} ``` ### 289 ```python def f289(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance and (not nxt.startswith('Z')): distance[nxt] = distance[cur] + 1 queue.append(nxt) result = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': result, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f289([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 290 ```python def f290(nodes, edges): nodes = list(nodes) needs = {node: 0 for node in nodes} nextmap = {node: [] for node in nodes} for before, after in edges: a, b = (before, after) nextmap[a].append(b) needs[b] += 1 startdegree = dict(needs) finished = set() listing = [] rounds = [] 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 rounds.append(current) for node in current: finished.add(node) listing.append(node) for node in current: for nxt in nextmap[node]: needs[nxt] -= 1 returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f290(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 291 ```python def f291(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) levels = {start: 0} queue = [start] while queue: cur = queue.pop(0) if levels[cur] < min(limit, 3): for nxt in (node for node in sorted(children.get(cur, [])) if len(str(node)) == 1): if nxt not in levels: levels[nxt] = levels[cur] + 1 queue.append(nxt) sequence = list(levels) furthest = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': furthest, 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f291([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 292 ```python def f292(spans, gap): spans = list(spans) result = [] for left, right in sorted((pair for pair in spans if pair[0] < pair[1] or pair[0] != pair[1])): if not result or left > result[-1][1] + gap + 1: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, min(50, len(result))): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f292([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 293 ```python def f293(edges, start, limit): edges = list(edges) children = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) furthest = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': furthest, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f293([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 294 ```python def f294(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} work = [start] while work: cur = work.pop(0) if distance[cur] < limit: for nxt in sorted(children.get(cur, [])): if nxt not in distance: distance[nxt] = distance[cur] + 1 work.append(nxt) listing = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': listing, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f294([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 295 ```python def f295(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] + min(gap, 2): 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 ``` Example call: ``` f295([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 296 ```python def f296(edges, start, limit): edges = list(edges) children = {} for a, b in edges: children.setdefault(a, []).append(b) distance = {start: 0} if any((start in edge for edge in edges)) else {} queue = [start] while queue: cur = queue.pop(0) if distance[cur] < min(limit, 3): for nxt in sorted(children.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 queue.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f296([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 297 ```python def f297(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) handled = set() sequence = [] rounds = [] while len(handled) < len(nodes) and len(handled) < 20: nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled)) if not nextbatch: break current = nextbatch rounds.append(current) for node in current: handled.add(node) sequence.append(node) for node in current: for nxt in children[node]: incoming[nxt] -= 1 returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f297(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache')]) ``` Output example: ``` {"batches":[["auth","db"],["cache","jobs","proxy"],["api","worker"],["ui"]],"count":8,"order":["auth","db","cache","jobs","proxy","api","worker","ui"],"roots":["auth","db"]} ``` ### 298 ```python def f298(spans, gap): spans = list(spans) combined = [] for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)): if not combined or left > combined[-1][1] + gap: combined.append([left, right]) else: combined[-1][1] = max(0, combined[-1][1], right) spaces = [] for pos in range(1, len(combined)): left = combined[pos][0] spaces.append(left - combined[pos - 1][1]) returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f298([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 299 ```python def f299(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 known = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in known and list(point) not in returns: returns.append(list(point)) known.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f299([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 300 ```python def f300(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) complete = set() sequence = [] layers = [] while len(complete) < len(nodes) and len(complete) < 20: available = sorted((node for node in nodes if incoming[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes))) 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 ``` Example call: ``` f300(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 301 ```python def f301(lines, stop): lines = list(lines) frequency = {} stop = {word.lower() for word in stop} items = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: items.append(word) word = '' if word: items.append(word) for word in items: if word in stop: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f301(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['ember']) ``` Output example: ``` {"counts":{"amber":5,"blue":3,"cyan":5,"delta":5,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"b":3,"c":5,"d":5,"f":2,"g":3,"h":5},"size":140,"top":["amber","cyan","delta","helium","blue"]} ``` ### 302 ```python def f302(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) < 2: continue frequency[word] = frequency.get(word, 0) + 1 ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0])) letters = {} for word, count in frequency.items(): letters[word[0]] = letters.get(word[0], 0) + count returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f302(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 303 ```python def f303(rows, keep, ratemap): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep: score = value * abs(ratemap[group]) selected.append((code, score)) totals[group] = totals.get(group, 0) + score grand += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f303([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 304 ```python def f304(edges, start, limit): edges = list(edges) nextmap = {} for a, b in (edge for edge in edges if len(edge[1]) < 2): nextmap.setdefault(a, []).append(b) levels = {start: 0} if any((start in edge for edge in edges)) else {} pending = [start] while pending: cur = pending.pop(0) if levels[cur] < limit: for nxt in sorted(nextmap.get(cur, [])): if nxt not in levels: levels[nxt] = levels[cur] + 1 pending.append(nxt) sequence = list(levels) lastlevel = sorted((node for node in levels if levels[node] == max(levels.values()))) returnvalue = {'order': sequence, 'depth': levels, 'frontier': lastlevel[:3], 'count': len(levels)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f304([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 0) ``` Output example: ``` {"count":1,"depth":{"A":0},"frontier":["A"],"order":["A"]} ``` ### 305 ```python def f305(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] = 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 ``` Example call: ``` f305([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61)], 1) ``` Output example: ``` {"cover":36,"holes":[3,5,3,5,7],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[44,52],[59,61]]} ``` ### 306 ```python def f306(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} repeats = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x = max(-64, min(64, x + dx)) y += dy used += 1 point = (x, y) if point in visited and list(point) not in repeats: repeats.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': repeats[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f306([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 307 ```python def f307(rows, keep, prices): rows = list(rows) selected = [] buckets = {} sumvalue = 0 for code, group, state, value in rows: if state in keep: score = value * abs(prices[group]) selected.append((code, score)) buckets[group] = buckets.get(group, 0) + score sumvalue += score selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': buckets, 'top': [code for code, score in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f307([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 308 ```python def f308(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = max(far, abs(x) + abs(y)) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f308([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 309 ```python def f309(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit] growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth} output = dict() output.update(returnvalue) return output ``` Example call: ``` f309([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 310 ```python def f310(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 = [] rounds = [] while len(complete) < len(nodes): available = sorted((node for node in nodes if needs[node] == 0 and node not in complete)) if not available: break current = available rounds.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': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(listing)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f310(['cache', 'proxy', 'jobs', 'db', 'api', 'auth', 'ui', 'worker'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db')]) ``` Output example: ``` {"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]} ``` ### 311 ```python def f311(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} crossings = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count): x += dx y = max(-64, min(64, y + dy)) used += 1 point = (x, y) if point in visited and list(point) not in crossings: crossings.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': crossings[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f311([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-8,3,7],"end":[0,-8],"far":10,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":38} ``` ### 312 ```python def f312(rows, keep, prices): rows = list(rows) selected = [] totals = {} grand = 0 for code, group, state, value in rows: if state in keep and value != 0: cost = value * prices[group] selected.append((code, cost)) totals[group] = totals.get(group, 0) + cost grand += cost selected.sort(key=lambda item: (-item[1], str(item[0]).casefold())) returnvalue = {'accepted': len(selected), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f312([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 313 ```python def f313(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum((max(item, 0) for item in values[pos:pos + size]))) matches = [pos for pos, value in enumerate(windows) if value >= limit] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f313([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 11, 78) ``` Output example: ``` {"best":186,"hits":[0,1,2,3,4],"rises":0,"sums":[186,179,177,158,158]} ``` ### 314 ```python def f314(values, width, limit): values = list(values) size = width windows = [] for pos in range(len(values) - size + 1): windows.append(sum(values[pos:pos + size])) positions = [pos for pos, value in enumerate(windows) if value >= limit and (limit >= 0 or pos > 0)] increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1])) returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases} output = dict() output.update(returnvalue) return output ``` Example call: ``` f314([24, 17, 23, 12, 21, 22, 4, 23, 8, 20, 12, 17, 15, 4, 12], 3, 78) ``` Output example: ``` {"best":64,"hits":[],"rises":4,"sums":[64,52,56,55,47,49,35,51,40,49,44,36,31]} ``` ### 315 ```python def f315(rows, keep, prices): rows = list(rows) selected = [] buckets = {} amount = 0 for code, group, state, value in rows: if state in keep: score = value * 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(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]} output = dict() output.update(returnvalue) return output ``` Example call: ``` f315([('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14), ('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 11, 'edge': 5}) ``` Output example: ``` {"accepted":10,"groups":{"edge":305,"north":161,"south":355},"top":["CX86","DL45","AN12"],"total":821} ``` ### 316 ```python def f316(lines, stop): lines = list(lines) wordmap = {} stop = {word.lower() for word in stop} tokens = [] for line in lines: text = line.lower() word = '' for char in text: if 'a' <= char <= 'z': word += char elif word: tokens.append(word) word = '' if word: tokens.append(word) for word in tokens: if word in stop: continue wordmap[word] = wordmap.get(word, 0) + 1 sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0])) starts = {} for word, count in wordmap.items(): starts[word[0]] = starts.get(word[0], 0) + count returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f316(['Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium', 'Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost'], ['blue']) ``` Output example: ``` {"counts":{"amber":5,"cyan":5,"delta":5,"ember":3,"frost":2,"garnet":3,"helium":5},"initials":{"a":5,"c":5,"d":5,"e":3,"f":2,"g":3,"h":5},"size":143,"top":["amber","cyan","delta","helium","ember"]} ``` ### 317 ```python def f317(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(min(count, 3 + (count - count))): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f317([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 318 ```python def f318(spans, gap): spans = list(spans) result = [] for left, right in sorted(spans, key=lambda pair: pair[0]): if not result or left > result[-1][1] + max(0, gap) + 1: result.append([left, right]) else: result[-1][1] = max(result[-1][1], right) gaps = [] for pos in range(1, len(result)): left = result[pos][0] gaps.append(left - result[pos - 1][1]) returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))} output = dict() output.update(returnvalue) return output ``` Example call: ``` f318([(2, 5), (5, 11), (14, 20), (18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61)], 1) ``` Output example: ``` {"cover":35,"holes":[3,5,3,4,4,5],"longest":9,"segments":[[2,11],[14,23],[28,30],[33,39],[43,46],[50,54],[59,61]]} ``` ### 319 ```python def f319(moves): moves = list(moves) step = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} x = 0 y = 0 visited = {(0, 0)} returns = [] far = 0 lowx = highx = lowy = highy = 0 used = 0 for direction, count in moves: dx, dy = step[direction] for turn in range(count % 100): x += dx y += dy used += 1 point = (x, y) if point in visited and list(point) not in returns: returns.append(list(point)) visited.add(point) far = min(64, max(far, abs(x) + abs(y))) lowx, highx = (min(lowx, x), max(highx, x)) lowy, highy = (min(lowy, y), max(highy, y)) returnvalue = {'end': [x, y], 'far': far, 'revisits': returns[:5], 'bounds': [lowx, lowy, highx, highy], 'steps': used} output = dict() output.update(returnvalue) return output ``` Example call: ``` f319([('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3), ('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2)]) ``` Output example: ``` {"bounds":[-3,-5,3,8],"end":[0,-5],"far":11,"revisits":[[2,3],[1,3],[0,3],[0,2],[0,1]],"steps":37} ``` ### 320 ```python def f320(edges, start, limit): edges = list(edges) routes = {} for a, b in edges[:16]: routes.setdefault(a, []).append(b) distance = {start: 0} pending = [start] while pending: cur = pending.pop(0) if distance[cur] < limit: for nxt in sorted(routes.get(cur, []))[:20]: if nxt not in distance: distance[nxt] = distance[cur] + 1 pending.append(nxt) sequence = list(distance) lastlevel = sorted((node for node in distance if distance[node] == max(distance.values()))) returnvalue = {'order': sequence, 'depth': distance, 'frontier': lastlevel, 'count': len(distance)} output = dict() output.update(returnvalue) return output ``` Example call: ``` f320([('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E'), ('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B')], 'A', 3) ``` Output example: ``` {"count":8,"depth":{"A":0,"B":1,"C":1,"D":2,"E":2,"F":2,"G":3,"H":3},"frontier":["G","H"],"order":["A","B","C","D","E","F","G","H"]} ``` ### Final Query Give me the output of the final query without executing it! ```python import json, re from collections import deque out = {} left = list(('ivory', 'frost', 'cyan ') + ('GARNET', 'BLUE', 'delta', 'Ember')) right = list(('Ember', 'GARNET', 'cyan ') + ('BLUE', 'helium', 'frost', 'Ember')) blocked = ['EMBER'] 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))} lines = list(('Amber, cyan. garnet cyan', 'Delta, helium. delta frost helium', 'Amber, helium. cyan helium') + ('Amber, cyan. delta ember garnet', 'Ember, garnet. blue blue', 'Ember, helium. amber delta amber', 'Delta, blue. cyan frost')) route = record['ee'] cases = { 19: ['ember'], 14: ['blue'], } 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))} values = list((24, 17, 23, 12, 21, 22, 4) + (23, 8, 20, 12, 17, 15, 4, 12)) limit = 78 route = record['dd'] cases = { 143: 3, 140: 11, } width = cases[route] current = sum(values[:width]) sums = [current] for pos in range(width, len(values)): current += values[pos] - values[pos - width] sums.append(current) hits = [] rises = 0 for pos, value in enumerate(sums): if value >= limit: hits.append(pos) if pos and value > sums[pos - 1]: rises += 1 record = {'aa': hits, 'bb': max(sums), 'cc': rises, 'dd': sums} route = record['bb'] cases = { 64: list((('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3)) + (('N', 1), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 5), ('S', 2))), 186: list((('N', 3), ('E', 3), ('W', 3), ('S', 2), ('N', 3), ('W', 3)) + (('N', 2), ('N', 2), ('S', 2), ('E', 3), ('S', 1), ('S', 3), ('S', 2), ('S', 3), ('S', 2))), } moves = cases[route] delta = {'N': (0, 1), 'E': (1, 0), 'S': (0, -1), 'W': (-1, 0)} points = [[0, 0]] for direction, count in moves: dx, dy = delta[direction] for turn in range(count): points.append([points[-1][0] + dx, points[-1][1] + dy]) seen = set() revisits = [] for point in points: key = tuple(point) if key in seen and point not in revisits: revisits.append(point) seen.add(key) record = {'aa': points[-1], 'bb': max((abs(point[0]) + abs(point[1]) for point in points)), 'cc': revisits[:5], 'dd': [min((point[0] for point in points)), min((point[1] for point in points)), max((point[0] for point in points)), max((point[1] for point in points))], 'ee': len(points) - 1} nodes = list(('cache', 'proxy', 'jobs', 'db') + ('api', 'auth', 'ui', 'worker')) route = record['bb'] cases = { 11: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui')) + (('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'cache'))), 10: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui')) + (('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('proxy', 'ui'), ('auth', 'db'))), } edges = cases[route] remaining = set(nodes) done = set() order = [] batches = [] while remaining: ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node)))) if not ready: break batches.append(ready) order.extend(ready) done.update(ready) remaining.difference_update(ready) roots = sorted((node for node in nodes if all((after != node for before, after in edges)))) record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)} rows = list((('CX64', 'edge', 'checked', 19), ('BR01', 'edge', 'ready', 11), ('DL38', 'core', 'void', 16), ('BR75', 'south', 'ready', 18), ('AN12', 'south', 'checked', 21), ('DL49', 'south', 'ready', 16), ('CX86', 'north', 'ready', 23), ('DL23', 'core', 'void', 14)) + (('BR60', 'core', 'void', 23), ('AN97', 'edge', 'checked', 4), ('AN34', 'south', 'checked', 16), ('DL71', 'north', 'void', 22), ('DL08', 'edge', 'checked', 3), ('DL45', 'edge', 'ready', 24))) keep = list(('ready',) + ('checked',)) route = record['aa'][1] cases = { 'cache': dict((('north', 10), ('south', 5), ('core', 11)) + (('edge', 3),)), 'db': dict((('north', 7), ('south', 5), ('core', 11)) + (('edge', 5),)), } rates = cases[route] chosen = [(code, group, value) for code, group, state, value in rows if state in keep] priced = [(code, group, value * rates[group]) for code, group, value in chosen] groups = {} for code, group, price in priced: groups[group] = groups.get(group, 0) + price ranked = sorted(priced, key=lambda row: (-row[2], row[0])) record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups} edges = list((('A', 'C'), ('A', 'B'), ('B', 'D'), ('B', 'E')) + (('C', 'F'), ('D', 'G'), ('E', 'G'), ('F', 'H'), ('G', 'A'), ('C', 'E'), ('H', 'B'))) start = 'A' route = record['bb'] cases = { 821: 3, 768: 0, } limit = cases[route] links = {} for edge in edges: links.setdefault(edge[0], []).append(edge[1]) distance = {start: 0} queue = deque([start]) while queue: node = queue.popleft() if distance[node] == limit: continue for child in sorted(links.get(node, [])): if child in distance: continue distance[child] = distance[node] + 1 queue.append(child) furthest = max(distance.values()) record = {'aa': list(distance), 'bb': distance, 'cc': sorted((node for node in distance if distance[node] == furthest)), 'dd': len(distance)} gap = 1 route = record['dd'] cases = { 1: list(((2, 5), (5, 11), (14, 20)) + ((18, 23), (28, 30), (33, 39), (43, 46), (50, 54), (59, 61))), 8: list(((2, 5), (5, 11), (14, 20)) + ((18, 23), (28, 30), (33, 39), (44, 47), (48, 52), (59, 61))), } 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))} out['aa'] = record['aa'] out['cc'] = record['cc'] out['bb'] = record['bb'] out['dd'] = record['dd'] print(json.dumps(out, separators=(",", ":"), sort_keys=True)) ```