From 60f79d3c894c285f8b992965ab9e619882c0a778 Mon Sep 17 00:00:00 2001 From: sagar-sehgal Date: Wed, 23 Jan 2019 05:03:29 +0530 Subject: [PATCH 1/3] update in multimap functi n in utils.py and added test for it --- tests/test_utils.py | 3 +++ utils.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 8c7f5c318..597e28a63 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -25,6 +25,9 @@ def test_count(): assert count([True, False, True, True, False]) == 3 assert count([5 > 1, len("abc") == 3, 3+1 == 5]) == 2 +def test_multimap(): + assert multimap([(1,2),(1,3),(1,4),(2,3),(2,4),(4,5)]) == {1:[2,3,4],2:[3,4],4:[5]} + assert multimap([("a",2),("a",3),("a",4),("b",3),("b",4),("c",5)]) == {'a': [2, 3, 4], 'b': [3, 4], 'c': [5]} def test_product(): assert product([1, 2, 3, 4]) == 24 diff --git a/utils.py b/utils.py index ab6aa1032..9c86b17ca 100644 --- a/utils.py +++ b/utils.py @@ -42,10 +42,10 @@ def count(seq): # TODO: replace with quantify def multimap(items): """Given (key, val) pairs, return {key: [val, ....], ...}.""" - result = defaultdict(list) + result = collections.defaultdict(list) for (key, val) in items: result[key].append(val) - return result + return dict(result) def multimap_items(mmap): """Yield all (key, val) pairs stored in the multimap.""" From 1918e13bbdedbc3139bfbe67e1daa97bdcce9798 Mon Sep 17 00:00:00 2001 From: sagar-sehgal Date: Fri, 25 Jan 2019 19:41:41 +0530 Subject: [PATCH 2/3] made changes according to PEP style --- tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 597e28a63..1483c895a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -26,8 +26,8 @@ def test_count(): assert count([5 > 1, len("abc") == 3, 3+1 == 5]) == 2 def test_multimap(): - assert multimap([(1,2),(1,3),(1,4),(2,3),(2,4),(4,5)]) == {1:[2,3,4],2:[3,4],4:[5]} - assert multimap([("a",2),("a",3),("a",4),("b",3),("b",4),("c",5)]) == {'a': [2, 3, 4], 'b': [3, 4], 'c': [5]} + assert multimap([(1, 2),(1, 3),(1, 4),(2, 3),(2, 4),(4, 5)]) == {1:[2, 3, 4], 2:[3, 4], 4:[5]} + assert multimap([("a", 2), ("a", 3), ("a", 4), ("b", 3), ("b", 4), ("c", 5)]) == {'a': [2, 3, 4], 'b': [3, 4], 'c': [5]} def test_product(): assert product([1, 2, 3, 4]) == 24 From 75c08b22fb34e34b91a277ffee96cb639bb84c69 Mon Sep 17 00:00:00 2001 From: sagar-sehgal Date: Thu, 31 Jan 2019 02:09:15 +0530 Subject: [PATCH 3/3] broke the long sentence to 2 shorter sentences --- tests/test_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1483c895a..7617fdd20 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -26,8 +26,10 @@ def test_count(): assert count([5 > 1, len("abc") == 3, 3+1 == 5]) == 2 def test_multimap(): - assert multimap([(1, 2),(1, 3),(1, 4),(2, 3),(2, 4),(4, 5)]) == {1:[2, 3, 4], 2:[3, 4], 4:[5]} - assert multimap([("a", 2), ("a", 3), ("a", 4), ("b", 3), ("b", 4), ("c", 5)]) == {'a': [2, 3, 4], 'b': [3, 4], 'c': [5]} + assert multimap([(1, 2),(1, 3),(1, 4),(2, 3),(2, 4),(4, 5)]) == \ + {1: [2, 3, 4], 2: [3, 4], 4: [5]} + assert multimap([("a", 2), ("a", 3), ("a", 4), ("b", 3), ("b", 4), ("c", 5)]) == \ + {'a': [2, 3, 4], 'b': [3, 4], 'c': [5]} def test_product(): assert product([1, 2, 3, 4]) == 24