Skip to content

Instantly share code, notes, and snippets.

@andrewboyley
Last active November 16, 2019 16:24
Show Gist options
  • Save andrewboyley/6371d64ecf9d13d60308c4ef0a210fcc to your computer and use it in GitHub Desktop.
Save andrewboyley/6371d64ecf9d13d60308c4ef0a210fcc to your computer and use it in GitHub Desktop.
A simple bogosort implementation that keeps track of elapsed time and allow variable array size to be sorted. Use --help to view the usage.
"""
__ __
/ /_ ____ ____ _____ _________ _____/ /_
/ __ \/ __ \/ __ `/ __ \/ ___/ __ \/ ___/ __/
/ /_/ / /_/ / /_/ / /_/ (__ ) /_/ / / / /_
/_.___/\____/\__, /\____/____/\____/_/ \__/
/____/
by Andrew Boyley 2019
"""
import datetime
import argparse
import random
import curses
import time
class Timer(object): # https://gist.github.com/igniteflow/1253276
"""A simple timer class"""
def __init__(self):
pass
def start(self):
"""Starts the timer"""
self.start = datetime.datetime.now()
return self.start
def stop(self, message="Total: "):
"""Stops the timer. Returns the time elapsed"""
self.stop = datetime.datetime.now()
return message + str(self.stop - self.start)
def elapsed(self, message="Elapsed: "):
"""Time elapsed since start was called"""
return message + str(datetime.datetime.now() - self.start)
def bar_graph(arr, length):
max_digits = len(str(length-1))
spacing = max_digits + 1
for row in range(length, 0, -1):
line = ''
for col in range(length):
if arr[col] <= row - 1:
line += ' ' + ' '*(spacing-1)
else:
line += '#' + ' '*(spacing-1)
stdscr.addstr(length-row, 0, line + '\n')
stdscr.addstr(length, 0, '-'*spacing*length + '\n')
line = ''
for col in range(length):
line += str(arr[col]) + ' '*(spacing-len(str(arr[col])))
stdscr.addstr(length+1, 0, line + '\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="""\033[0;32m
__ __
/ /_ ____ ____ _____ _________ _____/ /_
/ __ \/ __ \/ __ `/ __ \/ ___/ __ \/ ___/ __/
/ /_/ / /_/ / /_/ / /_/ (__ ) /_/ / / / /_
/_.___/\____/\__, /\____/____/\____/_/ \__/
/____/
\033[0m""")
parser.add_argument('-s', '--size', default=10,
help='The size of the array to be sorted', type=int)
parser.add_argument('-i', '--interval', default=0,
help='Interval between printing the bar graph (seconds)', type=float)
args = parser.parse_args()
sorted_arr = list(range(args.size))
arr = sorted_arr[:]
random.shuffle(arr)
timer = Timer()
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
try:
bar_graph(arr, args.size)
timer.start()
last = time.time()
while arr != sorted_arr:
stdscr.addstr(args.size+3, 0, timer.elapsed())
stdscr.refresh()
random.shuffle(arr)
current = time.time()
if current - last > args.interval:
last = current
bar_graph(arr, args.size)
bar_graph(arr, args.size)
stdscr.refresh()
finally:
stdscr.addstr(args.size + 3, 0, timer.stop())
stdscr.refresh()
curses.echo()
curses.nocbreak()
input("\nPress ENTER to continue...")
curses.endwin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment