Created
October 13, 2016 17:16
-
-
Save JuliusNM/1a984ebe58517355868264c04cc125cb to your computer and use it in GitHub Desktop.
Weird_sort, Subset generator
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
def subsets(items): | |
subsets = [] | |
for code in range(0, 2**len(items)): | |
code = ("{0:0" + str(len(items)) + "b}").format(code, len(items)) | |
print(code) | |
# for i in range(0,len(items)): | |
subset = set() | |
for i, c in enumerate(code): | |
# print(i, c) | |
if c == "1": | |
subset.add(items[i]) | |
subsets.append(subset) | |
return subsets | |
if __name__ == '__main__': | |
s = subsets(["a", "b"]); print(s); | |
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
def weird_sort(numbers): | |
numbers = sorted(numbers) | |
count = 0 | |
results = [] | |
length = len(numbers) | |
#print(results, numbers) | |
while count < length: | |
if count %2 == 0: | |
results.append(numbers[0]) | |
numbers.remove(numbers[0]) | |
elif count %2==1: | |
results.append(numbers[-1]) | |
numbers.remove(numbers[-1]) | |
#print(results, numbers) | |
count +=1 | |
return results | |
x = weird_sort([2,1,4,3,7,6,5,8,9]) | |
print(x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment