Created
May 23, 2018 14:01
-
-
Save moxis/f3711f0541672c00933a9094b1ccb3ef to your computer and use it in GitHub Desktop.
SleepSort meme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import threading | |
import time | |
import numpy | |
class SleepSort: | |
def __init__(self, values, speed=1): | |
self.values = values | |
self.converted_values = [] | |
for x in self.values: | |
if type(x) is str: | |
self.converted_values.append(self.convert_to_ascii(x)) | |
else: | |
self.converted_values.append(x) | |
self.max = max(self.converted_values) * speed | |
self.result = [] | |
def convert_to_ascii(self, x): | |
ascii_values = [] | |
for letter in str(x): | |
ascii_values.append(str(ord(letter))) | |
return int(''.join(ascii_values)) | |
def sleep(self, x): | |
time.sleep(x / self.max) | |
self.result.append(self.values[self.converted_values.index(x)]) | |
def sort(self): | |
threads = [] | |
for x in self.converted_values: | |
thread = threading.Thread(target=self.sleep, args=(x,)) | |
thread.start() | |
threads.append(thread) | |
for thread in threads: | |
thread.join() | |
return self.result | |
if __name__ == '__main__': | |
random_array = numpy.random.randint(low=1, high=100, size=20) | |
correct = sorted(random_array) | |
result = SleepSort(random_array).sort() | |
if result == correct: | |
print("The array was sleep sorted succesfully!") | |
else: | |
print("Dumb program!") | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment