Skip to content

Instantly share code, notes, and snippets.

@igauravsehrawat
Created May 20, 2015 11:39
Show Gist options
  • Save igauravsehrawat/0f49bacc65b3670c9876 to your computer and use it in GitHub Desktop.
Save igauravsehrawat/0f49bacc65b3670c9876 to your computer and use it in GitHub Desktop.
unbelievable result
#
# Write partition to return a new array with
# all values less then `v` to the left
# and all values greater then `v` to the right
#
someList = [4, 5,1, 3, 0, 9, 8, 12]
def partition(L, v):
P = L
# your code here
pos = 0
v_pos = rank(L,v)
print v_pos
P[v_pos-1] = v
for item in xrange(0,len(someList)):
if someList[item] < v:
P[pos] = someList[item]
pos += 1
elif someList[item] > v and v_pos < len(someList):
print item,v_pos
P[v_pos]= someList[item]
v_pos += 1
print P
return P
def rank(L, v):
pos = 0
for val in L:
if val < v:
pos += 1
return pos
partition(someList,4)
output:
3
1 3
3 4
4 5
5 6
6 7
[4, 5, 4, 5, 5, 5, 5, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment