Skip to content

Commit 26c38a4

Browse files
authored
Merge pull request #41 from lufftw/fix/solutions-to-pass-tests
2 parents 5b443a2 + ae3c902 commit 26c38a4

19 files changed

+1023
-367
lines changed

README.md

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -543,41 +543,53 @@ if __name__ == "__main__":
543543

544544
### 🚀 Multi-Solution Benchmarking
545545

546-
Compare multiple approaches for the same problem:
546+
Compare multiple approaches for the same problem using the **polymorphic pattern**:
547547

548548
```python
549549
# solutions/0023_merge_k_sorted_lists.py
550+
from _runner import get_solver
550551

551552
SOLUTIONS = {
552553
"default": {
553-
"method": "mergeKLists_heap",
554+
"class": "SolutionHeap",
555+
"method": "mergeKLists",
554556
"complexity": "O(N log k)",
555557
"description": "Min Heap approach"
556558
},
557559
"divide": {
558-
"method": "mergeKLists_divide",
560+
"class": "SolutionDivideConquer",
561+
"method": "mergeKLists",
559562
"complexity": "O(N log k)",
560563
"description": "Divide and Conquer"
561564
},
562565
"greedy": {
563-
"method": "mergeKLists_greedy",
566+
"class": "SolutionGreedy",
567+
"method": "mergeKLists",
564568
"complexity": "O(kN)",
565569
"description": "Greedy comparison"
566570
},
567571
}
568572

569-
class Solution:
570-
def mergeKLists_heap(self, lists):
573+
class SolutionHeap:
574+
def mergeKLists(self, lists):
571575
# Heap implementation
572576
pass
573-
574-
def mergeKLists_divide(self, lists):
577+
578+
class SolutionDivideConquer:
579+
def mergeKLists(self, lists):
575580
# Divide & Conquer implementation
576581
pass
577-
578-
def mergeKLists_greedy(self, lists):
582+
583+
class SolutionGreedy:
584+
def mergeKLists(self, lists):
579585
# Greedy implementation
580586
pass
587+
588+
def solve():
589+
# ... parse input ...
590+
solver = get_solver(SOLUTIONS)
591+
result = solver.mergeKLists(lists)
592+
print(result)
581593
```
582594

583595
**Run commands:**
@@ -604,36 +616,7 @@ greedy 44.82ms O(kN) 3/3
604616
============================================================
605617
```
606618

607-
<details>
608-
<summary><strong>Advanced: Wrapper Pattern for Multiple Classes</strong></summary>
609-
610-
When you need separate classes with the same method name:
611-
612-
```python
613-
class SolutionRecursive:
614-
def reverseKGroup(self, head, k):
615-
pass # Recursive implementation
616-
617-
class SolutionIterative:
618-
def reverseKGroup(self, head, k):
619-
pass # Iterative implementation
620-
621-
# Wrapper functions
622-
def solve_recursive(head, k):
623-
return SolutionRecursive().reverseKGroup(head, k)
624-
625-
def solve_iterative(head, k):
626-
return SolutionIterative().reverseKGroup(head, k)
627-
628-
SOLUTIONS = {
629-
"default": {"method": "solve_iterative", ...},
630-
"recursive": {"method": "solve_recursive", ...},
631-
}
632-
```
633-
634-
Create with template: `new_problem.bat 0025_reverse_nodes --wrapper`
635-
636-
</details>
619+
Create with template: `new_problem.bat 0023_merge_k_lists --multi`
637620

638621
### 🔀 Flexible Output Validation
639622

@@ -784,8 +767,7 @@ neetcode/
784767
785768
├── templates/ # 📄 Problem templates
786769
│ ├── template_solution.py # Single solution
787-
│ ├── template_solution_multi.py # Multi-solution (one class)
788-
│ └── template_solution_wrapper.py # Multi-solution (wrapper pattern)
770+
│ └── template_solution_multi.py # Multi-solution (polymorphic)
789771
790772
├── .vscode/ # 🔧 VS Code integration
791773
│ ├── settings.json # Python environment settings

README_zh-TW.md

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -427,41 +427,53 @@ if __name__ == "__main__":
427427

428428
### 🚀 多解法效能比較
429429

430-
比較同一題目的多種解法:
430+
使用**多型模式**比較同一題目的多種解法:
431431

432432
```python
433433
# solutions/0023_merge_k_sorted_lists.py
434+
from _runner import get_solver
434435

435436
SOLUTIONS = {
436437
"default": {
437-
"method": "mergeKLists_heap",
438+
"class": "SolutionHeap",
439+
"method": "mergeKLists",
438440
"complexity": "O(N log k)",
439441
"description": "最小堆方法"
440442
},
441443
"divide": {
442-
"method": "mergeKLists_divide",
444+
"class": "SolutionDivideConquer",
445+
"method": "mergeKLists",
443446
"complexity": "O(N log k)",
444447
"description": "分治法"
445448
},
446449
"greedy": {
447-
"method": "mergeKLists_greedy",
450+
"class": "SolutionGreedy",
451+
"method": "mergeKLists",
448452
"complexity": "O(kN)",
449453
"description": "貪婪比較"
450454
},
451455
}
452456

453-
class Solution:
454-
def mergeKLists_heap(self, lists):
457+
class SolutionHeap:
458+
def mergeKLists(self, lists):
455459
# 堆實作
456460
pass
457-
458-
def mergeKLists_divide(self, lists):
461+
462+
class SolutionDivideConquer:
463+
def mergeKLists(self, lists):
459464
# 分治實作
460465
pass
461-
462-
def mergeKLists_greedy(self, lists):
466+
467+
class SolutionGreedy:
468+
def mergeKLists(self, lists):
463469
# 貪婪實作
464470
pass
471+
472+
def solve():
473+
# ... 解析輸入 ...
474+
solver = get_solver(SOLUTIONS)
475+
result = solver.mergeKLists(lists)
476+
print(result)
465477
```
466478

467479
**執行指令:**
@@ -488,36 +500,7 @@ greedy 44.82ms O(kN) 3/3
488500
============================================================
489501
```
490502

491-
<details>
492-
<summary><strong>進階:Wrapper 模式(多個類別使用相同方法名)</strong></summary>
493-
494-
當需要多個類別使用相同方法名稱時:
495-
496-
```python
497-
class SolutionRecursive:
498-
def reverseKGroup(self, head, k):
499-
pass # 遞迴實作
500-
501-
class SolutionIterative:
502-
def reverseKGroup(self, head, k):
503-
pass # 迭代實作
504-
505-
# Wrapper 函式
506-
def solve_recursive(head, k):
507-
return SolutionRecursive().reverseKGroup(head, k)
508-
509-
def solve_iterative(head, k):
510-
return SolutionIterative().reverseKGroup(head, k)
511-
512-
SOLUTIONS = {
513-
"default": {"method": "solve_iterative", ...},
514-
"recursive": {"method": "solve_recursive", ...},
515-
}
516-
```
517-
518-
使用模板建立:`new_problem.bat 0025_reverse_nodes --wrapper`
519-
520-
</details>
503+
使用模板建立:`new_problem.bat 0023_merge_k_lists --multi`
521504

522505
### 🔀 彈性輸出驗證
523506

@@ -668,8 +651,7 @@ neetcode/
668651
669652
├── templates/ # 📄 題目模板
670653
│ ├── template_solution.py # 單一解法
671-
│ ├── template_solution_multi.py # 多解法(單一類別)
672-
│ └── template_solution_wrapper.py # 多解法(Wrapper 模式)
654+
│ └── template_solution_multi.py # 多解法(多型模式)
673655
674656
├── .vscode/ # 🔧 VS Code 整合
675657
│ ├── settings.json # Python 環境設定

docs/ARCHITECTURE_MIGRATION.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ SOLUTIONS = {
410410
| Phase 1 | Runner infrastructure | ✅ Completed |
411411
| Phase 2 | Example migrations | ✅ Completed |
412412
| Phase 3 | Full migration | Pending (scan remaining solutions) |
413-
| Phase 4 | Documentation | ✅ Completed |
413+
| Phase 4 | Documentation & Templates | ✅ Completed |
414414

415415
### Completed Items
416416

@@ -419,5 +419,12 @@ SOLUTIONS = {
419419
- `0027_remove_element.py` - Polymorphic pattern with `SolutionTwoPointers`, `SolutionTwoEnds`
420420
- `0025_reverse_nodes_in_k_group.py` - Polymorphic pattern with `SolutionIterative`, `SolutionRecursive`
421421
- `0023_merge_k_sorted_lists.py` - Split into `SolutionHeap`, `SolutionDivideConquer`, `SolutionGreedy`
422-
- **Phase 4**: Updated test fixtures in `.dev/tests/` to use polymorphic format
422+
- **Phase 4**:
423+
- Updated test fixtures in `.dev/tests/` to use polymorphic format
424+
- Updated `template_solution.py` to use `SOLUTIONS` + `get_solver()`
425+
- Updated `template_solution_multi.py` to use polymorphic pattern
426+
- Deleted deprecated `template_solution_wrapper.py`
427+
- Updated `new_problem.bat` and `new_problem.sh` (removed `--wrapper` option)
428+
- Updated `run_tests.bat` to pass all arguments
429+
- Created `docs/solution_contract.md` as canonical specification
423430

0 commit comments

Comments
 (0)