Guest User

Untitled

a guest
May 27th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. from copy import copy
  2. import json
  3. import numpy as np
  4. import random
  5. from numpngw import write_apng
  6.  
  7. class Instrument:
  8.  
  9. def __init__(self):
  10. self.context = {}
  11. self.events = []
  12.  
  13. def under(self, ctx):
  14. self.context = {}
  15.  
  16. def emit(self, event):
  17. self.events.append((self.context, copy(event)))
  18.  
  19. def report(self):
  20. for el in self.dump():
  21. print(json.dumps(_ctx))
  22.  
  23. def dump(self):
  24. for ctx, el in self.events:
  25. _ctx = copy(ctx)
  26. _ctx['event'] = el
  27. yield _ctx
  28.  
  29. instrument = Instrument()
  30.  
  31. def quickSort(alist):
  32. quickSortHelper(alist,0,len(alist)-1)
  33.  
  34. def quickSortHelper(alist,first,last):
  35. if first<last:
  36.  
  37. splitpoint = partition(alist,first,last)
  38.  
  39. quickSortHelper(alist,first,splitpoint-1)
  40. quickSortHelper(alist,splitpoint+1,last)
  41.  
  42.  
  43. def partition(alist,first,last):
  44. pivotvalue = alist[first]
  45.  
  46. leftmark = first+1
  47. rightmark = last
  48.  
  49. done = False
  50. while not done:
  51.  
  52. while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
  53. leftmark = leftmark + 1
  54.  
  55. while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
  56. rightmark = rightmark -1
  57.  
  58. if rightmark < leftmark:
  59. done = True
  60. else:
  61. temp = alist[leftmark]
  62. alist[leftmark] = alist[rightmark]
  63. alist[rightmark] = temp
  64. instrument.emit(alist)
  65.  
  66. temp = alist[first]
  67. alist[first] = alist[rightmark]
  68. alist[rightmark] = temp
  69. instrument.emit(alist)
  70.  
  71. return rightmark
  72.  
  73. def gen_alist():
  74. A = 0
  75. B = 255
  76. COUNT = 255
  77. return random.sample(range(A,B+1),COUNT)
  78.  
  79. def main():
  80. alist = gen_alist()
  81. instrument.under({'line': 1})
  82. instrument.emit(alist)
  83. quickSort(alist)
  84. seq = []
  85. for e in instrument.dump():
  86. a = np.zeros((100, 255), dtype=np.uint8)
  87. for index, alist_el in enumerate(e['event']):
  88. a[:, index] = alist_el
  89. seq.append(a)
  90.  
  91. write_apng("demo.png", seq, delay=50, use_palette=True)
  92.  
  93. if __name__ == '__main__':
  94. main()
Add Comment
Please, Sign In to add comment