Download tasks/program-execution-tracing/contexts/sample-00005.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 389 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00005.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/program-execution-tracing/contexts/sample-00005.md
-
curl -L -o sample-00005.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/program-execution-tracing/contexts/sample-00005.md
001
def f1(lines, stop):
lines = list(lines)
tallies = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text:
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]))
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:
f1(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
002
def f2(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[:64]:
if word in stop:
continue
wordmap[word] = wordmap.get(word, 0) + 1
ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in wordmap.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': wordmap, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in wordmap.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f2(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
003
def f3(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
incoming = {node: 0 for node in nodes}
nextmap = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
nextmap[a].append(b)
incoming[b] += 1
initial = dict(incoming)
finished = set()
sequence = []
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
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:
f3(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
004
def f4(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f4(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
005
def f5(rows, keep, prices):
rows = list(rows)
selected = []
groupmap = {}
amount = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
cost = value * abs(prices[group])
selected.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
amount += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f5([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
006
def f6(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
positions = [pos for pos, value in enumerate(windows) if value > limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f6([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
007
def f7(left, right, banned):
left = list(left)
def canon(value):
return str(value).strip(' \r').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:
f7(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
008
def f8(lines, stop):
lines = list(lines)
frequency = {}
stop = {str(word) for word in stop}
items = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = min(9, 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:
f8(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
009
def f9(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(windows[:64]) if value > limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f9([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
010
def f10(lines, stop):
lines = list(lines)
tallies = {}
stop = {word.lower() for word in stop}
terms = []
for line in lines:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z' or (char == "'" and word):
word += char
elif word:
terms.append(word)
word = ''
if word:
terms.append(word)
for word in terms:
if word in stop or len(word) > 12:
continue
tallies[word] = tallies.get(word, 0) + 1
ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in tallies.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f10(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
011
def f11(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
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:
f11(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
012
def f12(rows, keep, prices):
rows = list(rows)
matches = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep and value >= 0:
charge = value * abs(prices[group])
matches.append((code, charge))
totals[group] = totals.get(group, 0) + charge
amount += charge
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f12([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
013
def f13(board):
board = list(board)
known = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
work = [(row, col)]
known.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f13(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
014
def f14(rows, keep, prices):
rows = list(rows)
kept = []
groupmap = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep and value != 0:
charge = value * prices[group]
kept.append((code, charge))
groupmap[group] = groupmap.get(group, 0) + charge
sumvalue += charge
kept.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(kept)), 'total': sumvalue, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, charge in kept)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f14([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
015
def f15(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip().upper()
excluded = {str(value).strip() for value in excluded}
a = {tidy(value) for value in left 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((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f15(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
016
def f16(board):
board = list(board)
known = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f16(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
017
def f17(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)
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:
f17([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
018
def f18(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + gap + 1:
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
gaps = []
for pos in range(1, len(combined)):
left = combined[pos][0]
gaps.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f18([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
019
def f19(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted({key for key, count in left if key not in prices})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f19([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
020
def f20(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(spans):
if not result or left > result[-1][1] + gap % 5:
result.append([left, right])
else:
result[-1][1] = max(result[-1][1], right)
spaces = []
for pos in range(1, min(50, len(result))):
left = result[pos][0]
spaces.append(left - result[pos - 1][1])
returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in result)))}
output = dict()
output.update(returnvalue)
return output
Example call:
f20([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
021
def f21(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
items = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
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:
f21(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
022
def f22(nodes, edges):
nodes = list(nodes)
needs = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
needs[b] += 1
startdegree = dict(needs)
finished = set()
result = []
groups = []
while len(finished) < len(nodes) and len(finished) < 20:
nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished))
if not nextbatch:
break
current = nextbatch
groups.append(current)
for node in current:
finished.add(node)
result.append(node)
for node in current:
for nxt in routes[node]:
needs[nxt] -= 1
returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f22(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
023
def f23(lines, stop):
lines = list(lines)
tallies = {}
stop = {str(word) for word in stop}
tokens = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens[:64]:
if word in stop:
continue
tallies[word] = tallies.get(word, 0) + 1
ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in tallies.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f23(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
024
def f24(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens[:64]:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
starts = {}
for word, count in frequency.items():
starts[word[0]] = starts.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': starts, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f24(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
025
def f25(board):
board = list(board)
found = set()
regions = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
work = [(row, col)]
found.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
work.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f25(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
026
def f26(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens[:64]:
if word in stop or len(word) > 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:
f26(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
027
def f27(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()
result = []
groups = []
while len(handled) < len(nodes) and len(handled) < 20:
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20)
if not nextbatch:
break
current = nextbatch
groups.append(current)
for node in current:
handled.add(node)
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:
f27(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
028
def f28(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
indegree = {node: 0 for node in nodes}
nextmap = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
nextmap[a].append(b)
indegree[b] += 1
initial = dict(indegree)
handled = set()
sequence = []
rounds = []
while len(handled) < len(nodes):
available = sorted((node for node in nodes if indegree[node] == 0 and node not in handled))
if not available:
break
current = available
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:
f28(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
029
def f29(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(len(values) - size + 1):
totals.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(totals[:64]) if value > limit]
increases = sum((1 for pos in range(1, len(totals)) if totals[pos] >= totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f29([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
030
def f30(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 if normalize(value) != ''} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f30(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
031
def f31(left, right, ignored):
left = list(left)
def normalize(value):
return str(value).strip(' \r').upper()
ignored = {str(value).strip() for value in ignored}
a = {normalize(value) for value in left} - ignored
b = {normalize(value) for value in right} - ignored
both = a & b
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:
f31(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
032
def f32(left, right, excluded):
left = list(left)
def normalize(value):
return str(value).strip(' \r').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((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f32(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
033
def f33(rows, keep, ratemap):
rows = list(rows)
selected = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep:
cost = value * ratemap[group]
selected.append((code, cost))
totals[group] = totals.get(group, 0) + cost
amount += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f33([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
034
def f34(left, right, excluded):
left = list(left)
def normalize(value):
return ' '.join(str(value).split()).upper()
excluded = {normalize(value) for value in excluded}
a = {normalize(value) for value in left} - excluded
b = {normalize(value) for value in right} - excluded
both = a & b
result = sorted
returnvalue = {'both': result(both), 'left': result(a - b), 'right': result(b - a), 'all': result(a | b), 'score': sum((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f34(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
035
def f35(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
incoming = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
routes[a].append(b)
incoming[b] += 1
initial = dict(incoming)
finished = set()
sequence = []
rounds = []
while len(finished) < len(nodes):
available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
rounds.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in routes[node]:
incoming[nxt] -= 1
returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f35(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
036
def f36(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left:
if key in prices and count > 0:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in prices})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f36([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
037
def f37(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[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
terms.append(word)
word = ''
if word:
terms.append(word)
for word in terms:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
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:
f37(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
038
def f38(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
gaps = []
for pos in range(1, len(segments)):
left = segments[pos][0]
gaps.append(left - segments[pos - 1][1])
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': min(100, max((right - left for left, right in segments)))}
output = dict()
output.update(returnvalue)
return output
Example call:
f38([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
039
def f39(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)
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:
f39([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
040
def f40(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + gap % 5:
combined.append([left, right])
else:
combined[-1][1] = right
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:
f40([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
041
def f41(lines, stop):
lines = list(lines)
wordmap = {}
stop = {str(word) for word in stop}
terms = []
for line in lines:
text = line.lower()
word = ''
for char in text:
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
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:
f41(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
042
def f42(spans, gap):
spans = list(spans)
segments = []
for left, right in sorted(spans):
if not segments or left > segments[-1][1] + gap:
segments.append([left, right])
else:
segments[-1][1] = 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:
f42([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
043
def f43(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': min(256, max(windows)), 'hits': positions, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f43([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
044
def f44(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + gap:
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
breaks = []
for pos in range(1, len(combined)):
left = combined[pos][0]
breaks.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f44([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
045
def f45(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors and rate0 != 0:
factors[name0] = rate0
matched = []
for key, count in left:
if key in factors:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f45([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
046
def f46(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
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(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
047
def f47(left, right, banned):
left = list(left)
def normalize(value):
return str(value).strip().upper()[:12]
banned = {str(value).strip() for value in banned}
a = {normalize(value) for value in left} - banned
b = {normalize(value) for value in right} - banned
both = a & b
listing = sorted
returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f47(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
048
def f48(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip().upper()
excluded = {tidy(value) for value in excluded}
a = {tidy(value) for value in left} - excluded
b = {tidy(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f48(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
049
def f49(left, right, excluded):
left = list(left)
def normalize(value):
return ' '.join(str(value).split()).upper()[:12]
excluded = {normalize(value) for value in excluded}
a = {normalize(value) for value in left if normalize(value) != normalize(value) or normalize(value) != ''} - excluded
b = {normalize(value) for value in right} - excluded
both = a & b
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:
f49(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
050
def f50(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
needs = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
needs[b] += 1
initial = dict(needs)
finished = set()
sequence = []
groups = []
while len(finished) < len(nodes) and len(finished) < 20:
nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), reverse=len(nodes) > 20)
if not nextbatch:
break
current = nextbatch
groups.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in routes[node]:
needs[nxt] -= 1
returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f50(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
051
def f51(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in prices})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f51([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
052
def f52(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(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, 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:
f52([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
053
def f53(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(spans):
if not combined or left > combined[-1][1] + gap % 5 + 1:
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
spaces = []
for pos in range(1, len(combined)):
left = combined[pos][0]
spaces.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f53([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
054
def f54(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 * 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:
f54([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
055
def f55(board):
board = list(board)
known = set()
components = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
pending = [(row, col)]
known.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f55(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
056
def f56(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
items = []
for key, count in left[:20]:
if key in factors and count > 0:
items.append((key, count * factors[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len(items)}
output = dict()
output.update(returnvalue)
return output
Example call:
f56([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
057
def f57(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
finished = set()
sequence = []
layers = []
while len(finished) < len(nodes) and len(finished) < 20:
available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
layers.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
incoming[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f57(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
058
def f58(left, right, excluded):
left = list(left)
def canon(value):
return str(value).strip().upper()[:12]
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:
f58(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
059
def f59(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((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': min(256, max(series)), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f59([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
060
def f60(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + max(0, gap):
combined.append([left, right])
else:
combined[-1][1] = 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:
f60([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
061
def f61(left, right, excluded):
left = list(left)
def canon(value):
return str(value).strip(' ').upper()
excluded = {str(value).strip() for value in excluded}
a = {canon(value) for value in left} - excluded
b = {canon(value) for value in right} - excluded
both = a & b
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:
f61(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
062
def f62(rows, keep, factors):
rows = list(rows)
selected = []
buckets = {}
grand = 0
for code, group, state, value in rows:
if state in keep and value >= 0:
cost = value * factors[group]
selected.append((code, cost))
buckets[group] = buckets.get(group, 0) + cost
grand += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': grand, 'groups': buckets, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f62([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
063
def f63(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(min(64, len(values) - size + 1)):
totals.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(totals) if value >= limit]
increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f63([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
064
def f64(board):
board = list(board)
found = set()
components = []
for row in range(len(board)):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in found:
continue
cells = []
work = [(row, col)]
found.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
if len(found) >= 100:
continue
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f64(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
065
def f65(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
finished = set()
sequence = []
rounds = []
while len(finished) < len(nodes):
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=len(nodes) > 20)
if not nextbatch:
break
current = nextbatch
rounds.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
incoming[nxt] -= 1
returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f65(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
066
def f66(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
handled = set()
result = []
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[:1] if any((str(node).isupper() for node in nodes)) else available
rounds.append(current)
for node in current:
handled.add(node)
result.append(node)
for node in current:
for nxt in routes[node]:
incoming[nxt] -= 1
returnvalue = {'order': result, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f66(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
067
def f67(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)
spaces = []
for pos in range(1, len(segments)):
left = segments[pos][0]
spaces.append(min(100, left - segments[pos - 1][1]))
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': min(100, max((right - left for left, right in segments)))}
output = dict()
output.update(returnvalue)
return output
Example call:
f67([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
068
def f68(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
combined = []
for key, count in left:
if key in factors:
combined.append((key, count * factors[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f68([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
069
def f69(nodes, edges):
nodes = list(nodes)
needs = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
needs[b] += 1
startdegree = dict(needs)
finished = set()
listing = []
groups = []
while len(finished) < len(nodes) and len(finished) < 20:
available = sorted((node for node in nodes if needs[node] == 0 and node not in finished))
if not available:
break
current = available
groups.append(current)
for node in current:
finished.add(node)
listing.append(node)
for node in current:
for nxt in children[node]:
needs[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:
f69(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
070
def f70(board):
board = list(board)
visited = set()
components = []
for row in range(len(board)):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f70(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
071
def f71(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
pending = [(row, col)]
visited.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f71(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
072
def f72(left, right, excluded):
left = list(left)
def normalize(value):
return str(value).strip(' ').upper()[:12]
excluded = {normalize(value) for value in excluded}
a = {normalize(value) for value in left} - excluded
b = {normalize(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f72(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
073
def f73(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
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:
f73(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
074
def f74(rows, keep, prices):
rows = list(rows)
selected = []
buckets = {}
amount = 0
for code, group, state, value in rows:
if state in keep and value >= 0:
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:
f74([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
075
def f75(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not result or left > result[-1][1] + gap:
result.append([left, right])
else:
result[-1][1] = 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:
f75([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
076
def f76(lines, stop):
lines = list(lines)
wordmap = {}
stop = {word.lower() for word in stop}
items = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
wordmap[word] = wordmap.get(word, 0) + 1
ordered = sorted(wordmap.items(), key=lambda item: (-item[1], item[0]))
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:
f76(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
077
def f77(lines, stop):
lines = list(lines)
wordmap = {}
stop = {str(word) for word in stop}
terms = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
terms.append(word)
word = ''
if word:
terms.append(word)
for word in terms:
if word in stop:
continue
wordmap[word] = 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:
f77(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
078
def f78(board):
board = list(board)
found = set()
regions = []
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
pending = [(row, col)]
found.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
if len(found) >= 100:
continue
pending.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f78(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
079
def f79(rows, keep, factors):
rows = list(rows)
selected = []
totals = {}
grand = 0
for code, group, state, value in rows:
if state in keep:
cost = value * factors[group]
selected.append((code, cost))
totals[group] = totals.get(group, 0) + cost
grand += cost
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': grand, 'groups': totals, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f79([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
080
def f80(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
matched = []
for key, count in left[:20]:
if key in ratemap and count > 0:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in ratemap})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f80([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
081
def f81(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)
handled = set()
sequence = []
groups = []
while len(handled) < len(nodes):
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes)))
if not nextbatch:
break
current = nextbatch
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 rootsource[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f81(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
082
def f82(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)
handled = set()
sequence = []
groups = []
while len(handled) < len(nodes) and len(handled) < 20:
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled))
if not nextbatch:
break
current = nextbatch
groups.append(current)
for node in current:
handled.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
incoming[nxt] -= 1
returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f82(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
083
def f83(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[: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
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:
f83(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
084
def f84(values, width, limit):
values = list(values)
size = width if width > 1 or width < 1 else 2
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
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:
f84([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
085
def f85(left, right, banned):
left = list(left)
def normalize(value):
return str(value).strip(' ').upper()
banned = {str(value).upper() for value in banned}
a = {normalize(value) for value in left} - banned
b = {normalize(value) for value in right} - banned
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f85(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
086
def f86(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]))
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:
f86(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
087
def f87(rows, keep, ratemap):
rows = list(rows)
selected = []
buckets = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep:
charge = value * ratemap[group]
selected.append((code, charge))
buckets[group] = buckets.get(group, 0) + charge
sumvalue += charge
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': buckets, 'top': list(dict.fromkeys((code for code, charge in selected)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f87([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
088
def f88(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices and rate0 != 0:
prices[name0] = rate0
combined = []
for key, count in left:
if key in prices:
combined.append((key, count * prices[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f88([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
089
def f89(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
matched = []
for key, count in left:
if key in ratemap and count > 0:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f89([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
090
def f90(rows, keep, ratemap):
rows = list(rows)
kept = []
buckets = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep:
score = value * ratemap[group]
kept.append((code, score))
buckets[group] = buckets.get(group, 0) + score
sumvalue += score
kept.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': buckets, 'top': [code for code, score in kept[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f90([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
091
def f91(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
sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in tallies.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f91(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
092
def f92(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip(' \n').upper()
excluded = {tidy(value) for value in excluded}
a = {tidy(value) for value in left 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:
f92(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
093
def f93(spans, gap):
spans = list(spans)
segments = []
for left, right in sorted(spans):
if not segments or left > segments[-1][1] + gap % 5:
segments.append([left, right])
else:
segments[-1][1] = max(segments[-1][1], right)
gaps = []
for pos in range(1, len(segments)):
left = segments[pos][0]
gaps.append(left - segments[pos - 1][1])
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))}
output = dict()
output.update(returnvalue)
return output
Example call:
f93([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
094
def f94(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip(' \n').upper()
excluded = {str(value).upper() for value in excluded}
a = {tidy(value) for value in left} - excluded
b = {tidy(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f94(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
095
def f95(lines, stop):
lines = list(lines)
tallies = {}
stop = {word.lower() for word in stop}
items = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop or len(word) > 12:
continue
tallies[word] = min(9, tallies.get(word, 0) + 1)
sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in tallies.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f95(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
096
def f96(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in prices})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f96([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
097
def f97(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip().upper()
excluded = {tidy(value) for value in excluded}
a = {tidy(value) for value in left} - excluded
b = {tidy(value) for value in right} - excluded
both = a & b
listing = sorted
returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f97(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
098
def f98(rows, keep, ratemap):
rows = list(rows)
matches = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep and value != 0:
charge = value * ratemap[group]
matches.append((code, charge))
totals[group] = totals.get(group, 0) + charge
amount += charge
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': [code for code, charge in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f98([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
099
def f99(board):
board = list(board)
known = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
pending = [(row, col)]
known.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f99(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
100
def f100(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip(' ').upper()
excluded = {tidy(value) for value in excluded}
a = {tidy(value) for value in left} - excluded
b = {tidy(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f100(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
101
def f101(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(windows) if value >= limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f101([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
102
def f102(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(min(64, len(values) - size + 1)):
series.append(sum(values[pos:pos + size]))
positions = [pos for pos, value in enumerate(series) if value >= limit]
increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1]))
returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f102([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
103
def f103(board):
board = list(board)
visited = set()
pieces = []
for row in range(len(board)):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
pending = [(row, col)]
visited.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
pending.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f103(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
104
def f104(spans, gap):
spans = list(spans)
result = []
for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])):
if not result or left > result[-1][1] + gap + 1:
result.append([left, right])
else:
result[-1][1] = max(result[-1][1], right)
spaces = []
for pos in range(1, min(50, len(result))):
left = result[pos][0]
spaces.append(left - result[pos - 1][1])
returnvalue = {'segments': result, 'cover': sum((right - left for left, right in result)), 'holes': spaces, 'longest': max((right - left for left, right in result))}
output = dict()
output.update(returnvalue)
return output
Example call:
f104([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
105
def f105(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(len(values) - size + 1):
totals.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(totals[:64]) if value >= limit]
increases = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f105([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
106
def f106(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(len(values) - size + 1):
series.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(series) if value >= limit]
increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1]))
returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f106([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
107
def f107(rows, keep, factors):
rows = list(rows)
selected = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep:
charge = value * abs(factors[group])
selected.append((code, charge))
totals[group] = totals.get(group, 0) + charge
sumvalue += charge
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f107([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
108
def f108(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
incoming = {node: 0 for node in nodes}
nextmap = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
nextmap[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
finished = set()
listing = []
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=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]:
incoming[nxt] -= 1
returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)}
output = dict()
output.update(returnvalue)
return output
Example call:
f108(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
109
def f109(values, width, limit):
values = list(values)
size = width if width > 1 or width < 1 else 2
series = []
for pos in range(len(values) - size + 1):
series.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(series) if value >= limit]
increases = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1]))
returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f109([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
110
def f110(rows, keep, prices):
rows = list(rows)
selected = []
totals = {}
amount = 0
for code, group, state, value in (row for pos, row in enumerate(rows) if pos == rows.index(row) or pos < len(rows) - (len(rows) - rows.index(row))):
if state in keep:
cost = value * prices[group]
selected.append((code, cost))
totals[group] = totals.get(group, 0) + cost
amount += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f110([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
111
def f111(board):
board = list(board)
found = set()
components = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
pending = [(row, col)]
found.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
if len(found) >= 100:
continue
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f111(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
112
def f112(rows, keep, prices):
rows = list(rows)
matches = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
charge = value * prices[group]
matches.append((code, charge))
totals[group] = totals.get(group, 0) + charge
sumvalue += charge
matches.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, charge in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f112([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
113
def f113(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
indegree = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
indegree[b] += 1
rootsource = dict(indegree)
complete = set()
sequence = []
layers = []
while len(complete) < len(nodes):
available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
layers.append(current)
for node in current:
complete.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f113(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
114
def f114(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])):
if not combined or left > combined[-1][1] + gap:
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
breaks = []
for pos in range(1, len(combined)):
left = combined[pos][0]
breaks.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((min(100, right - left) for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f114([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
115
def f115(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(spans):
if not combined or left > combined[-1][1] + max(0, gap):
combined.append([left, right])
else:
combined[-1][1] = right
gaps = []
for pos in range(1, len(combined)):
left = combined[pos][0]
gaps.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f115([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
116
def f116(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + gap + 1:
combined.append([left, right])
else:
combined[-1][1] = max(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:
f116([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
117
def f117(rows, keep, prices):
rows = list(rows)
kept = []
groupmap = {}
grand = 0
for code, group, state, value in rows:
if state in keep:
cost = value * prices[group]
kept.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
grand += cost
kept.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(kept), 'total': grand, 'groups': groupmap, 'top': [code for code, cost in kept[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f117([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
118
def f118(rows, keep, prices):
rows = list(rows)
selected = []
groupmap = {}
amount = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
cost = value * prices[group]
selected.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
amount += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f118([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
119
def f119(board):
board = list(board)
visited = set()
regions = []
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
pending = [(row, col)]
visited.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
pending.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f119(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
120
def f120(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
positions = [pos for pos, value in enumerate(windows[:64]) if value >= limit]
ups = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f120([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
121
def f121(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in prices})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f121([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
122
def f122(rows, keep, prices):
rows = list(rows)
matches = []
totals = {}
grand = 0
for code, group, state, value in rows:
if state in keep:
cost = value * prices[group]
matches.append((code, cost))
totals[group] = totals.get(group, 0) + cost
grand += cost
matches.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(matches), 'total': grand, 'groups': totals, 'top': [code for code, cost in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f122([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
123
def f123(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(len(values) - size + 1):
series.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(series) if value > limit]
ups = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1]))
returnvalue = {'sums': series, 'best': max([0] + series), 'hits': indexes, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f123([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
124
def f124(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((min(100, right - left) for left, right in result)), 'holes': gaps, 'longest': max((right - left for left, right in result))}
output = dict()
output.update(returnvalue)
return output
Example call:
f124([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
125
def f125(nodes, edges):
nodes = list(nodes)
indegree = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
indegree[b] += 1
initial = dict(indegree)
finished = set()
sequence = []
groups = []
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
groups.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in routes[node]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f125(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
126
def f126(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
combined = []
for key, count in left:
if key in factors:
combined.append((key, count * factors[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f126([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
127
def f127(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
needs = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
needs[b] += 1
initial = dict(needs)
complete = set()
sequence = []
layers = []
while len(complete) < len(nodes):
nextbatch = sorted((node for node in nodes if needs[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]:
needs[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f127(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
128
def f128(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right[:20]:
if name0 not in ratemap or rate0 <= rate0 + 0:
ratemap[name0] = rate0
matched = []
for key, count in left[:20]:
if key in ratemap:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f128([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
129
def f129(left, right, ignored):
left = list(left)
def canon(value):
return str(value).strip().upper()[:12]
ignored = {str(value).upper() for value in ignored}
a = {canon(value) for value in left} - ignored
b = {canon(value) for value in right} - ignored
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f129(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
130
def f130(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f130([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
131
def f131(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
indexes = [pos for pos, value in enumerate(windows) if value > limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f131([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
132
def f132(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
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:
f132(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
133
def f133(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)
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:
f133([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
134
def f134(board):
board = list(board)
known = set()
regions = []
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
queue.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f134(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
135
def f135(lines, stop):
lines = list(lines)
tallies = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z' or (char == "'" and word):
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
tallies[word] = min(9, tallies.get(word, 0) + 1)
ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in tallies.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f135(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
136
def f136(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
initial = dict(incoming)
finished = set()
result = []
layers = []
while len(finished) < len(nodes):
available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished), reverse=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
layers.append(current)
for node in current:
finished.add(node)
result.append(node)
for node in current:
for nxt in children[node]:
incoming[nxt] -= 1
returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f136(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
137
def f137(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(len(values) - size + 1):
totals.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(totals[:64]) if value > limit]
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': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f137([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
138
def f138(rows, keep, ratemap):
rows = list(rows)
kept = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep and value != 0:
cost = value * ratemap[group]
kept.append((code, cost))
totals[group] = totals.get(group, 0) + cost
amount += cost
kept.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
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:
f138([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
139
def f139(board):
board = list(board)
known = set()
components = []
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f139(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
140
def f140(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(len(values) - size + 1):
series.append(sum(values[pos:pos + size]))
positions = [pos for pos, value in enumerate(series) if value >= limit]
increases = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1]))
returnvalue = {'sums': series, 'best': max([0] + series), 'hits': positions, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f140([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
141
def f141(board):
board = list(board)
known = set()
components = []
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f141(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
142
def f142(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(spans):
if not result or left > result[-1][1] + gap + 1:
result.append([left, right])
else:
result[-1][1] = 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:
f142([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
143
def f143(lines, stop):
lines = list(lines)
frequency = {}
stop = {str(word) for word in stop}
items = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in frequency.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f143(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
144
def f144(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
indegree = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
indegree[b] += 1
startdegree = dict(indegree)
complete = set()
sequence = []
rounds = []
while len(complete) < len(nodes):
available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=len(nodes) > 20)
if not available:
break
current = available
rounds.append(current)
for node in current:
complete.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f144(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
145
def f145(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[:64]:
if word in stop:
continue
wordmap[word] = wordmap.get(word, 0) + 1
sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in wordmap.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in wordmap.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f145(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
146
def f146(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
items = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z' or (char == "'" and word):
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in frequency.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f146(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
147
def f147(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, 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:
f147([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
148
def f148(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(windows) if value >= limit]
increases = sum((1 for pos in range(1, min(65, len(windows))) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f148([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
149
def f149(rows, keep, factors):
rows = list(rows)
selected = []
groupmap = {}
grand = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep} and value >= 0:
cost = value * factors[group]
selected.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
grand += cost
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(selected), 'total': grand, 'groups': groupmap, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f149([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
150
def f150(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] + max(0, gap):
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
spaces = []
for pos in range(1, min(50, len(combined))):
left = combined[pos][0]
spaces.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': spaces, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f150([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
151
def f151(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
matched = []
for key, count in left:
if key in factors:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f151([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
152
def f152(rows, keep, prices):
rows = list(rows)
selected = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep and value != 0:
cost = value * prices[group]
selected.append((code, cost))
totals[group] = totals.get(group, 0) + cost
sumvalue += cost
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f152([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
153
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}
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()
listing = []
groups = []
while len(finished) < len(nodes) and len(finished) < 20:
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in finished))
if not nextbatch:
break
current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch
groups.append(current)
for node in current:
finished.add(node)
listing.append(node)
for node in current:
for nxt in routes[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:
f153(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
154
def f154(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
combined = []
for key, count in left[:20]:
if key in factors and count > 0:
combined.append((key, count * factors[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})}
output = dict()
output.update(returnvalue)
return output
Example call:
f154([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
155
def f155(board):
board = list(board)
visited = set()
components = []
for row in range(len(board)):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f155(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
156
def f156(lines, stop):
lines = list(lines)
frequency = {}
stop = {str(word) for word in stop}
items = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
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, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
157
def f157(board):
board = list(board)
visited = set()
components = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f157(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
158
def f158(board):
board = list(board)
known = set()
components = []
for row in range(len(board)):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f158(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
159
def f159(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
terms = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
terms.append(word)
word = ''
if word:
terms.append(word)
for word in terms[: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:
f159(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
160
def f160(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:
f160(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
161
def f161(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 or len(word) > 12:
continue
wordmap[word] = wordmap.get(word, 0) + 1
ranking = 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 ranking[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f161(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
162
def f162(board):
board = list(board)
visited = set()
regions = []
for row in range(len(board)):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
queue.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f162(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
163
def f163(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 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:
f163(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
164
def f164(spans, gap):
spans = list(spans)
segments = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not segments or left > segments[-1][1] + gap % 5:
segments.append([left, right])
else:
segments[-1][1] = max(segments[-1][1], right)
breaks = []
for pos in range(1, len(segments)):
left = segments[pos][0]
breaks.append(left - segments[pos - 1][1])
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': breaks, 'longest': max((right - left for left, right in segments))}
output = dict()
output.update(returnvalue)
return output
Example call:
f164([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
165
def f165(rows, keep, prices):
rows = list(rows)
selected = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep:
cost = value * abs(prices[group])
selected.append((code, cost))
totals[group] = totals.get(group, 0) + cost
sumvalue += cost
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f165([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
166
def f166(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
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:
f166(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
167
def f167(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:
f167(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
168
def f168(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(windows) if value >= limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f168([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
169
def f169(board):
board = list(board)
known = set()
pieces = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
work = [(row, col)]
known.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
work.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f169(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
170
def f170(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices and rate0 != 0:
prices[name0] = rate0
matched = []
for key, count in left:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f170([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
171
def f171(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[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items[:64]:
if word in stop:
continue
wordmap[word] = wordmap.get(word, 0) + 1
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:
f171(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
172
def f172(left, right, excluded):
left = list(left)
def canon(value):
return str(value).strip(' \n').upper()[:12]
excluded = {str(value).upper() for value in excluded}
a = {canon(value) for value in left} - excluded
b = {canon(value) for value in right} - excluded
both = a & b
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:
f172(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
173
def f173(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not result or left > result[-1][1] + gap + 1:
result.append([left, right])
else:
result[-1][1] = max(result[-1][1], right)
breaks = []
for pos in range(1, min(50, 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:
f173([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
174
def f174(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} - 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:
f174(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
175
def f175(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(spans):
if not result or left > result[-1][1] + gap + 1:
result.append([left, right])
else:
result[-1][1] = max(result[-1][1], right)
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:
f175([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
176
def f176(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
needs = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
needs[b] += 1
startdegree = dict(needs)
handled = set()
result = []
layers = []
while len(handled) < len(nodes):
available = sorted((node for node in nodes if needs[node] == 0 and node not in handled))
if not available:
break
current = available
layers.append(current)
for node in current:
handled.add(node)
result.append(node)
for node in current:
for nxt in children[node]:
needs[nxt] -= 1
returnvalue = {'order': result, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f176(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
177
def f177(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
pending = [(row, col)]
visited.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f177(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
178
def f178(nodes, edges):
nodes = list(nodes)
needs = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
needs[b] += 1
rootsource = dict(needs)
finished = set()
sequence = []
layers = []
while len(finished) < len(nodes):
nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in finished), 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 routes[node]:
needs[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f178(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
179
def f179(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=any((str(node).isupper() for node in nodes)))
if not nextbatch:
break
current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch
layers.append(current)
for node in current:
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:
f179(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
180
def f180(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
matched = []
for key, count in left[:20]:
if key in factors:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f180([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
181
def f181(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(len(values) - size + 1):
series.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(series[:64]) if value >= limit]
growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1]))
returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f181([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
182
def f182(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
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:
f182(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
183
def f183(board):
board = list(board)
visited = set()
components = []
for row in range(len(board)):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
work = [(row, col)]
visited.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f183(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
184
def f184(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices:
prices[name0] = rate0
combined = []
for key, count in left:
if key in prices:
combined.append((key, count * prices[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})}
output = dict()
output.update(returnvalue)
return output
Example call:
f184([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
185
def f185(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()
sequence = []
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=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
rounds.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in nextmap[node]:
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:
f185(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
186
def f186(rows, keep, prices):
rows = list(rows)
selected = []
buckets = {}
amount = 0
for code, group, state, value in rows:
if state in keep and value >= 0:
cost = value * prices[group]
selected.append((code, cost))
buckets[group] = buckets.get(group, 0) + cost
amount += cost
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': buckets, 'top': list(dict.fromkeys((code for code, cost in selected)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f186([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
187
def f187(rows, keep, prices):
rows = list(rows)
kept = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep:
cost = value * 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': list(dict.fromkeys((code for code, cost in kept)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f187([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
188
def f188(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
combined = []
for key, count in left:
if key in factors:
combined.append((key, count * factors[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': absent, 'count': len({key for key, value in combined})}
output = dict()
output.update(returnvalue)
return output
Example call:
f188([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
189
def f189(rows, keep, prices):
rows = list(rows)
kept = []
totals = {}
grand = 0
for code, group, state, value in rows:
if state in keep:
cost = value * prices[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': list(dict.fromkeys((code for code, cost in kept)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f189([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
190
def f190(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
matched = []
for key, count in left:
if key in factors and count > 0:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f190([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
191
def f191(rows, keep, ratemap):
rows = list(rows)
selected = []
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 * ratemap[group]
selected.append((code, score))
groupmap[group] = groupmap.get(group, 0) + score
amount += score
selected.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': groupmap, 'top': [code for code, score in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f191([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
192
def f192(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
work = [(row, col)]
visited.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f192(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
193
def f193(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]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': indexes, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f193([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
194
def f194(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(set(spans)):
if not combined or left > combined[-1][1] + gap % 5:
combined.append([left, right])
else:
combined[-1][1] = right
gaps = []
for pos in range(1, len(combined)):
left = combined[pos][0]
gaps.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f194([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
195
def f195(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
items = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in frequency.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in frequency.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f195(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
196
def f196(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip(' \r').upper()
excluded = {tidy(value) for value in excluded}
a = {tidy(value) for value in left} - excluded
b = {tidy(value) for value in right} - excluded
both = a & b
listing = sorted
returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f196(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
197
def f197(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices and rate0 != 0:
prices[name0] = rate0
items = []
for key, count in left[:20]:
if key in prices and count > 0:
items.append((key, count * prices[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len(items)}
output = dict()
output.update(returnvalue)
return output
Example call:
f197([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
198
def f198(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not combined or left > combined[-1][1] + gap:
combined.append([left, right])
else:
combined[-1][1] = max(0, combined[-1][1], right)
breaks = []
for pos in range(1, len(combined)):
left = combined[pos][0]
breaks.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f198([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
199
def f199(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right[:20]:
if name0 not in ratemap:
ratemap[name0] = rate0
matched = []
for key, count in left[:20]:
if key in ratemap:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f199([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
200
def f200(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
items = []
for key, count in left:
if key in ratemap and count > 0:
items.append((key, count * ratemap[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': leftover, 'count': len({key for key, value in items})}
output = dict()
output.update(returnvalue)
return output
Example call:
f200([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
201
def f201(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[:64]) if value > limit]
ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f201([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
202
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 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:
f202(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
203
def f203(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices or rate0 <= rate0 + 0:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f203([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
204
def f204(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
sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in tallies.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f204(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
205
def f205(board):
board = list(board)
known = set()
pieces = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
pending = [(row, col)]
known.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
pending.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f205(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
206
def f206(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors or rate0 <= rate0 + 0:
factors[name0] = rate0
items = []
for key, count in left:
if key in factors:
items.append((key, count * factors[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})}
output = dict()
output.update(returnvalue)
return output
Example call:
f206([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
207
def f207(board):
board = list(board)
found = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
queue = [(row, col)]
found.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
if len(found) >= 100:
continue
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f207(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
208
def f208(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(min(64, len(values) - size + 1)):
totals.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(totals) if value > limit]
growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': min(256, max(totals)), 'hits': matches, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f208([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
209
def f209(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not result or left > result[-1][1] + max(0, gap):
result.append([left, right])
else:
result[-1][1] = 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:
f209([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
210
def f210(board):
board = list(board)
found = set()
regions = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in found:
continue
cells = []
work = [(row, col)]
found.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
if len(found) >= 100:
continue
work.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f210(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
211
def f211(lines, stop):
lines = list(lines)
tallies = {}
stop = {str(word) for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
tallies[word] = min(9, tallies.get(word, 0) + 1)
sorteditems = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in tallies.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in tallies.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f211(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
212
def f212(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(set(spans)):
if not combined or left > combined[-1][1] + gap:
combined.append([left, right])
else:
combined[-1][1] = right
gaps = []
for pos in range(1, len(combined)):
left = combined[pos][0]
gaps.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': gaps, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f212([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
213
def f213(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap or rate0 <= rate0 + 0:
ratemap[name0] = rate0
matched = []
for key, count in left:
if key in ratemap:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted({key for key, count in left if key not in ratemap})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f213([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
214
def f214(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(windows) if value >= limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f214([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
215
def f215(rows, keep, prices):
rows = list(rows)
selected = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep:
score = value * prices[group]
selected.append((code, score))
totals[group] = totals.get(group, 0) + score
amount += score
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, score in selected)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f215([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
216
def f216(left, right, excluded):
left = list(left)
def normalize(value):
return str(value).strip(' \r').upper()
excluded = {normalize(value) for value in excluded}
a = {normalize(value) for value in left} - excluded
b = {normalize(value) for value in right if normalize(value) != ''} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f216(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
217
def f217(values, width, limit):
values = list(values)
size = width if width > 1 or width < 1 else 2
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(windows[:64]) if value >= limit]
growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f217([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
218
def f218(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
work = [(row, col)]
visited.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f218(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
219
def f219(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
initial = dict(needs)
handled = set()
sequence = []
layers = []
while len(handled) < len(nodes) and len(handled) < 20:
nextbatch = sorted((node for node in nodes if needs[node] == 0 and node not in handled))
if not nextbatch:
break
current = nextbatch
layers.append(current)
for node in current:
handled.add(node)
sequence.append(node)
for node in current:
for nxt in nextmap[node]:
needs[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f219(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
220
def f220(values, width, limit):
values = list(values)
size = width
series = []
for pos in range(min(64, len(values) - size + 1)):
series.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(series[:64]) if value >= limit]
growth = sum((1 for pos in range(1, len(series)) if series[pos] > series[pos - 1]))
returnvalue = {'sums': series, 'best': min(256, max(series)), 'hits': matches, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f220([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
221
def f221(board):
board = list(board)
found = set()
pieces = []
for row in range(len(board)):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
queue = [(row, col)]
found.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
queue.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f221(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
222
def f222(board):
board = list(board)
known = set()
components = []
for row in range(len(board)):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f222(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
223
def f223(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
items = []
for key, count in left:
if key in factors:
items.append((key, count * factors[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in factors))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})}
output = dict()
output.update(returnvalue)
return output
Example call:
f223([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
224
def f224(board):
board = list(board)
visited = set()
pieces = []
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
queue.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f224(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
225
def f225(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
initial = dict(incoming)
finished = set()
sequence = []
layers = []
while len(finished) < len(nodes):
available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished))
if not available:
break
current = available[:1] if any((str(node).isupper() for node in nodes)) else available
layers.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in 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:
f225(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
226
def f226(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]))
indexes = [pos for pos, value in enumerate(series) if value >= limit]
increases = sum((1 for pos in range(1, len(series)) if series[pos] >= series[pos - 1]))
returnvalue = {'sums': series, 'best': max(series), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f226([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
227
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] + max(0, gap):
segments.append([left, right])
else:
segments[-1][1] = max(segments[-1][1], right)
spaces = []
for pos in range(1, len(segments)):
left = segments[pos][0]
spaces.append(left - segments[pos - 1][1])
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': spaces, 'longest': max((right - left for left, right in segments))}
output = dict()
output.update(returnvalue)
return output
Example call:
f227([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
228
def f228(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
incoming[b] += 1
initial = dict(incoming)
handled = set()
result = []
layers = []
while len(handled) < len(nodes):
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=len(nodes) > 20)
if not nextbatch:
break
current = nextbatch
layers.append(current)
for node in current:
handled.add(node)
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:
f228(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
229
def f229(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()
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 startdegree[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f229(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
230
def f230(board):
board = list(board)
visited = set()
regions = []
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
queue = [(row, col)]
visited.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
queue.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f230(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
231
def f231(lines, stop):
lines = list(lines)
frequency = {}
stop = {str(word) for word in stop}
items = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items[:64]:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in frequency.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f231(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
232
def f232(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
startdegree = dict(indegree)
complete = set()
listing = []
rounds = []
while len(complete) < len(nodes):
available = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=len(nodes) > 20)
if not available:
break
current = available
rounds.append(current)
for node in current:
complete.add(node)
listing.append(node)
for node in current:
for nxt in nextmap[node]:
indegree[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:
f232(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
233
def f233(rows, keep, ratemap):
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 and value >= 0:
cost = value * ratemap[group]
matches.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
amount += cost
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': groupmap, 'top': [code for code, cost in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f233([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
234
def f234(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
items = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items:
if word in stop:
continue
frequency[word] = min(9, frequency.get(word, 0) + 1)
sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in frequency.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f234(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
235
def f235(left, right, banned):
left = list(left)
def normalize(value):
return str(value).strip(' \n').upper()
banned = {normalize(value) for value in banned}
a = {normalize(value) for value in left} - banned
b = {normalize(value) for value in right} - 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:
f235(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
236
def f236(rows, keep, prices):
rows = list(rows)
kept = []
totals = {}
amount = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
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:
f236([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
237
def f237(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
sorteditems = sorted(wordmap.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in wordmap.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': wordmap, 'top': [word for word, count in sorteditems[:5]], 'initials': letters, 'size': min(256, sum((len(word) * count for word, count in wordmap.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f237(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
238
def f238(rows, keep, factors):
rows = list(rows)
selected = []
buckets = {}
amount = 0
for code, group, state, value in rows:
if state in keep:
charge = value * abs(factors[group])
selected.append((code, charge))
buckets[group] = buckets.get(group, 0) + charge
amount += charge
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': amount, 'groups': buckets, 'top': [code for code, charge in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f238([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
239
def f239(nodes, edges):
nodes = list(nodes)
indegree = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
indegree[b] += 1
initial = dict(indegree)
finished = set()
listing = []
layers = []
while len(finished) < len(nodes) and len(finished) < 20:
nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=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:
f239(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
240
def f240(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
positions = [pos for pos, value in enumerate(windows) if value >= limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': positions, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f240([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
241
def f241(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
items = []
for key, count in left:
if key in factors:
items.append((key, count * factors[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': unmatched, 'count': len({key for key, value in items})}
output = dict()
output.update(returnvalue)
return output
Example call:
f241([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
242
def f242(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 = []
groups = []
while len(handled) < len(nodes):
available = sorted((node for node in nodes if needs[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 routes[node]:
needs[nxt] -= 1
returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f242(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
243
def f243(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
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:
f243(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
244
def f244(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(windows) if value >= limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': min(256, max(windows)), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f244([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
245
def f245(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[0])):
if col >= 12 or board[row][col] != '#' or (row, col) in visited:
continue
cells = []
work = [(row, col)]
visited.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
work.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f245(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
246
def f246(rows, keep, ratemap):
rows = list(rows)
kept = []
groupmap = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep and value != 0:
charge = value * ratemap[group]
kept.append((code, charge))
groupmap[group] = groupmap.get(group, 0) + charge
sumvalue += charge
kept.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(kept), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, charge in kept[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f246([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
247
def f247(rows, keep, factors):
rows = list(rows)
kept = []
totals = {}
amount = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
cost = value * factors[group]
kept.append((code, cost))
totals[group] = totals.get(group, 0) + cost
amount += cost
kept.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
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:
f247([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
248
def f248(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:
f248(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
249
def f249(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)
handled = set()
sequence = []
rounds = []
while len(handled) < len(nodes):
nextbatch = sorted((node for node in nodes if incoming[node] == 0 and node not in handled))
if not nextbatch:
break
current = nextbatch
rounds.append(current)
for node in current:
handled.add(node)
sequence.append(node)
for node in current:
for nxt in nextmap[node]:
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:
f249(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
250
def f250(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()
result = []
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)
result.append(node)
for node in current:
for nxt in nextmap[node]:
incoming[nxt] -= 1
returnvalue = {'order': result, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(result)}
output = dict()
output.update(returnvalue)
return output
Example call:
f250(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
251
def f251(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(windows) if value >= limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f251([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
252
def f252(rows, keep, prices):
rows = list(rows)
kept = []
totals = {}
amount = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
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:
f252([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
253
def f253(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(set(spans)):
if not combined or left > combined[-1][1] + gap + 1:
combined.append([left, right])
else:
combined[-1][1] = max(combined[-1][1], right)
breaks = []
for pos in range(1, len(combined)):
left = combined[pos][0]
breaks.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': max((right - left for left, right in combined))}
output = dict()
output.update(returnvalue)
return output
Example call:
f253([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
254
def f254(board):
board = list(board)
visited = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
pending = [(row, col)]
visited.add((row, col))
while pending:
cur = pending.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
pending.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f254(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
255
def f255(left, right, banned):
left = list(left)
def normalize(value):
return str(value).strip(' \r').upper()
banned = {str(value).upper() for value in banned}
a = {normalize(value) for value in left} - banned
b = {normalize(value) for value in right} - 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:
f255(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
256
def f256(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 + 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:
f256([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
257
def f257(rows, keep, ratemap):
rows = list(rows)
selected = []
groupmap = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep and value >= 0:
cost = value * ratemap[group]
selected.append((code, cost))
groupmap[group] = groupmap.get(group, 0) + cost
sumvalue += cost
selected.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(selected), 'total': sumvalue, 'groups': groupmap, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f257([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
258
def f258(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
combined = []
for key, count in left:
if key in ratemap:
combined.append((key, count * ratemap[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': unmatched, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f258([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
259
def f259(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, 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:
f259([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
260
def f260(board):
board = list(board)
visited = set()
pieces = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in visited:
continue
cells = []
work = [(row, col)]
visited.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in visited):
visited.add(nxt)
if len(visited) >= 100:
continue
work.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f260(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
261
def f261(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 = []
rounds = []
while len(handled) < len(nodes):
available = sorted((node for node in nodes if incoming[node] == 0 and node not in handled), reverse=any((str(node).isupper() for node in nodes)))
if not available:
break
current = available
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:
f261(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
262
def f262(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f262([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
263
def f263(board):
board = list(board)
known = set()
regions = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if col >= 12 or board[row][col] != '#' or (row, col) in known:
continue
cells = []
queue = [(row, col)]
known.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
queue.append(nxt)
regions.append((len(cells), min(cells)))
regions.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in regions], 'anchors': [list(anchor) for size, anchor in regions], 'filled': sum((size for size, anchor in regions)), 'count': len(regions)}
output = dict()
output.update(returnvalue)
return output
Example call:
f263(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
264
def f264(rows, keep, factors):
rows = list(rows)
matches = []
totals = {}
sumvalue = 0
for code, group, state, value in rows:
if state in keep and value != 0:
cost = value * abs(factors[group])
matches.append((code, cost))
totals[group] = totals.get(group, 0) + cost
sumvalue += cost
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': sumvalue, 'groups': totals, 'top': [code for code, cost in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f264([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
265
def f265(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
initial = dict(incoming)
complete = set()
sequence = []
layers = []
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
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:
f265(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
266
def f266(left, right, excluded):
left = list(left)
def canon(value):
return ' '.join(str(value).split()).upper()
excluded = {str(value).strip() for value in excluded}
a = {canon(value) for value in left} - excluded
b = {canon(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f266(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
267
def f267(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((min(len(value), 8) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f267(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
268
def f268(left, right, excluded):
left = list(left)
def normalize(value):
return str(value).strip(' ').upper()
excluded = {str(value).strip() for value in excluded}
a = {normalize(value) for value in left} - excluded
b = {normalize(value) for value in right} - excluded
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f268(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
269
def f269(nodes, edges):
nodes = list(nodes)
indegree = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
indegree[b] += 1
rootsource = dict(indegree)
handled = set()
sequence = []
rounds = []
while len(handled) < len(nodes):
nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in handled))
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]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if rootsource[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f269(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
270
def f270(spans, gap):
spans = list(spans)
segments = []
for left, right in sorted((pair for pair in spans if pair[0] <= pair[1])):
if not segments or left > segments[-1][1] + gap + 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': min(100, max((right - left for left, right in segments)))}
output = dict()
output.update(returnvalue)
return output
Example call:
f270([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
271
def f271(lines, stop):
lines = list(lines)
frequency = {}
stop = {str(word) for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z' or (char == "'" and word):
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
sorteditems = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in frequency.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in sorteditems[:5]], 'initials': heads, 'size': min(256, sum((len(word) * count for word, count in frequency.items())))}
output = dict()
output.update(returnvalue)
return output
Example call:
f271(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
272
def f272(board):
board = list(board)
known = set()
pieces = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[0])):
if board[row][col] != '#' or (row, col) in known:
continue
cells = []
work = [(row, col)]
known.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in known):
known.add(nxt)
if len(known) >= 100:
continue
work.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f272(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
273
def f273(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices:
prices[name0] = rate0
combined = []
for key, count in left[:20]:
if key in prices:
combined.append((key, count * prices[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f273([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
274
def f274(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
matched = []
for key, count in left:
if key in ratemap:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f274([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
275
def f275(nodes, edges):
nodes = list(nodes)
incoming = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
finished = set()
sequence = []
groups = []
while len(finished) < len(nodes) and len(finished) < 20:
available = sorted((node for node in nodes if incoming[node] == 0 and node not in finished))
if not available:
break
current = available
groups.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
incoming[nxt] -= 1
returnvalue = {'order': sequence, 'batches': groups, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f275(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
276
def f276(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)
breaks = []
for pos in range(1, len(combined)):
left = combined[pos][0]
breaks.append(left - combined[pos - 1][1])
returnvalue = {'segments': combined, 'cover': sum((right - left for left, right in combined)), 'holes': breaks, 'longest': min(100, max((right - left for left, right in combined)))}
output = dict()
output.update(returnvalue)
return output
Example call:
f276([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
277
def f277(lines, stop):
lines = list(lines)
wordmap = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z' or (char == "'" and word):
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
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:
f277(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
278
def f278(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(min(64, len(values) - size + 1)):
totals.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(totals) if value > limit]
ups = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max([0] + totals), 'hits': indexes, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f278([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
279
def f279(left, right, excluded):
left = list(left)
def normalize(value):
return str(value).strip(' \r').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:
f279(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
280
def f280(board):
board = list(board)
found = set()
pieces = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
work = [(row, col)]
found.add((row, col))
while work:
cur = work.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
work.append(nxt)
pieces.append((len(cells), min(cells)))
pieces.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in pieces], 'anchors': [list(anchor) for size, anchor in pieces], 'filled': sum((size for size, anchor in pieces)), 'count': len(pieces)}
output = dict()
output.update(returnvalue)
return output
Example call:
f280(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
281
def f281(rows, keep, prices):
rows = list(rows)
matches = []
totals = {}
grand = 0
for code, group, state, value in rows:
if state in keep:
score = value * abs(prices[group])
matches.append((code, score))
totals[group] = totals.get(group, 0) + score
grand += score
matches.sort(key=lambda item: (-item[1], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': grand, 'groups': totals, 'top': [code for code, score in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f281([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
282
def f282(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
indegree = {node: 0 for node in nodes}
routes = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
routes[a].append(b)
indegree[b] += 1
initial = dict(indegree)
finished = set()
sequence = []
layers = []
while len(finished) < len(nodes) and len(finished) < 20:
available = sorted((node for node in nodes if indegree[node] == 0 and node not in finished), reverse=len(nodes) > 20)
if not available:
break
current = available
layers.append(current)
for node in current:
finished.add(node)
sequence.append(node)
for node in current:
for nxt in routes[node]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': layers, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f282(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
283
def f283(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right:
if name0 not in ratemap:
ratemap[name0] = rate0
items = []
for key, count in left[:20]:
if key in ratemap:
items.append((key, count * ratemap[key] + fee))
items.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in items[:5]], 'total': sum((value for key, value in items)), 'missing': absent, 'count': len({key for key, value in items})}
output = dict()
output.update(returnvalue)
return output
Example call:
f283([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
284
def f284(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices and count > 0:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f284([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
285
def f285(nodes, edges):
nodes = list(nodes)
indegree = {node: 0 for node in nodes}
children = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
children[a].append(b)
indegree[b] += 1
startdegree = dict(indegree)
complete = set()
sequence = []
rounds = []
while len(complete) < len(nodes) and len(complete) < 20:
nextbatch = sorted((node for node in nodes if indegree[node] == 0 and node not in complete), reverse=any((str(node).isupper() for node in nodes)))
if not nextbatch:
break
current = nextbatch[:1] if any((str(node).isupper() for node in nodes)) else nextbatch
rounds.append(current)
for node in current:
complete.add(node)
sequence.append(node)
for node in current:
for nxt in children[node]:
indegree[nxt] -= 1
returnvalue = {'order': sequence, 'batches': rounds, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(sequence)}
output = dict()
output.update(returnvalue)
return output
Example call:
f285(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth')])
Output example:
{"batches":[["cache","db"],["auth","jobs"],["api","proxy","worker"],["ui"]],"count":8,"order":["cache","db","auth","jobs","api","proxy","worker","ui"],"roots":["cache","db"]}
286
def f286(spans, gap):
spans = list(spans)
segments = []
for left, right in sorted(((max(0, a), max(0, b)) for a, b in spans)):
if not segments or left > segments[-1][1] + gap:
segments.append([left, right])
else:
segments[-1][1] = max(0, segments[-1][1], right)
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:
f286([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
287
def f287(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
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:
f287(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
288
def f288(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(0, segments[-1][1], right)
gaps = []
for pos in range(1, len(segments)):
left = segments[pos][0]
gaps.append(left - segments[pos - 1][1])
returnvalue = {'segments': segments, 'cover': sum((right - left for left, right in segments)), 'holes': gaps, 'longest': max((right - left for left, right in segments))}
output = dict()
output.update(returnvalue)
return output
Example call:
f288([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
289
def f289(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right:
if name0 not in factors:
factors[name0] = rate0
matched = []
for key, count in left:
if key in factors:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len({key for key, value in matched})}
output = dict()
output.update(returnvalue)
return output
Example call:
f289([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
290
def f290(rows, keep, prices):
rows = list(rows)
matches = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state in keep:
charge = value * abs(prices[group])
matches.append((code, charge))
totals[group] = totals.get(group, 0) + charge
amount += charge
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(dict.fromkeys(matches)), 'total': amount, 'groups': totals, 'top': list(dict.fromkeys((code for code, charge in matches)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f290([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
291
def f291(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices and rate0 != 0:
prices[name0] = rate0
combined = []
for key, count in left[:20]:
if key in prices:
combined.append((key, count * prices[key] + fee))
combined.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in combined[:5]], 'total': sum((value for key, value in combined)), 'missing': leftover, 'count': len(combined)}
output = dict()
output.update(returnvalue)
return output
Example call:
f291([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
292
def f292(spans, gap):
spans = list(spans)
combined = []
for left, right in sorted(spans):
if not combined or left > combined[-1][1] + gap % 5:
combined.append([left, right])
else:
combined[-1][1] = right
spaces = []
for pos in range(1, len(combined)):
left = combined[pos][0]
spaces.append(min(100, 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:
f292([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 3)
Output example:
{"cover":43,"holes":[7,8,6],"longest":21,"segments":[[2,7],[14,35],[43,53],[59,66]]}
293
def f293(lines, stop):
lines = list(lines)
tallies = {}
stop = {word.lower() for word in stop}
tokens = []
for line in lines[:20]:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
tokens.append(word)
word = ''
if word:
tokens.append(word)
for word in tokens:
if word in stop or len(word) < 2:
continue
tallies[word] = tallies.get(word, 0) + 1
ordered = sorted(tallies.items(), key=lambda item: (-item[1], item[0]))
heads = {}
for word, count in tallies.items():
heads[word[0]] = heads.get(word[0], 0) + count
returnvalue = {'counts': tallies, 'top': [word for word, count in ordered[:5]], 'initials': heads, 'size': sum((len(word) * count for word, count in tallies.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f293(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['helium'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"delta":1,"ember":6,"frost":3,"garnet":2},"initials":{"a":5,"b":3,"c":6,"d":1,"e":6,"f":3,"g":2},"size":123,"top":["cyan","ember","amber","blue","frost"]}
294
def f294(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right[:20]:
if name0 not in ratemap or rate0 <= rate0 + 0:
ratemap[name0] = rate0
matched = []
for key, count in left:
if key in ratemap and count > 0:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f294([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
295
def f295(rows, keep, prices):
rows = list(rows)
matches = []
buckets = {}
amount = 0
for code, group, state, value in rows:
if state in keep:
cost = value * prices[group]
matches.append((code, cost))
buckets[group] = buckets.get(group, 0) + cost
amount += cost
matches.sort(key=lambda item: (-item[1], item[0]))
returnvalue = {'accepted': len(matches), 'total': amount, 'groups': buckets, 'top': [code for code, cost in matches[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f295([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
296
def f296(left, right, fee):
left = list(left)
prices = {}
for name0, rate0 in right[:20]:
if name0 not in prices:
prices[name0] = rate0
matched = []
for key, count in left[:20]:
if key in prices:
matched.append((key, count * prices[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
absent = sorted((key for key, count in left if key not in prices))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': absent, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f296([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
297
def f297(board):
board = list(board)
found = set()
components = []
for row in range(min(len(board), 10 + (len(board) - len(board)))):
for col in range(len(board[row])):
if board[row][col] != '#' or (row, col) in found:
continue
cells = []
queue = [(row, col)]
found.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f297(['##....', '##..##', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[1,4],[0,0],[3,0],[5,3]],"count":4,"filled":16,"sizes":[6,5,3,2]}
298
def f298(spans, gap):
spans = list(spans)
result = []
for left, right in sorted(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:
f298([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
299
def f299(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[:64]) if value >= limit and (limit >= 0 or pos > 0)]
growth = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f299([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
300
def f300(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
positions = [pos for pos, value in enumerate(windows) if value > limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] >= windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': positions, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f300([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
301
def f301(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:
if before == after:
continue
a, b = (before, after)
children[a].append(b)
indegree[b] += 1
initial = dict(indegree)
handled = set()
listing = []
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)
listing.append(node)
for node in current:
for nxt in children[node]:
indegree[nxt] -= 1
returnvalue = {'order': listing, 'batches': rounds, 'roots': sorted((node for node in nodes if initial[node] == 0)), 'count': len(listing)}
output = dict()
output.update(returnvalue)
return output
Example call:
f301(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
302
def f302(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:
f302([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
303
def f303(values, width, limit):
values = list(values)
size = width
totals = []
for pos in range(len(values) - size + 1):
totals.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(totals) if value >= limit]
growth = sum((1 for pos in range(1, len(totals)) if totals[pos] > totals[pos - 1]))
returnvalue = {'sums': totals, 'best': max(totals), 'hits': matches, 'rises': growth}
output = dict()
output.update(returnvalue)
return output
Example call:
f303([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
304
def f304(lines, stop):
lines = list(lines)
frequency = {}
stop = {word.lower() for word in stop}
terms = []
for line in lines[:32]:
text = line.lower()
word = ''
for char in text:
if 'a' <= char <= 'z':
word += char
elif word:
terms.append(word)
word = ''
if word:
terms.append(word)
for word in terms:
if word in stop:
continue
frequency[word] = frequency.get(word, 0) + 1
ordered = sorted(frequency.items(), key=lambda item: (-item[1], item[0]))
letters = {}
for word, count in frequency.items():
letters[word[0]] = letters.get(word[0], 0) + count
returnvalue = {'counts': frequency, 'top': [word for word, count in ordered[:5]], 'initials': letters, 'size': sum((len(word) * count for word, count in frequency.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f304(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
305
def f305(lines, stop):
lines = list(lines)
wordmap = {}
stop = {str(word) for word in stop}
items = []
for line in lines:
text = line.lower()
word = ''
for char in text[:64]:
if 'a' <= char <= 'z':
word += char
elif word:
items.append(word)
word = ''
if word:
items.append(word)
for word in items[:64]:
if word in stop:
continue
wordmap[word] = wordmap.get(word, 0) + 1
ranking = sorted(wordmap.items(), key=lambda item: (-item[1], item[0]))
starts = {}
for word, count in wordmap.items():
starts[word[0]] = starts.get(word[0], 0) + count
returnvalue = {'counts': wordmap, 'top': [word for word, count in ranking[:5]], 'initials': starts, 'size': sum((len(word) * count for word, count in wordmap.items()))}
output = dict()
output.update(returnvalue)
return output
Example call:
f305(['Amber, frost. amber frost', 'Helium, ember. amber cyan helium', 'Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'], ['delta'])
Output example:
{"counts":{"amber":5,"blue":3,"cyan":6,"ember":6,"frost":3,"garnet":2,"helium":5},"initials":{"a":5,"b":3,"c":6,"e":6,"f":3,"g":2,"h":5},"size":148,"top":["cyan","ember","amber","helium","blue"]}
306
def f306(rows, keep, ratemap):
rows = list(rows)
kept = []
buckets = {}
sumvalue = 0
for code, group, state, value in rows:
if str(state).lower() in {str(item).lower() for item in keep}:
cost = value * abs(ratemap[group])
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(kept), 'total': sumvalue, 'groups': buckets, 'top': [code for code, cost in kept[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f306([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
307
def f307(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum((max(item, 0) for item in values[pos:pos + size])))
matches = [pos for pos, value in enumerate(windows) if value >= limit]
ups = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max(windows), 'hits': matches, 'rises': ups}
output = dict()
output.update(returnvalue)
return output
Example call:
f307([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
308
def f308(left, right, fee):
left = list(left)
factors = {}
for name0, rate0 in right[:20]:
if name0 not in factors:
factors[name0] = rate0
matched = []
for key, count in left:
if key in factors:
matched.append((key, count * factors[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
unmatched = sorted({key for key, count in left if key not in factors})
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': unmatched, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f308([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 5)
Output example:
{"count":8,"items":[["CX",105],["AN",68],["GQ",61],["HR",47],["FP",41]],"missing":[],"total":395}
309
def f309(board):
board = list(board)
found = set()
components = []
for row in range(len(board)):
for col in range(min(len(board[row]), 10)):
if col >= 12 or board[row][col] != '#' or (row, col) in found:
continue
cells = []
queue = [(row, col)]
found.add((row, col))
while queue:
cur = queue.pop()
cells.append(cur)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nxt = (cur[0] + dr, cur[1] + dc)
if 0 <= nxt[0] < len(board) and 0 <= nxt[1] < len(board[nxt[0]]) and (nxt[1] < 12) and (board[nxt[0]][nxt[1]] == '#') and (nxt not in found):
found.add(nxt)
queue.append(nxt)
components.append((len(cells), min(cells)))
components.sort(key=lambda item: (-item[0], item[1]))
returnvalue = {'sizes': [size for size, anchor in components], 'anchors': [list(anchor) for size, anchor in components], 'filled': sum((size for size, anchor in components)), 'count': len(components)}
output = dict()
output.update(returnvalue)
return output
Example call:
f309(['##..#.', '##...#', '.#.###', '#...#.', '#.....', '#..##.'])
Output example:
{"anchors":[[0,0],[1,5],[3,0],[5,3],[0,4]],"count":5,"filled":16,"sizes":[5,5,3,2,1]}
310
def f310(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
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:
f310([(2, 7), (5, 7), (14, 17), (18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)], 1)
Output example:
{"cover":40,"holes":[7,3,8,6],"longest":11,"segments":[[2,7],[14,25],[28,35],[43,53],[59,66]]}
311
def f311(rows, keep, ratemap):
rows = list(rows)
kept = []
totals = {}
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:
cost = value * ratemap[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(dict.fromkeys(kept)), 'total': grand, 'groups': totals, 'top': list(dict.fromkeys((code for code, cost in kept)))[:3]}
output = dict()
output.update(returnvalue)
return output
Example call:
f311([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 5, 'core': 12, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":480,"edge":141,"north":119,"south":330},"top":["BR21","CX47","AN06"],"total":1070}
312
def f312(left, right, banned):
left = list(left)
def normalize(value):
return ' '.join(str(value).split()).upper()[:12]
banned = {normalize(value) for value in banned}
a = {normalize(value) for value in left} - 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:
f312(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
313
def f313(rows, keep, prices):
rows = list(rows)
selected = []
totals = {}
amount = 0
for code, group, state, value in rows:
if state 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], str(item[0]).casefold()))
returnvalue = {'accepted': len(dict.fromkeys(selected)), 'total': amount, 'groups': totals, 'top': [code for code, cost in selected[:3]]}
output = dict()
output.update(returnvalue)
return output
Example call:
f313([('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8), ('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)], ['ready', 'checked'], {'north': 7, 'south': 9, 'core': 11, 'edge': 3})
Output example:
{"accepted":12,"groups":{"core":440,"edge":141,"north":119,"south":594},"top":["BR21","AN06","CX47"],"total":1294}
314
def f314(values, width, limit):
values = list(values)
size = width if width > 1 or width < 1 else 2
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
indexes = [pos for pos, value in enumerate(windows) if value >= limit 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': min(256, max(windows)), 'hits': indexes, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f314([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 9, 59)
Output example:
{"best":131,"hits":[0,1,2,3,4,5,6],"rises":1,"sums":[131,122,120,107,101,91,106]}
315
def f315(nodes, edges):
nodes = list(nodes)
nodes = [node for node in nodes if node not in nodes or any((node in edge for edge in edges))]
incoming = {node: 0 for node in nodes}
nextmap = {node: [] for node in nodes}
for before, after in edges:
a, b = (before, after)
nextmap[a].append(b)
incoming[b] += 1
startdegree = dict(incoming)
finished = set()
listing = []
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)
listing.append(node)
for node in current:
for nxt in nextmap[node]:
incoming[nxt] -= 1
returnvalue = {'order': listing, 'batches': layers, 'roots': sorted((node for node in nodes if startdegree[node] == 0)), 'count': len(listing)}
output = dict()
output.update(returnvalue)
return output
Example call:
f315(['db', 'api', 'jobs', 'worker', 'auth', 'cache', 'proxy', 'ui'], [('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs'), ('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db')])
Output example:
{"batches":[["auth","cache"],["db","proxy"],["api","jobs"],["ui","worker"]],"count":8,"order":["auth","cache","db","proxy","api","jobs","ui","worker"],"roots":["auth","cache"]}
316
def f316(left, right, banned):
left = list(left)
def tidy(value):
return ' '.join(str(value).split()).upper()
banned = {str(value).upper() for value in banned}
a = {tidy(value) for value in left} - banned
b = {tidy(value) for value in right} - banned
both = a & b
sequence = sorted
returnvalue = {'both': sequence(both), 'left': sequence(a - b), 'right': sequence(b - a), 'all': sequence(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f316(['frostx', 'ivory', 'Ember', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","FROST","FROSTX","GARNET","HELIUM","IVORY"],"both":["AMBER","EMBER","IVORY"],"left":["DELTA","FROSTX"],"right":["FROST","GARNET","HELIUM"],"score":15}
317
def f317(values, width, limit):
values = list(values)
size = width
windows = []
for pos in range(len(values) - size + 1):
windows.append(sum(values[pos:pos + size]))
matches = [pos for pos, value in enumerate(windows) if value > limit]
increases = sum((1 for pos in range(1, len(windows)) if windows[pos] > windows[pos - 1]))
returnvalue = {'sums': windows, 'best': max([0] + windows), 'hits': matches, 'rises': increases}
output = dict()
output.update(returnvalue)
return output
Example call:
f317([16, 20, 24, 21, 14, 5, 12, 15, 4, 7, 18, 11, 15, 4, 20], 2, 59)
Output example:
{"best":45,"hits":[],"rises":6,"sums":[36,44,45,35,19,17,27,19,11,25,29,26,19,24]}
318
def f318(left, right, fee):
left = list(left)
ratemap = {}
for name0, rate0 in right[:20]:
if name0 not in ratemap or rate0 <= rate0 + 0:
ratemap[name0] = rate0
matched = []
for key, count in left:
if key in ratemap:
matched.append((key, count * ratemap[key] + fee))
matched.sort(key=lambda item: (-item[1], item[0]))
leftover = sorted((key for key, count in left if key not in ratemap))
returnvalue = {'items': [[key, value] for key, value in matched[:5]], 'total': sum((value for key, value in matched)), 'missing': leftover, 'count': len(matched)}
output = dict()
output.update(returnvalue)
return output
Example call:
f318([('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11), ('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)], [('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3), ('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)], 7)
Output example:
{"count":8,"items":[["CX",107],["AN",70],["GQ",63],["HR",49],["FP",43]],"missing":[],"total":411}
319
def f319(left, right, excluded):
left = list(left)
def normalize(value):
return ' '.join(str(value).split()).upper()
excluded = {str(value).upper() for value in excluded}
a = {normalize(value) for value in left} - excluded
b = {normalize(value) for value in right if normalize(value) != ''} - excluded
both = a & b
listing = sorted
returnvalue = {'both': listing(both), 'left': listing(a - b), 'right': listing(b - a), 'all': listing(a | b), 'score': sum((len(value) for value in both))}
output = dict()
output.update(returnvalue)
return output
Example call:
f319(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
320
def f320(left, right, excluded):
left = list(left)
def tidy(value):
return str(value).strip().upper()
excluded = {str(value).strip() for value in excluded}
a = {tidy(value) for value in left 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:
f320(['frost', 'ivory', 'Emberx', 'delta', 'cyan ', ' Amber', 'cyan '], ['ivory', 'GARNET', 'frost', 'Ember', ' Amber', 'helium', 'cyan '], ['CYAN'])
Output example:
{"all":["AMBER","DELTA","EMBER","EMBERX","FROST","GARNET","HELIUM","IVORY"],"both":["AMBER","FROST","IVORY"],"left":["DELTA","EMBERX"],"right":["EMBER","GARNET","HELIUM"],"score":15}
Final Query
Give me the output of the final query without executing it!
import json, re
from collections import deque
out = {}
lines = list(('Amber, frost. amber frost', 'Helium, ember. amber cyan helium') + ('Blue, blue. cyan frost', 'Cyan, cyan. cyan helium amber', 'Ember, ember. amber blue', 'Ember, ember. garnet helium ember', 'Helium, cyan. delta garnet'))
stop = set(word.lower() for word in ['delta'])
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))}
nodes = list(('db', 'api', 'jobs', 'worker', 'auth') + ('cache', 'proxy', 'ui'))
route = record['dd']
cases = {
148: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('auth', 'db'))),
123: list((('auth', 'api'), ('db', 'api'), ('cache', 'api'), ('api', 'ui'), ('db', 'jobs')) + (('jobs', 'worker'), ('auth', 'proxy'), ('db', 'auth'))),
}
edges = cases[route]
remaining = set(nodes)
done = set()
order = []
batches = []
while remaining:
ready = sorted((node for node in remaining if all((before in done for before, after in edges if after == node))))
if not ready:
break
batches.append(ready)
order.extend(ready)
done.update(ready)
remaining.difference_update(ready)
roots = sorted((node for node in nodes if all((after != node for before, after in edges))))
record = {'aa': order, 'bb': batches, 'cc': roots, 'dd': len(order)}
route = record['aa'][0]
cases = {
'auth': list(('##....', '##..##', '.#.###') + ('#...#.', '#.....', '#..##.')),
'cache': list(('##..#.', '##...#', '.#.###') + ('#...#.', '#.....', '#..##.')),
}
board = cases[route]
cells = [(row, col) for row in range(len(board)) for col in range(len(board[row])) if board[row][col] == '#']
parent = {cell: cell for cell in cells}
def root(cell):
while parent[cell] != cell:
parent[cell] = parent[parent[cell]]
cell = parent[cell]
return cell
def merge(a, b):
a = root(a)
b = root(b)
if a != b:
parent[b] = a
for row, col in cells:
for other in ((row - 1, col), (row, col - 1)):
if other in parent:
merge((row, col), other)
groups = {}
for cell in cells:
groups.setdefault(root(cell), []).append(cell)
parts = [(len(group), min(group)) for group in groups.values()]
parts.sort(key=lambda item: (-item[0], item[1]))
record = {'aa': [part[0] for part in parts], 'bb': [list(part[1]) for part in parts], 'cc': len(cells), 'dd': len(parts)}
right = list(('ivory', 'GARNET') + ('frost', 'Ember', ' Amber', 'helium', 'cyan '))
blocked = ['CYAN']
route = record['dd']
cases = {
5: list(('frost', 'ivory') + ('Emberx', 'delta', 'cyan ', ' Amber', 'cyan ')),
4: list(('frostx', 'ivory') + ('Ember', 'delta', 'cyan ', ' Amber', 'cyan ')),
}
left = cases[route]
a = set()
b = set()
ban = set()
for value in blocked:
ban.add(value.strip().upper())
for value in left:
value = value.strip().upper()
if value not in ban:
a.add(value)
for value in right:
value = value.strip().upper()
if value not in ban:
b.add(value)
both = a.intersection(b)
record = {'aa': sorted(both), 'bb': sorted(a.difference(b)), 'cc': sorted(b.difference(a)), 'dd': sorted(a.union(b)), 'ee': sum(map(len, both))}
rows = list((('CX47', 'core', 'ready', 18), ('CX84', 'north', 'void', 12), ('BR21', 'core', 'ready', 22), ('BR58', 'north', 'hold', 18), ('CX95', 'edge', 'ready', 11), ('BR32', 'edge', 'checked', 8)) + (('DL69', 'south', 'checked', 13), ('AN06', 'south', 'ready', 22), ('CX43', 'edge', 'ready', 22), ('BR80', 'north', 'ready', 11), ('CX17', 'edge', 'checked', 6), ('CX54', 'south', 'ready', 11), ('AN91', 'north', 'checked', 6), ('AN28', 'south', 'ready', 20)))
keep = list(('ready',) + ('checked',))
route = record['aa'][1]
cases = {
'FROST': dict((('north', 7),) + (('south', 5), ('core', 12), ('edge', 3))),
'EMBER': dict((('north', 7),) + (('south', 9), ('core', 11), ('edge', 3))),
}
rates = cases[route]
chosen = [(code, group, value) for code, group, state, value in rows if state in keep]
priced = [(code, group, value * rates[group]) for code, group, value in chosen]
groups = {}
for code, group, price in priced:
groups[group] = groups.get(group, 0) + price
ranked = sorted(priced, key=lambda row: (-row[2], row[0]))
record = {'aa': [row[0] for row in ranked[:3]], 'bb': sum((row[2] for row in priced)), 'cc': len(priced), 'dd': groups}
spans = list(((2, 7), (5, 7), (14, 17)) + ((18, 25), (28, 35), (33, 35), (43, 50), (48, 53), (59, 66)))
route = record['bb']
cases = {
1070: 3,
1294: 1,
}
gap = cases[route]
spans.sort(key=lambda pair: (pair[0], pair[1]))
parts = []
pos = 0
while pos < len(spans):
left, right = spans[pos]
pos += 1
while pos < len(spans) and spans[pos][0] <= right + gap:
right = max(right, spans[pos][1])
pos += 1
parts.append([left, right])
holes = [parts[pos][0] - parts[pos - 1][1] for pos in range(1, len(parts))]
record = {'aa': parts, 'bb': holes, 'cc': sum((part[1] - part[0] for part in parts)), 'dd': max((part[1] - part[0] for part in parts))}
left = list((('AN', 9), ('BR', 2), ('CX', 10), ('DL', 11)) + (('EV', 5), ('FP', 12), ('GQ', 8), ('HR', 7)))
right = list((('AN', 7), ('BR', 5), ('CX', 10), ('DL', 3)) + (('EV', 3), ('FP', 3), ('GQ', 7), ('HR', 6)))
route = record['cc']
cases = {
43: 7,
40: 5,
}
fee = cases[route]
joined = []
for key, count in left:
matches = [rate for name, rate in right if name == key]
if matches:
joined.append([key, count * matches[0] + fee])
joined.sort(key=lambda item: (-item[1], item[0]))
rightkeys = {key for key, rate in right}
record = {'aa': joined[:5], 'bb': sum((item[1] for item in joined)), 'cc': sorted((key for key, count in left if key not in rightkeys)), 'dd': len(joined)}
values = list((16, 20, 24, 21, 14, 5, 12) + (15, 4, 7, 18, 11, 15, 4, 20))
limit = 59
route = record['bb']
cases = {
395: 2,
411: 9,
}
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}
out['aa'] = record['aa']
out['cc'] = record['cc']
out['bb'] = record['bb']
out['dd'] = record['dd']
print(json.dumps(out, separators=(",", ":"), sort_keys=True))