Created
August 18, 2012 18:08
-
-
Save chexov/3388785 to your computer and use it in GitHub Desktop.
mytcat1
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
#!/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