fix(curriculum): extend selection sort algorithm tests (#64523)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Krzysztof G.
2025-12-17 13:29:02 +01:00
committed by GitHub
parent 2b0ff8f657
commit 1e4f6f0b8c

View File

@@ -20,6 +20,8 @@ Selection sort results in a quadratic time complexity in the best, average, and
1. You should define a function named `selection_sort`. 1. You should define a function named `selection_sort`.
1. Your `selection_sort` function should have one parameter that represents the list of items. 1. Your `selection_sort` function should have one parameter that represents the list of items.
1. Your `selection_sort` function should take a list and sort the items in place from smallest to largest. 1. Your `selection_sort` function should take a list and sort the items in place from smallest to largest.
1. Your `selection_sort` function should modify the input list in-place, and return it once it's sorted.
1. Your `selection_sort` function should follow the selection sort algorithm, swapping the smallest element from the unsorted portion of the list, with the first unsorted element.
1. Your `selection_sort` function should not use either the built-in `sort()` method or `sorted()` function. 1. Your `selection_sort` function should not use either the built-in `sort()` method or `sorted()` function.
# --hints-- # --hints--
@@ -61,6 +63,111 @@ You should not use the built-in `sort()` method or `sorted()` function in your c
) )
``` ```
Your `selection_sort` should return the same list as the input list.
```js
(
{
test: () => runPython(`
input_list = [32, 0, 88, 1, 203]
assert selection_sort(input_list) is input_list
`)
}
)
```
Your `selection_sort` should modify the input list in-place. You should not use any method adding, or removing items from the list.
```js
(
{
test: () => runPython(`
from collections import UserList
class CustomList(UserList):
def __init__(self, *args, **kwargs):
self._record = []
super().__init__(*args, **kwargs)
def __setitem__(self, key, value):
self._record.append(f'Setting item {key} to {value}. Previous value: {self[key]}')
super().__setitem__(key, value)
def append(self, *args, **kwargs):
self._record.append(f'Appending to list: {args}, {kwargs}')
super().append(*args, **kwargs)
def insert(self, *args, **kwargs):
self._record.append(f'Inserting to list: {args}, {kwargs}')
super().insert(*args, **kwargs)
def pop(self, *args, **kwargs):
self._record.append(f'Popping from list: {args}, {kwargs}')
super().pop(*args, **kwargs)
def remove(self, *args, **kwargs):
self._record.append(f'Removing from list: {args}, {kwargs}')
super().remove(*args, **kwargs)
list_to_sort = CustomList([32, 0, 88, 1, 203])
selection_sort(list_to_sort)
assert all(
'Setting item' in record
for record in list_to_sort._record
)
`)
}
)
```
Your `selection_sort` should follow the selection sort algorithm, swapping the minimum value in unsorted part of the list with first the unsorted element.
```js
(
{
test: () => runPython(`
from collections import UserList
class CustomList(UserList):
def __init__(self, *args, **kwargs):
self._record = []
super().__init__(*args, **kwargs)
def __setitem__(self, key, value):
self._record.append(f'Setting item {key} to {value}. Previous value: {self[key]}')
super().__setitem__(key, value)
list_to_sort = CustomList([33, 1, 89, 2, 67, 245])
swap_pairs = [
((0, 1, 33), (1, 33, 1)),
((1, 2, 33), (3, 33, 2)),
((2, 33, 89), (3, 89, 33)),
((3, 67, 89), (4, 89, 67)),
]
selection_sort(list_to_sort)
record = list_to_sort._record
actual_pairs = [
(record[offset * 2], record[offset * 2 + 1])
for offset, _ in enumerate(record[::2])
]
assert len(swap_pairs) == len(actual_pairs)
for expected_pair, actual_pair in zip(swap_pairs, actual_pairs):
assert (
set(f'Setting item {key} to {value}. Previous value: {old}' for key, value, old in expected_pair)
== set(actual_pair)
)
`)
}
)
```
`selection_sort([33, 1, 89, 2, 67, 245])` should return `[1, 2, 33, 89, 67, 245]`. `selection_sort([33, 1, 89, 2, 67, 245])` should return `[1, 2, 33, 89, 67, 245]`.
```js ```js