БГ
def closest_zero(idx:int, lst: list):
prev_nil = lst.index(0)
next_nil = lst.index(0, idx)
closest = min(prev_nil, next_nil, key=lambda n: abs(idx-n))
return closest
Size: a a a
БГ
def closest_zero(idx:int, lst: list):
prev_nil = lst.index(0)
next_nil = lst.index(0, idx)
closest = min(prev_nil, next_nil, key=lambda n: abs(idx-n))
return closest
IT
БГ
def closest_zero(idx: int, lst: list):
lst_len = len(lst)
max_shift = max(idx, lst_len-idx)
for shift in range(max_shift):
back_i = idx-shift
forw_i = idx+shift
for i in back_i, forw_i:
if i not in range(lst_len):
continue
if not lst[i]:
return i
else:
return None
БГ
def closest_zero(idx: int, lst: list):
lst_len = len(lst)
max_shift = max(idx, lst_len-idx)
for shift in range(max_shift):
back_i = idx-shift
forw_i = idx+shift
for i in back_i, forw_i:
if i not in range(lst_len):
continue
if not lst[i]:
return i
else:
return None
J
houses=[0, 5, 6, 2, 0, 4, 3, 1]
for i in range(len(houses)):
if houses[i]!=0:
b=[list(reversed(houses[:i + 1])), houses[i:]]
left=b[0].index(0) if 0 in b[0] else len(houses)
right=b[1].index(0) if 0 in b[1] else len(houses)
houses[i]=min(left,right)
print(*houses)
J
pl
БГ
БГ
houses=[0, 5, 6, 2, 0, 4, 3, 1]
for i in range(len(houses)):
if houses[i]!=0:
b=[list(reversed(houses[:i + 1])), houses[i:]]
left=b[0].index(0) if 0 in b[0] else len(houses)
right=b[1].index(0) if 0 in b[1] else len(houses)
houses[i]=min(left,right)
print(*houses)
houses=[0, 5, 6, 2, 0, 4, 3, 1]
counts = []
rev_counts = []
count = rev_count = 0
for i in range(len(houses)):
count = count+1 if houses[i] else 0
counts.append(count)
rev_count = rev_count+1 if houses[-i-1] else 0
rev_counts.append(rev_count)
result = [min(back, forw) for back, forw in zip(counts, reversed(rev_counts))]
БГ
houses=[0, 5, 6, 2, 0, 4, 3, 1]
counts = []
rev_counts = []
count = rev_count = 0
for i in range(len(houses)):
count = count+1 if houses[i] else 0
counts.append(count)
rev_count = rev_count+1 if houses[-i-1] else 0
rev_counts.append(rev_count)
result = [min(back, forw) for back, forw in zip(counts, reversed(rev_counts))]
J
J
IT
БГ
БГ
J
БГ
J
БГ
БГ