Implement Sort Dispatcher Method
Description
Implement the sort() method in the Sorter class (sort.py). This method acts as a dispatcher that delegates to the appropriate sorting algorithm based on the method parameter.
Requirements
Method Signature:
@staticmethod
def sort(data: List[Any], comparator: Callable[[Any, Any], bool], method: str = "merge") -> List[Any]
Parameters:
data: A list of elements to sort
comparator: A function that takes two elements and returns True if the first should come before the second
method: The sorting algorithm to use - "merge", "insertion", or "bubble" (default: "merge")
Returns:
- A new sorted list using the specified algorithm
Algorithm Details
This is a dispatcher/facade method that:
- Accepts a
method string parameter
- Normalises it to lowercase
- Calls the appropriate sorting method (
merge(), insertion(), or bubble())
- Raises
ValueError if an unknown method is provided
Implementation Guidelines
- Convert method to lowercase for case-insensitive matching
- Delegate to existing methods:
"merge" → call Sorter.merge()
"insertion" → call Sorter.insertion()
"bubble" → call Sorter.bubble()
- Raise ValueError with a helpful message for unknown methods
- Default to merge sort when no method is specified
- Handle edge cases:
- Empty method string (should raise ValueError)
- Mixed case method names (e.g.,
"MeRgE")
- Invalid method names
Testing
Run the test suite to verify your implementation:
python3 -m colorful_test test_sort.py
All tests related to sort() must pass, including:
- Correct dispatching to merge, insertion, and bubble sort
- Case-insensitive method names
- Default behaviour (merge sort)
- ValueError for invalid methods
- ValueError for empty string
Example Usage
from sort import Sorter
data = [5, 2, 8, 1, 9]
# Using different methods
sorted_merge = Sorter.sort(data, lambda a, b: a < b, "merge")
sorted_insertion = Sorter.sort(data, lambda a, b: a < b, "insertion")
sorted_bubble = Sorter.sort(data, lambda a, b: a < b, "bubble")
# Default (merge sort)
sorted_default = Sorter.sort(data, lambda a, b: a < b)
# Case insensitive
sorted_mixed = Sorter.sort(data, lambda a, b: a < b, "MERGE")
# All produce: [1, 2, 5, 8, 9]
Error Handling
# Should raise ValueError
try:
Sorter.sort(data, lambda a, b: a < b, "quicksort")
except ValueError as e:
print(e) # Unknown sort method 'quicksort'. Choose 'merge', 'insertion', or 'bubble'.
Acceptance Criteria
Dependencies
This method depends on:
Sorter.merge() - must be implemented first
Sorter.insertion() - must be implemented first
Sorter.bubble() - must be implemented first
Note: You may want to implement the individual sorting methods before tackling this dispatcher method.
Note: Do not modify the test files. Ensure your implementation passes all existing tests.
Implement Sort Dispatcher Method
Description
Implement the
sort()method in theSorterclass (sort.py). This method acts as a dispatcher that delegates to the appropriate sorting algorithm based on themethodparameter.Requirements
Method Signature:
Parameters:
data: A list of elements to sortcomparator: A function that takes two elements and returnsTrueif the first should come before the secondmethod: The sorting algorithm to use -"merge","insertion", or"bubble"(default:"merge")Returns:
Algorithm Details
This is a dispatcher/facade method that:
methodstring parametermerge(),insertion(), orbubble())ValueErrorif an unknown method is providedImplementation Guidelines
"merge"→ callSorter.merge()"insertion"→ callSorter.insertion()"bubble"→ callSorter.bubble()"MeRgE")Testing
Run the test suite to verify your implementation:
All tests related to
sort()must pass, including:Example Usage
Error Handling
Acceptance Criteria
Dependencies
This method depends on:
Sorter.merge()- must be implemented firstSorter.insertion()- must be implemented firstSorter.bubble()- must be implemented firstNote: You may want to implement the individual sorting methods before tackling this dispatcher method.
Note: Do not modify the test files. Ensure your implementation passes all existing tests.