Skip to content

Commit 594523b

Browse files
committed
Fix indexing and empty search results bugs in Pandas and React implementations
1 parent 068cf43 commit 594523b

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

SQL/Leetcode/Intermediate Select/1204. Last Person to Fit in the Bus/Claude Sonnet 4.6 Extended/Last_Person_to_Fit_in_the_Bus_Pandas.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ def last_passenger(queue: pd.DataFrame) -> pd.DataFrame:
4646

4747
# Step3: 条件を満たす最後の行(最大 turn)を idxmax で取得
4848
# cum_w は turn 昇順なので、最後の True のインデックス = 答え
49-
last_idx = mask[mask].index[-1] # O(N)
49+
valid_mask = mask[mask]
50+
if valid_mask.empty:
51+
return pd.DataFrame({'person_name': []})
52+
last_idx = valid_mask.index[-1] # O(N)
5053

5154
# Step4: 仕様列のみ返却
5255
return pd.DataFrame({'person_name': [queue.at[last_idx, 'person_name']]})
@@ -235,6 +238,9 @@ def last_passenger(queue: pd.DataFrame) -> pd.DataFrame:
235238
# side='right': 1000 より大きくなる最初の位置を返す → -1 で最後の有効位置
236239
last_pos = np.searchsorted(cum_w, 1000, side='right') - 1
237240

241+
if last_pos < 0:
242+
return pd.DataFrame({'person_name': []})
243+
238244
return pd.DataFrame(
239245
{'person_name': [names[order[last_pos]]]}
240246
)
@@ -277,6 +283,9 @@ def last_passenger(queue: pd.DataFrame) -> pd.DataFrame:
277283
cum_w = weights_sorted.cumsum() # O(N)
278284
last_pos = np.searchsorted(cum_w, 1000, side='right') - 1
279285

286+
if last_pos < 0:
287+
return pd.DataFrame({'person_name': []})
288+
280289
return pd.DataFrame(
281290
{'person_name': [names_sorted[last_pos]]}
282291
)

SQL/Leetcode/Intermediate Select/1204. Last Person to Fit in the Bus/Claude Sonnet 4.6 Extended/Last_Person_to_Fit_in_the_Bus_React.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ <h3 class="outfit font-bold text-amber-800 mb-2 text-base">
430430
# side='right': 1000 より大きくなる最初の位置 → -1 で最後の有効位置
431431
last_pos = np.searchsorted(cum_w, 1000, side='right') - 1
432432

433+
if last_pos < 0:
434+
return pd.DataFrame({'person_name': []})
435+
433436
return pd.DataFrame({'person_name': [names_sorted[last_pos]]})
434437

435438

@@ -1363,6 +1366,11 @@ <h3 class="outfit font-bold text-slate-800 mb-3 text-base">
13631366
}
13641367

13651368
function CumsumVisual({ data }) {
1369+
const lastIndex = data.cumvals.reduce(
1370+
(lastIdx, v, i) => (v <= data.limit ? i : lastIdx),
1371+
-1,
1372+
);
1373+
13661374
return (
13671375
<div className="mt-4">
13681376
<div className="text-xs font-bold text-slate-500 mb-2">{data.label}</div>
@@ -1375,7 +1383,7 @@ <h3 class="outfit font-bold text-slate-800 mb-3 text-base">
13751383
className={[
13761384
'flex flex-col items-center rounded-xl p-2 border-2 transition',
13771385
ok
1378-
? i === 2
1386+
? i === lastIndex
13791387
? 'bg-emerald-200 border-emerald-500'
13801388
: 'bg-emerald-50 border-emerald-300'
13811389
: 'bg-red-50 border-red-300',

public/SQL/Leetcode/Intermediate Select/1204. Last Person to Fit in the Bus/Claude Sonnet 4.6 Extended/Last_Person_to_Fit_in_the_Bus_React.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ <h3 class="outfit font-bold text-amber-800 mb-2 text-base">
430430
# side='right': 1000 より大きくなる最初の位置 → -1 で最後の有効位置
431431
last_pos = np.searchsorted(cum_w, 1000, side='right') - 1
432432

433+
if last_pos < 0:
434+
return pd.DataFrame({'person_name': []})
435+
433436
return pd.DataFrame({'person_name': [names_sorted[last_pos]]})
434437

435438

@@ -1363,6 +1366,11 @@ <h3 class="outfit font-bold text-slate-800 mb-3 text-base">
13631366
}
13641367

13651368
function CumsumVisual({ data }) {
1369+
const lastIndex = data.cumvals.reduce(
1370+
(lastIdx, v, i) => (v <= data.limit ? i : lastIdx),
1371+
-1,
1372+
);
1373+
13661374
return (
13671375
<div className="mt-4">
13681376
<div className="text-xs font-bold text-slate-500 mb-2">{data.label}</div>
@@ -1375,7 +1383,7 @@ <h3 class="outfit font-bold text-slate-800 mb-3 text-base">
13751383
className={[
13761384
'flex flex-col items-center rounded-xl p-2 border-2 transition',
13771385
ok
1378-
? i === 2
1386+
? i === lastIndex
13791387
? 'bg-emerald-200 border-emerald-500'
13801388
: 'bg-emerald-50 border-emerald-300'
13811389
: 'bg-red-50 border-red-300',

0 commit comments

Comments
 (0)