harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-1301harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-1301pseudocode
Algorithm _find_uptime(series: pd.Series, downtime: bool = False) -> pd.DataFrame // Preconditions // - series.index is UTC-aware DateTimeIndex // - series.values are numeric; 0 indicates downtime, non-zero indicates uptime
n ← length(series) idx ← series.index vals ← series.values is_zero[i] ← (vals[i] == 0) for i in [0, n-1]
// Build contiguous zero blocks: list of (start_idx, end_idx) inclusive blocks ← empty list i ← 0 while i < n if is_zero[i] == true s ← i while i + 1 < n and is_zero[i + 1] == true i ← i + 1 e ← i append (s, e) to blocks i ← i + 1
// Edge cases: no zeros or all zeros if blocks is empty if downtime == true return empty DataFrame with columns ["shutdown","startup"] else row ← { "startup": NaT, "shutdown": NaT } return DataFrame([row], columns ["startup","shutdown"]) end if end if
// All zeros? a single block spanning entire series if length(blocks) == 1 and blocks[0] == (0, n - 1) if downtime == true row ← { "shutdown": NaT, "startup": NaT } return DataFrame([row], columns ["shutdown","startup"]) else return empty DataFrame with columns ["startup","shutdown"] end if end if
// Helper: time_of(k) = idx[k] // a pandas Timestamp function time_of(k) return idx[k]
if downtime == true rows ← empty list for each (s, e) in blocks if s == 0 row ← { "shutdown": NaT, "startup": time_of(s) } else if e == n - 1 row ← { "shutdown": time_of(s), "startup": NaT } else row ← { "shutdown": time_of(s), "startup": time_of(e) } end if append row to rows end for return DataFrame(rows, columns ["shutdown","startup"]) else // downtime == false rows ← empty list
// 1) inter-block boundaries: between consecutive zero blocks
for k from 0 to length(blocks) - 2
(s_k, e_k) ← blocks[k]
(s_next, e_next) ← blocks[k + 1]
row ← { "startup": time_of(e_k), "shutdown": time_of(s_next) }
append row to rows
end for
// 2) internal boundaries inside a block of length > 1
for each (s, e) in blocks
if (e - s + 1) > 1
row_start ← { "shutdown": time_of(s), "startup": NaT }
row_end ← { "startup": time_of(e), "shutdown": NaT }
append row_start to rows
append row_end to rows
end if
end for
if rows is empty
return empty DataFrame with columns ["startup","shutdown"]
else
return DataFrame(rows, columns ["startup","shutdown"])
end if
end if