Multiplications
python
def multiply(tab: list, k: int) -> list:
"""
Multiplies each element in the list by k.
Input: list and integrer
Output: list
// Doctests generated by Github Copilot • Claude Sonnet 4
>>> multiply([1, 2, 3, 4], 2)
[2, 4, 6, 8]
>>> multiply([5, 10, 15], 3)
[15, 30, 45]
>>> multiply([1, 2, 3], 0)
[0, 0, 0]
>>> multiply([2, -3, 4], -1)
[-2, 3, -4]
>>> multiply([], 5)
[]
>>> multiply([7], 10)
[70]
>>> multiply([1.5, 2.5], 4)
[6.0, 10.0]
>>> multiply([-1, -2, -3], -2)
[2, 4, 6]
"""
# This operation is only run once
output = []
for e in tab:
# Here, the operation is reapeted len(tab) times
output.append(e * k)
# For a final cost of n + 1 = O(n)
return output
if __name__ == "__main__":
import doctest
doctest.testmod()Selection
python
def selectionSort(t: list) -> list:
"""
The one and only. Selection Sort
Input: lists
Output: list but sorted
// Doctests generated by Github Copilot • Claude Sonnet 4
>>> selectionSort([64, 34, 25, 12, 22, 11, 90])
[11, 12, 22, 25, 34, 64, 90]
>>> selectionSort([5, 2, 4, 6, 1, 3])
[1, 2, 3, 4, 5, 6]
>>> selectionSort([1])
[1]
>>> selectionSort([])
[]
>>> selectionSort([3, 3, 3, 3])
[3, 3, 3, 3]
>>> selectionSort([5, 4, 3, 2, 1])
[1, 2, 3, 4, 5]
>>> selectionSort([1, 2, 3, 4, 5])
[1, 2, 3, 4, 5]
>>> selectionSort([-5, -1, -10, 0, 3])
[-10, -5, -1, 0, 3]
"""
tab = list(t)
for i in range(len(tab) - 1):
mini = i
for j in range(i+1, len(tab)):
if tab[mini] > tab[j]:
mini = j
tab[mini], tab[i] = tab[i], tab[mini]
return tab
if __name__ == "__main__":
import doctest
doctest.testmod()