Skip to content

Instantly share code, notes, and snippets.

@chexov
Created August 18, 2012 18:08
Show Gist options
  • Save chexov/3388785 to your computer and use it in GitHub Desktop.
Save chexov/3388785 to your computer and use it in GitHub Desktop.
mytcat1
#!/usr/bin/env python
a = [1,2,3,45,-5,-5,6,5,0,-100, 1, 2, 3, 45, -5, -5, 6, 5, 0, -100, -5, -5, -5, -5, -5, -5,-5, -5, -5, -5]
def foo(myArray, count, isMin):
# creating unique array
mySet = set(myArray)
# if requested count > than number of elements
# setting count to number of elements
if count > len(mySet):
count = len(mySet)
result = []
if isMin:
f = min
else:
f = max
a = map(abs, mySet)
a.sort()
for i in range(count):
v = f(a)
if v not in mySet:
v = -1 * v
result.append(v)
a.remove(abs(v))
return result
print foo(a, 2, True)
print foo(a, 4, False)
print foo(a, 40000, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment