Created
December 6, 2013 04:29
-
-
Save kipanshi/7818584 to your computer and use it in GitHub Desktop.
``Make unique`` quiz research
This file contains hidden or 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
import unittest | |
import cProfile | |
import pstats | |
import StringIO | |
import random | |
import string | |
def make_unique_orig(list=[]): | |
""" | |
Returns list of unique elements | |
""" | |
if list == False: | |
return [] | |
# import ipdb; ipdb.set_trace() | |
for num, item in zip(range(len(list)), list): | |
if list.count(item) > 1: | |
del list[num] | |
return list | |
def make_unique(list_=[]): | |
""" | |
Returns list of unique elements | |
""" | |
if list_ == False: | |
return [] | |
for num, item in zip(range(len(list_)), list_): | |
if list_.count(item) > 1: | |
del list_[num] | |
return list_ | |
def make_unique_pop(list_=None): | |
""" | |
Returns list of unique elements | |
""" | |
if not list_ or not len(list_): | |
return [] | |
for num, item in enumerate(list_): | |
if list_.count(item) > 1: | |
list_.pop(num) | |
return list_ | |
def make_unique_remove(list_=None): | |
""" | |
Returns list of unique elements | |
""" | |
if not list_ or not len(list_): | |
return [] | |
for item in list_: | |
if list_.count(item) > 1: | |
list_.remove(item) | |
return list_ | |
def make_unique_del(list_=None): | |
""" | |
Returns list of unique elements | |
""" | |
if not list_ or not len(list_): | |
return [] | |
for num, item in enumerate(list_): | |
if list_.count(item) > 1: | |
del list_[num] | |
return list_ | |
def make_unique_set(iterable=None): | |
return list(set(iterable)) | |
def make_unique_populate_list(list_=None): | |
""" | |
Returns list of unique elements | |
""" | |
if not list_ or not len(list_): | |
return [] | |
result_list = [] | |
for i in list_: | |
if i not in result_list: | |
result_list.append(i) | |
return result_list | |
def make_unique_populate_tuple(list_=None): | |
""" | |
Returns list of unique elements | |
""" | |
if not list_ or not len(list_): | |
return [] | |
result_tuple = () | |
for i in list_: | |
if i not in result_tuple: | |
result_tuple += (i,) | |
# Convertion to list is optional, depends on how | |
# are you planning to use the data | |
return list(result_tuple) | |
def profile(func, *args, **kwargs): | |
pr = cProfile.Profile() | |
pr.enable() | |
func(*args, **kwargs) | |
pr.disable() | |
s = StringIO.StringIO() | |
sortby = 'cumulative' | |
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) | |
ps.print_stats() | |
return s.getvalue() | |
class UniqueTest(unittest.TestCase): | |
def setUp(self): | |
self.list = ['a', 'a', 'b', 'c', 'c', 1, 2, 2, 3] | |
self.result = ['a', 'b', 'c', 1, 2, 3] | |
def test_orig_unique(self): | |
self.assertEqual(self.result, make_unique_orig(self.list)) | |
def test_unique(self): | |
self.assertEqual(self.result, make_unique(self.list)) | |
def test_unique_enum_pop(self): | |
self.assertEqual(self.result, make_unique_pop(self.list)) | |
def test_unique_enum_remove(self): | |
self.assertEqual(self.result, make_unique_remove(self.list)) | |
def test_unique_enum_del(self): | |
self.assertEqual(self.result, make_unique_del(self.list)) | |
def test_make_unique_populate_list(self): | |
self.assertEqual(self.result, make_unique_populate_list(self.list)) | |
def test_make_unique_populate_tuple(self): | |
self.assertEqual(self.result, make_unique_populate_tuple(self.list)) | |
def test_performance(self): | |
def get_big_list(): | |
return [random.choice(string.ascii_letters) | |
for i in xrange(20000)] | |
print profile(make_unique_pop, get_big_list()) | |
print profile(make_unique_remove, get_big_list()) | |
print profile(make_unique_del, get_big_list()) | |
print profile(make_unique_set, get_big_list()) | |
print profile(make_unique_populate_list, get_big_list()) | |
print profile(make_unique_populate_tuple, get_big_list()) | |
if __name__ == '__main__': | |
unittest.main(verbosity=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment