Fun Random Selection of Students in the Class
Which pupil should go to the board?xxxxxxxxxx
import numpy as np, time
def random_coords(class_list):
idx = randint(1, len(class_list))
n = ceil(sqrt(len(class_list)))
y = (idx - 1) % n
x = (idx - 1) // n
return x, y, idx
def maplot(arr, st, n):
m = matrix(arr.tolist())
m.subdivide(list(range(1, n)), list(range(1, n)))
mp = matrix_plot(
m, figsize=(n, n), cmap='bone', frame=False, title=st,
subdivisions=True, subdivision_style=dict(color='steelblue'))
return mp
def class_id(idx, class_list):
if 0 < idx < len(class_list) + 1:
return str((idx, class_list[idx - 1]))
else:
return str((idx, 'unknown'))
class_list = ["Emily Johnson", "James Smith", "Hannah Brown", "William Davis",
"Olivia Wilson", "Benjamin Taylor", "Ava Martinez",
"Ethan Garcia", "Sophia Rodriguez", "Daniel Hernandez",
"Mia Lee", "Alexander Perez", "Chloe Jackson",
"Samuel Martin", "Isabella Anderson", "Jacob Thompson",
"Charlotte White", "Michael Clark", "Grace Lewis",
"Joseph Wright", "Lily Robinson", "David Baker"]
n, c = ceil(sqrt(len(class_list))), 0
targets = list(range(1, len(class_list) + 1))
arr = np.array(n * [n * [.1]])
mps = [maplot(arr, class_id(0, class_list), n)]
for idx in range(len(class_list) + 1, n ** 2 + 1) :
arr[(idx - 1) // n][(idx - 1) % n] = .3
mps += [maplot(arr, class_id(idx, class_list), n)]
while len(targets) > 1:
x, y, idx = random_coords(class_list)
arr[x][y] = .3
if idx in targets:
targets.remove(idx)
mps += [maplot(arr, class_id(idx, class_list), n)]
c += 1
animate(mps).show(delay=100)
time.sleep(c + 30)
pretty_print(html('%d random (x,y)' % c))
pretty_print(html(class_list[targets[0] - 1]))