Skip to content

Commit bf13a8d

Browse files
bites 78
1 parent 5a8dce9 commit bf13a8d

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@
4343
/10/README.md
4444
/62/README.md
4545
/86/README.md
46+
/78/README.md

78/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from functools import reduce
2+
3+
4+
def common_languages(programmers):
5+
"""Receive a dict of keys -> names and values -> a sequence of
6+
of programming languages, return the common languages"""
7+
listoflang = [programmers[dev] for dev in programmers]
8+
return reduce(set.intersection, map(set, listoflang))
9+
10+
# print(common_languages(dict(bob=['JS', 'PHP', 'Python', 'Perl', 'Java'],
11+
# tim=['Python', 'Haskell', 'C++', 'JS'],
12+
# sara=['Perl', 'C', 'Java', 'Python', 'JS'],
13+
# paul=['C++', 'JS', 'Python'])))

78/test_common.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
3+
from common import common_languages
4+
5+
6+
@pytest.fixture()
7+
def programmers():
8+
return dict(bob=['JS', 'PHP', 'Python', 'Perl', 'Java'],
9+
tim=['Python', 'Haskell', 'C++', 'JS'],
10+
sara=['Perl', 'C', 'Java', 'Python', 'JS'],
11+
paul=['C++', 'JS', 'Python'])
12+
13+
14+
def test_common_languages(programmers):
15+
expected = ['JS', 'Python']
16+
actual = common_languages(programmers)
17+
assert sorted(list(actual)) == expected
18+
19+
20+
def test_adding_programmer_without_js(programmers):
21+
programmers['sue'] = ['Scala', 'Python']
22+
expected = ['Python']
23+
actual = common_languages(programmers)
24+
assert list(actual) == expected
25+
26+
27+
def test_adding_programmer_without_js_nor_python(programmers):
28+
programmers['fabio'] = ['PHP']
29+
expected = []
30+
actual = common_languages(programmers)
31+
assert list(actual) == expected
32+
33+
34+
def test_common_languages_adding_new_common_language(programmers):
35+
programmers['bob'].append('C++')
36+
programmers['sara'].append('C++')
37+
expected = ['C++', 'JS', 'Python']
38+
actual = common_languages(programmers)
39+
assert sorted(list(actual)) == expected

0 commit comments

Comments
 (0)