-
Notifications
You must be signed in to change notification settings - Fork 0
/
missao4.py
34 lines (25 loc) · 855 Bytes
/
missao4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Missão4: Implementar o Algoritmo de Ordenação "Selection sort".
"""
import unittest
def selection_sort(data):
if not isinstance(data, (list, tuple)):
raise TypeError
for x1, n1 in enumerate(data):
tmp = (x1, n1)
for x2, n2 in enumerate(data[x1:], x1):
if n2 <= tmp[1]:
tmp = (x2, n2)
data[x1], data[tmp[0]] = tmp[1], n1
return data
class TestSelectionSort(unittest.TestCase):
def test_selection_sort(self):
self.assertRaises(TypeError, selection_sort, None)
self.assertEqual(selection_sort([]), [])
self.assertEqual(selection_sort([5]), [5])
data = [5, 1, 7, 2, 6, -3, 5, 7, -10]
self.assertEqual(selection_sort(data), sorted(data))
if __name__ == '__main__':
unittest.main()