Created
February 8, 2016 15:47
-
-
Save interrogator/13c7c59c13d3014ab3eb to your computer and use it in GitHub Desktop.
sort a list of numbers in one line
This file contains hidden or 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
# a list of ints | |
l = [4, 2, 6, 7, 8, 1, 50, 23, 13, 55, 12, 3] | |
# one line to sort them! | |
[l.insert(ind, l.pop(l.index(min(l[ind:])))) for ind in range(len(l))] | |
# see? | |
print(l) | |
# elaborated code | |
# count from zero to length of the list | |
for ind in range(len(l)): | |
# get smallest number from the unsorted portion of the list | |
smallest_number = min(l[ind:]) | |
# get the index of this | |
itsindex = l.index(smallest_number) | |
# pop this index from the list | |
the_smallest = l.pop(itsindex) | |
# insert at the end of the sorted part of the list | |
l.insert(ind, the_smallest) | |
print('Moving %d --> %d' % (the_smallest, ind)) | |
print(l) | |
# back to work now :( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment