Created
September 22, 2017 03:45
-
-
Save Zheaoli/3266319fb8e88a192bde240b9cfdd527 to your computer and use it in GitHub Desktop.
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
def groupBy(func): | |
def inner(data): | |
assert isinstance(data,list) | |
temp_dict={} | |
for value in data: | |
if func(value) not in temp_dict: | |
temp_dict[func(value)]=[value] | |
else: | |
temp_dict[func(value)].append(value) | |
return temp_dict | |
return inner | |
def test_group_by(): | |
data=groupBy(lambda x: x%2)([1,2,3]) | |
assert data == {1:[1,3], 0:[2]} | |
data=groupBy(lambda x: x%2)([]) | |
assert data == {} | |
data=groupBy(lambda x: x%2)(None) | |
# raise assert error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment