-
Notifications
You must be signed in to change notification settings - Fork 0
/
bite135.py
67 lines (55 loc) · 1.74 KB
/
bite135.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from collections import namedtuple
from datetime import datetime
from operator import attrgetter
Book = namedtuple('Book', 'title authors pages published')
books = [
Book(title="Python Interviews",
authors="Michael Driscoll",
pages=366,
published="2018-02-28"),
Book(title="Python Cookbook",
authors="David Beazley, Brian K. Jones",
pages=706,
published="2013-05-10"),
Book(title="The Quick Python Book",
authors="Naomi Ceder",
pages=362,
published="2010-01-15"),
Book(title="Fluent Python",
authors="Luciano Ramalho",
pages=792,
published="2015-07-30"),
Book(title="Automate the Boring Stuff with Python",
authors="Al Sweigart",
pages=504,
published="2015-04-14"),
]
# all functions return books sorted in ascending order.
def sort_books_by_len_of_title(books=books):
"""
Expected last book in list:
Automate the Boring Stuff with Python
"""
return sorted(books, key= lambda x: len(x.title))
def sort_books_by_first_authors_last_name(books=books):
"""
Expected last book in list:
Automate the Boring Stuff with Python
"""
return sorted(books, key= lambda x: x.authors.split(' ')[1])
def sort_books_by_number_of_page(books=books):
"""
Expected last book in list:
Fluent Python
"""
return sorted(books, key=lambda x: x.pages)
def sort_books_by_published_date(books=books):
"""
Expected last book in list:
Python Interviews
"""
return sorted(books, key=lambda x: x.published)
print(sort_books_by_len_of_title())
print(sort_books_by_first_authors_last_name())
print(sort_books_by_number_of_page())
print(sort_books_by_published_date())