Repository navigation
Expand file tree
/
Copy pathtest_Selection.py
More file actions
executable file
·79 lines (68 loc) · 2.92 KB
/
Copy pathtest_Selection.py
File metadata and controls
executable file
·79 lines (68 loc) · 2.92 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
"""Test Selection Sort"""
import sys
from AlgsSedgewickWayne.Selection import Sort
from AlgsSedgewickWayne.testcode.ArrayHistory import chk
from AlgsSedgewickWayne.testcode.ArrayHistory import ArrayHistory
from AlgsSedgewickWayne.testcode.InputArgs import cli_get_array
def test_wk2_lec(prt=sys.stdout):
"""Example from week 2 lecture, "Selection Sort (6:59)" """
# Give the array that results after the first 4 exchanges when
# selection sorting the following array:
arr = [int(i) for i in "7 10 5 3 8 4 2 9 6".split()]
run(arr, 'SELECTION SORT', prt=prt)
def test_wk2_ex_selections_489125(prt=sys.stdout):
"""What results after the first 4 exchanges when selection sorting the given array"""
# (seed = 183182)
# Give the array that results after the first 4 exchanges when
# selection sorting the following array:
arr = [int(i) for i in "13 16 40 60 19 70 71 47 12 67".split()]
run(arr, 'SELECTION SORT', prt=prt)
def test_wk2_q3a(prt=sys.stdout):
"""QUESTION: Any pair of items is compared no more than once during selection sort"""
# QUESTION: Any pair of items is compared no more than once during selection sort.
# ANSWER(FALSE): Consider the array { 2, 1, 0 }. Then, 2 and 1 are compared twice.
run([2, 1, 0], 'SELECTION SORT', prt=prt)
def test_wk2_q3b(prt=sys.stdout):
"""QUESTION: An exchange in selection sort can decrease the number of inversions by 2+"""
# QUESTION: An exchange in selection sort can decrease the number of inversions
# by two (or more).
# ANSWER(TRUE): Consider the array { 3, 2, 1 }, which has 3 inversions.
# The first exchange results in the array { 1, 2, 3 }, which has zero inversions.
run([3, 2, 1], 'SELECTION SORT', prt=prt)
def test_wk2_q2a(prt=sys.stdout):
"""Test selection sort on words"""
desc = 'SELECTION SORT WORDS'
prt.write(f"\n{desc}\n")
exp = "BECK BUSH DEVO EVE6 HOLE JAYZ KORN MIMS VAIN RATT TOTO PINK SADE NOFX SOAD WHAM"
arr = "HOLE BUSH MIMS BECK WHAM SOAD NOFX TOTO VAIN RATT DEVO PINK SADE KORN JAYZ EVE6".split()
ahistobj = ArrayHistory()
Sort(arr, array_history=ahistobj)
ahistobj.show(desc)
for idx, arrhist in enumerate(ahistobj):
if chk( arrhist[0], exp ):
prt.write(f"MATCH {idx}\n")
def run(arr, desc=None, prt=sys.stdout):
"""Run Selection sort"""
ahistobj = ArrayHistory()
Sort(arr, array_history=ahistobj)
if desc is None:
desc = "SELECTION SORT"
prt.write(f"{desc} RESULT {' '.join(str(e) for e in arr)}\n")
ahistobj.prt()
ahistobj.show(desc)
def run_all():
"""Run all tests."""
test_wk2_lec()
test_wk2_ex_selections_489125()
test_wk2_q3a()
test_wk2_q2a()
def cli():
"""Command line interface for variations of this test"""
len_array = len(sys.argv)
if len_array == 1:
run_all()
elif len_array == 2:
run(cli_get_array())
if __name__ == '__main__':
cli()