Skip to content

Instantly share code, notes, and snippets.

@ygpark2
Last active March 18, 2016 04:24
Show Gist options
  • Save ygpark2/49c5b48fd065e896ccf1 to your computer and use it in GitHub Desktop.
Save ygpark2/49c5b48fd065e896ccf1 to your computer and use it in GitHub Desktop.
import random
a = [random.randint(1,100) for _ in range(20)]
sorted_array = []
def swap(a, ppos, cpos):
a[ppos],a[cpos] = a[cpos],a[ppos]
print(a[ppos], a[cpos], ppos, cpos)
def heap_sort(a, sorted_array):
size = len(a)
for i,x in enumerate(a):
child = size - 1 - i
parent = round(child/2)
if (a[parent] < a[child]):
print("--------- swap ------------")
swap(a, parent, child)
else:
print("--------- else ------------")
print(i, x, parent, child, a[parent], a[child])
print(a)
sorted_array.append(a.pop(0))
if len(a)> 0:
heap_sort(a, sorted_array)
print("--------- start ------------")
print(a)
heap_sort(a, sorted_array)
print("--------- result ------------")
print(a)
print("--------- output result ------------")
print(sorted_array)
from functools import reduce
A = [23, 5, 4, 7, 2, 11]
result = [ A[i:ii+i+1] for i, v in enumerate(A) for ii, vv in enumerate(A[i:]) ]
sumResult = map(lambda xs: reduce(lambda x,y: x+y, xs), result)
sumResultSet = list(set(sumResult))
if T in sumResultSet :
print("True")
else:
print("False")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment