Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import copy
- import json
- import numpy as np
- import random
- from numpngw import write_apng
- class Instrument:
- def __init__(self):
- self.context = {}
- self.events = []
- def under(self, ctx):
- self.context = {}
- def emit(self, event):
- self.events.append((self.context, copy(event)))
- def report(self):
- for el in self.dump():
- print(json.dumps(_ctx))
- def dump(self):
- for ctx, el in self.events:
- _ctx = copy(ctx)
- _ctx['event'] = el
- yield _ctx
- instrument = Instrument()
- def quickSort(alist):
- quickSortHelper(alist,0,len(alist)-1)
- def quickSortHelper(alist,first,last):
- if first<last:
- splitpoint = partition(alist,first,last)
- quickSortHelper(alist,first,splitpoint-1)
- quickSortHelper(alist,splitpoint+1,last)
- def partition(alist,first,last):
- pivotvalue = alist[first]
- leftmark = first+1
- rightmark = last
- done = False
- while not done:
- while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
- leftmark = leftmark + 1
- while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
- rightmark = rightmark -1
- if rightmark < leftmark:
- done = True
- else:
- temp = alist[leftmark]
- alist[leftmark] = alist[rightmark]
- alist[rightmark] = temp
- instrument.emit(alist)
- temp = alist[first]
- alist[first] = alist[rightmark]
- alist[rightmark] = temp
- instrument.emit(alist)
- return rightmark
- def gen_alist():
- A = 0
- B = 255
- COUNT = 255
- return random.sample(range(A,B+1),COUNT)
- def main():
- alist = gen_alist()
- instrument.under({'line': 1})
- instrument.emit(alist)
- quickSort(alist)
- seq = []
- for e in instrument.dump():
- a = np.zeros((100, 255), dtype=np.uint8)
- for index, alist_el in enumerate(e['event']):
- a[:, index] = alist_el
- seq.append(a)
- write_apng("demo.png", seq, delay=50, use_palette=True)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment