Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Created July 5, 2013 19:43
Show Gist options
  • Save Snawoot/5936772 to your computer and use it in GitHub Desktop.
Save Snawoot/5936772 to your computer and use it in GitHub Desktop.
vlad@blastfurnace:~$ cat ex1.py
#!/usr/bin/python
def checkio(data):
#Your code here
#It's main function. Don't remove this function
#It's using for auto-testing and must return a result for check.
#replace this for solution
sorted_pairs = sorted(enumerate(data),None,lambda (index, element): element)
count = len(data)
if count == 1:
return []
nonuniq_sorted_pairs = map(lambda (prev, this, next): this, filter(lambda (prev, this, next): prev[1]==this[1] or next[1]==this[1], zip(sorted_pairs,sorted_pairs[1:],sorted_pairs[2:])))
if sorted_pairs[0][1]==sorted_pairs[1][1]:
nonuniq_sorted_pairs.append(sorted_pairs[0])
if sorted_pairs[count-2][1]==sorted_pairs[count-1][1]:
nonuniq_sorted_pairs.append(sorted_pairs[count-1])
return map(lambda (index, element): element, sorted(nonuniq_sorted_pairs,None,lambda (index, element): index))
print checkio(range(1,100000))
vlad@blastfurnace:~$ cat ex2.py
#!/usr/bin/python
def checkio(d):
return [x for x in d if d.count(x) > 1]
print checkio(range(1,100000))
vlad@blastfurnace:~$ time ./ex1.py
[]
real 0m0.366s
user 0m0.208s
sys 0m0.036s
vlad@blastfurnace:~$ time ./ex2.py
^CTraceback (most recent call last):
File "./ex2.py", line 6, in <module>
print checkio(range(1,100000))
File "./ex2.py", line 4, in checkio
return [x for x in d if d.count(x) > 1]
KeyboardInterrupt
real 0m58.396s
user 0m57.684s
sys 0m0.112s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment