-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_leds.py
223 lines (202 loc) · 6.85 KB
/
sort_leds.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
# ========================================================================
# sort_leds.py --algorithm bubble --palette wopr
#
# Description: Generates a random list of 8 values (1-8) representing
# colors and sorts them using the specified algorithm. Each
# step of the sorting process is displayed on the LED
# matrix, and colors are printed to the console.
#
# pip3 install termcolor
#
# Author: Jim Ing
# Date: 2025-01-16
# ========================================================================
import argparse
import random
import time
from termcolor import colored
from config import sense
sense.clear()
# Color palettes
PALETTES = {
"default": {
"names": ["Gry", "Red", "Grn", "Yel", "Blu", "Mag", "Cyn", "Wht"],
"colors": [
(102, 102, 102), # Grey
(255, 0, 0), # Red
(0, 255, 0), # Green
(255, 255, 0), # Yellow
(0, 0, 255), # Blue
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
(255, 255, 255) # White
],
"termcodes": {
"Gry": "dark_grey",
"Red": "red",
"Grn": "green",
"Yel": "yellow",
"Blu": "blue",
"Mag": "magenta",
"Cyn": "cyan",
"Wht": "white"
}
},
"wopr": {
"names": ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8"],
"colors": [
(127, 0, 0), # Dark Red
(255, 0, 0), # Red
(255, 127, 0), # Orange
(255, 127, 127), # Light Red
(255, 255, 0), # Yellow
(255, 255, 127), # Pale Yellow
(127, 127, 0), # Olive Green
(0, 127, 0) # Dark Green
],
"termcodes": {
"C1": "red",
"C2": "light_red",
"C3": "light_grey",
"C4": "white",
"C5": "yellow",
"C6": "light_yellow",
"C7": "green",
"C8": "light_green"
}
}
}
def partition(values, low, high):
"""
Partition is a helper function used in the Quick Sort algorithm.
Its purpose is to rearrange elements in a sublist so that all elements
less than a chosen pivot value come before the pivot, and all elements
greater than or equal to the pivot come after it.
"""
pivot = values[high]
i = low - 1
moves = 0
for j in range(low, high):
if values[j] < pivot:
i += 1
values[i], values[j] = values[j], values[i]
moves += 1
values[i + 1], values[high] = values[high], values[i + 1]
moves += 1
return i + 1, moves
def bubble_sort(values):
"""Bubble Sort Algorithm."""
moves = 0
for i in range(len(values) - 1):
for j in range(len(values) - 1 - i):
if values[j] > values[j + 1]:
values[j], values[j + 1] = values[j + 1], values[j]
moves += 1
yield values, moves
def selection_sort(values):
"""Selection Sort Algorithm."""
moves = 0
for i in range(len(values)):
min_idx = i
for j in range(i + 1, len(values)):
if values[j] < values[min_idx]:
min_idx = j
values[i], values[min_idx] = values[min_idx], values[i]
moves += 1
yield values, moves
def insertion_sort(values):
"""Insertion Sort Algorithm."""
moves = 0
for i in range(1, len(values)):
key = values[i]
j = i - 1
while j >= 0 and key < values[j]:
values[j + 1] = values[j]
j -= 1
moves += 1
yield values, moves
values[j + 1] = key
yield values, moves
def quick_sort(values, low=0, high=None, moves=[0]):
"""Quick Sort Algorithm."""
if high is None:
high = len(values) - 1
if low < high:
pivot, m = partition(values, low, high)
moves[0] += m
yield values, moves[0]
yield from quick_sort(values, low, pivot - 1, moves)
yield from quick_sort(values, pivot + 1, high, moves)
def cocktail_shaker_sort(values):
"""Cocktail Shaker Sort Algorithm."""
moves = 0
n = len(values)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False
for i in range(start, end):
if values[i] > values[i + 1]:
values[i], values[i + 1] = values[i + 1], values[i]
swapped = True
moves += 1
yield values, moves
end -= 1
for i in range(end, start, -1):
if values[i] < values[i - 1]:
values[i], values[i - 1] = values[i - 1], values[i]
swapped = True
moves += 1
yield values, moves
start += 1
def draw_bars(matrix):
"""Draws the entire matrix on the Sense HAT LED display."""
sense.clear()
for y in range(8):
for x in range(8):
sense.set_pixel(x, y, matrix[y][x])
time.sleep(0.5)
def display_colors(label, values, palette):
"""Displays colors as colored circles in the console."""
print(f"{label:03}:", end=" ")
for v in values:
print(colored("●", palette["termcodes"][palette["names"][v - 1]]), end=" ")
print()
def sorting_visualizer(algorithm, palette_name):
"""Runs a visual sorting demonstration on the 8x8 LED matrix."""
palette = PALETTES[palette_name]
while True:
print(f"{algorithm.capitalize()} sort:")
values = [random.randint(1, 8) for _ in range(8)]
matrix = [[(0, 0, 0) for _ in range(8)] for _ in range(8)]
# Initialize first row with randomly selected colors
for x in range(8):
matrix[0][x] = palette["colors"][values[x] - 1]
draw_bars(matrix)
display_colors(0, values, palette)
row = 1
sort_function = globals()[algorithm + "_sort"]
for sorted_values, moves in sort_function(values):
if row % 8 == 0:
sense.clear()
matrix = [[(0, 0, 0) for _ in range(8)] for _ in range(8)]
for x in range(8):
matrix[row % 8][x] = palette["colors"][sorted_values[x] - 1]
draw_bars(matrix)
display_colors(row, sorted_values, palette)
row += 1
draw_bars(matrix)
print()
time.sleep(20)
sense.clear()
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument("--algorithm", choices=["bubble", "selection", "insertion", "quick", "cocktail_shaker"], default="bubble")
parser.add_argument("--palette", choices=PALETTES.keys(), default="default", help="Choose a color palette (default or wopr).")
args = parser.parse_args()
sorting_visualizer(args.algorithm, args.palette)
except KeyboardInterrupt:
sense.clear()