Integer & Pranker
Hey Integer, how about we build a function that looks super clean—O(n) sorting, all that— but whenever it runs it spawns a little confetti party on your screen? Your precision, my prank—let's make it happen!
Sounds like a neat test of performance and aesthetics. I’ll write a quick O(n log n) sort, but I’ll keep the logic linear by using a radix sort for integers. Then after each swap I’ll send a command to a tiny canvas that draws a burst of colored dots. That way the algorithm stays efficient and the confetti stays lightweight. I’ll just make sure the console doesn’t get flooded with logs, otherwise the timing will suffer. Ready to dive in?
Absolutely, let’s see that linear‑logic fireworks! Just make sure the canvas stays a tiny dot‑size, or we’ll end up with a full‑blown disco floor. Let’s keep it light and let the code sparkle!
import sys
import random
import time
# tiny canvas helper
class Canvas:
def __init__(self, width=40, height=20):
self.w = width
self.h = height
self.clear()
def clear(self):
self.pixels = [[' ']*self.w for _ in range(self.h)]
def draw(self, x, y, char='*'):
if 0 <= x < self.w and 0 <= y < self.h:
self.pixels[y][x] = char
def render(self):
sys.stdout.write('\x1b[H')
for row in self.pixels:
sys.stdout.write(''.join(row)+'\n')
sys.stdout.flush()
def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
counting_sort(arr, exp)
exp *= 10
def counting_sort(arr, exp):
n = len(arr)
output = [0]*n
count = [0]*10
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1
for i in range(1,10):
count[i] += count[i-1]
for i in range(n-1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index]-1] = arr[i]
count[index] -= 1
# tiny confetti burst at each placement
confetti(index, i, arr[i])
for i in range(n):
arr[i] = output[i]
def confetti(val, pos, number):
# map number to canvas coordinates
x = (pos + val) % canvas.w
y = (number + val) % canvas.h
canvas.draw(x, y, '*')
def main():
global canvas
canvas = Canvas()
sys.stdout.write('\x1b[?25l\x1b[2J')
arr = [random.randint(0, 9999) for _ in range(100)]
start = time.time()
radix_sort(arr)
elapsed = time.time() - start
canvas.render()
print(f'Sorted {len(arr)} numbers in {elapsed:.3f}s')
sys.stdout.write('\x1b[?25h')
if __name__ == "__main__":
main()
Nice code! Watch the confetti dance while those numbers line up—pure prank‑power meets algorithmic precision!
Glad you liked the combo of clean code and a little sparkle—pure efficiency with a bit of visual flair.
Love the sparkle, it makes sorting feel like a tiny fireworks show—who knew clean code could be so flashy?
That’s the perfect proof that an algorithm can still be fun without losing its clarity.
Yep, even a tidy radix sort can throw a confetti party—who said efficiency can’t be a bit mischievous?
Exactly—efficiency can be elegant and a little mischievous at the same time.