Skip to content

Commit d2a2cf0

Browse files
author
boraxpr
committedOct 23, 2019
bite 5 and 103
1 parent 8c62893 commit d2a2cf0

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed
 

‎103/test_winners.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from winners import print_game_stats
2+
3+
4+
def test_print_game_stats(capfd):
5+
winner_prints = ["sara has won 0 games",
6+
"bob has won 1 game",
7+
"tim has won 5 games",
8+
"julian has won 3 games",
9+
"jim has won 1 game"]
10+
11+
print_game_stats()
12+
output = capfd.readouterr()[0].splitlines()
13+
14+
# dict + Python 3.7 = insert order should be retained
15+
for line in winner_prints:
16+
assert line in output

‎103/winners.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
games_won = dict(sara=0, bob=1, tim=5, julian=3, jim=1)
2+
3+
4+
def print_game_stats(games_won=games_won):
5+
"""Loop through games_won's dict (key, value) pairs (dict.items)
6+
printing (print, not return) how many games each person has won,
7+
pluralize 'game' based on number.
8+
9+
Expected output (ignore the docstring's indentation):
10+
11+
sara has won 0 games
12+
bob has won 1 game
13+
tim has won 5 games
14+
julian has won 3 games
15+
jim has won 1 game
16+
17+
(Note that as of Python 3.7 - which we're using atm - dict insert order is retained
18+
so no sorting is required for this Bite.)
19+
"""
20+
for key,value in games_won.items():
21+
# print(str(key) + " " + str(value))
22+
print(str(key) + " has won " + str(value) + " games")
23+
pass
24+
25+
# print_game_stats(games_won)

‎5/names.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
NAMES = ['arnold schwarzenegger', 'alec baldwin', 'bob belderbos',
2+
'julian sequeira', 'sandra bullock', 'keanu reeves',
3+
'julbob pybites', 'bob belderbos', 'julian sequeira',
4+
'al pacino', 'brad pitt', 'matt damon', 'brad pitt']
5+
6+
7+
def dedup_and_title_case_names(names):
8+
"""Should return a list of title cased names,
9+
each name appears only once"""
10+
nameset = set()
11+
for name in names:
12+
nameset.add(name.title())
13+
return list(nameset)
14+
15+
def sort_by_surname_desc(names):
16+
"""Returns names list sorted desc by surname"""
17+
names = dedup_and_title_case_names(names)
18+
namelist = []
19+
# ...
20+
namedict = dict()
21+
for name in names:
22+
split = name.split(" ")
23+
namedict[split[1]] = split[0]
24+
namedictsorted = sorted(namedict.items(), reverse=1)
25+
for each in namedictsorted:
26+
namelist.append(each[1]+" "+each[0])
27+
return namelist
28+
29+
def shortest_first_name(names):
30+
"""Returns the shortest first name (str).
31+
You can assume there is only one shortest name.
32+
"""
33+
names = dedup_and_title_case_names(names)
34+
firstnames = []
35+
# ...
36+
namedict = dict()
37+
for name in names:
38+
split = name.split(" ")
39+
namedict[split[1]] = split[0]
40+
for name in namedict.values():
41+
firstnames.append(str(name))
42+
return min(firstnames, key=len)
43+
44+
# dedup_and_title_case_names(NAMES)
45+
# sort_by_surname_desc(NAMES)
46+
# shortest_first_name(['brian okken', 'michael kennedy', 'trey hunner',
47+
# 'matt harrison', 'julian sequeira', 'dan bader',
48+
# 'michael kennedy', 'brian okken', 'dan bader'])

‎5/test_names.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from names import (NAMES, dedup_and_title_case_names,
2+
sort_by_surname_desc, shortest_first_name)
3+
4+
PY_CONTENT_CREATORS = ['brian okken', 'michael kennedy', 'trey hunner',
5+
'matt harrison', 'julian sequeira', 'dan bader',
6+
'michael kennedy', 'brian okken', 'dan bader']
7+
8+
9+
def test_dedup_and_title_case_names():
10+
names = dedup_and_title_case_names(NAMES)
11+
assert names.count('Bob Belderbos') == 1
12+
assert names.count('julian sequeira') == 0
13+
assert names.count('Brad Pitt') == 1
14+
assert len(names) == 10
15+
assert all(n.title() in names for n in NAMES)
16+
17+
18+
def test_dedup_and_title_case_names_different_names_list():
19+
actual = sorted(dedup_and_title_case_names(PY_CONTENT_CREATORS))
20+
expected = ['Brian Okken', 'Dan Bader', 'Julian Sequeira',
21+
'Matt Harrison', 'Michael Kennedy', 'Trey Hunner']
22+
assert actual == expected
23+
24+
25+
def test_sort_by_surname_desc():
26+
names = sort_by_surname_desc(NAMES)
27+
assert names[0] == 'Julian Sequeira'
28+
assert names[-1] == 'Alec Baldwin'
29+
30+
31+
def test_sort_by_surname_desc_different_names_list():
32+
names = sort_by_surname_desc(PY_CONTENT_CREATORS)
33+
assert names[0] == 'Julian Sequeira'
34+
assert names[-1] == 'Dan Bader'
35+
36+
37+
def test_shortest_first_name():
38+
assert shortest_first_name(NAMES) == 'Al'
39+
40+
41+
def test_shortest_first_name_different_names_list():
42+
assert shortest_first_name(PY_CONTENT_CREATORS) == 'Dan'
43+
44+
# test_dedup_and_title_case_names()
45+
# test_dedup_and_title_case_names_different_names_list()
46+
# test_shortest_first_name()
47+
# test_shortest_first_name_different_names_list()
48+
# test_sort_by_surname_desc()
49+
# test_sort_by_surname_desc_different_names_list()

0 commit comments

Comments
 (0)
Please sign in to comment.