defviz_binary_search(func:Callable?=none,width:int?=none)"""Trace a binary search: one bar per step, mid marked ✓/✗ by ok(mid)."""width=widthormin(100,shutil.get_terminal_size().columns)ifnonedefdecorator(f:Callable)->Callabledefwrapper(*args,**kwargs)viz=_Viz(f,width)bound=inspect.signature(f).bind(*args,**kwargs)forname,vinbound.arguments.items()ifcallable(v)bound.arguments[name]=_record(viz.calls,v,name)args,kwargs=bound.args,bound.kwargsold=sys.gettrace()sys.settrace(viz.trace)tryresult=f(*args,**kwargs)finallysys.settrace(old)viz.finish(result)resultwrappercallable(func)?decorator(func):decorator
defviz_binary_search(func:Optional[Callable]=None,width:Optional[int]=None):"""Trace a binary search: one bar per step, mid marked ✓/✗ by ok(mid)."""width=(min(100,shutil.get_terminal_size().columns)ifwidth==Noneelsewidth)defdecorator(f:Callable)->Callable:defwrapper(*args,**kwargs):viz=_Viz(f,width)bound=inspect.signature(f).bind(*args,**kwargs)forname,vinbound.arguments.items():ifcallable(v):bound.arguments[name]=_record(viz.calls,v,name)args,kwargs=bound.args,bound.kwargsold=sys.gettrace()sys.settrace(viz.trace)try:result=f(*args,**kwargs)finally:sys.settrace(old)viz.finish(result)returnresultreturnwrapperreturndecorator(func)ifcallable(func)elsedecorator
Smallest x in [lo, hi] with ok(x) True; ok is False then True.
Returns hi + 1 when ok is False on the whole range.
Source code in pydsa/search.mu
148149150151152
deffirst_true(lo:int,hi:int,ok:int->bool)->int"""Smallest x in [lo, hi] with ok(x) True; ok is False then True. Returns hi + 1 when ok is False on the whole range."""lo+bisect_left(range(lo,hi+1),true,key=ok)
163164165166167
deffirst_true(lo:int,hi:int,ok:Callable[[int],bool])->int:"""Smallest x in [lo, hi] with ok(x) True; ok is False then True. Returns hi + 1 when ok is False on the whole range."""returnlo+bisect_left(range(lo,hi+1),True,key=ok)
Largest x in [lo, hi] with ok(x) True; ok is True then False.
Returns lo - 1 when ok is False on the whole range.
Source code in pydsa/search.mu
154155156157158
deflast_true(lo:int,hi:int,ok:int->bool)->int"""Largest x in [lo, hi] with ok(x) True; ok is True then False. Returns lo - 1 when ok is False on the whole range."""first_false(lo,hi,ok)-1
170171172173174
deflast_true(lo:int,hi:int,ok:Callable[[int],bool])->int:"""Largest x in [lo, hi] with ok(x) True; ok is True then False. Returns lo - 1 when ok is False on the whole range."""returnfirst_false(lo,hi,ok)-1
Smallest x in [lo, hi] with ok(x) False; ok is True then False.
Returns hi + 1 when ok is True on the whole range.
Source code in pydsa/search.mu
160161162163164
deffirst_false(lo:int,hi:int,ok:int->bool)->int"""Smallest x in [lo, hi] with ok(x) False; ok is True then False. Returns hi + 1 when ok is True on the whole range."""firstxinlo..hiifnotok(x)
177178179180181
deffirst_false(lo:int,hi:int,ok:Callable[[int],bool])->int:"""Smallest x in [lo, hi] with ok(x) False; ok is True then False. Returns hi + 1 when ok is True on the whole range."""returnlo+bisect_left(range(lo,hi+1),True,key=lambdax:notok(x))
Largest x in [lo, hi] with ok(x) False; ok is False then True.
Returns lo - 1 when ok is True on the whole range.
Source code in pydsa/search.mu
166167168169170
deflast_false(lo:int,hi:int,ok:int->bool)->int"""Largest x in [lo, hi] with ok(x) False; ok is False then True. Returns lo - 1 when ok is True on the whole range."""first_true(lo,hi,ok)-1
184185186187188
deflast_false(lo:int,hi:int,ok:Callable[[int],bool])->int:"""Largest x in [lo, hi] with ok(x) False; ok is False then True. Returns lo - 1 when ok is True on the whole range."""returnfirst_true(lo,hi,ok)-1
Fewest contiguous chunks of nums with every chunk summing to at most cap.
Greedy: extend the current chunk while it fits, cut when the next number
would push it past cap. Returns inf when a single number exceeds cap.
Source code in pydsa/search.mu
172173174175176177178179180181182183184
defmin_chunks(nums:[int],cap:int)->float"""Fewest contiguous chunks of nums with every chunk summing to at most cap. Greedy: extend the current chunk while it fits, cut when the next number would push it past cap. Returns inf when a single number exceeds cap."""retchunks,total=1,0forninnumsifn>capreturninfiftotal+n>capchunks,total=chunks+1,nelsetotal+=n
191192193194195196197198199200201202203204
defmin_chunks(nums:list[int],cap:int)->float:"""Fewest contiguous chunks of nums with every chunk summing to at most cap. Greedy: extend the current chunk while it fits, cut when the next number would push it past cap. Returns inf when a single number exceeds cap."""chunks,total=1,0forninnums:ifn>cap:returninfiftotal+n>cap:chunks,total=chunks+1,nelse:total+=nreturnchunks