-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (91 loc) · 3.32 KB
/
main.py
File metadata and controls
100 lines (91 loc) · 3.32 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from dateutil import parser
from datetime import datetime
authors = {
"steven king": "Horror",
"rudyard kipling": "Adventure",
"isaac asimov": "Science Fiction",
"suzanne collins": "YA Fiction"
}
class Book:
def __init__(self, title, dateOfPublication, author, numberOfPages):
self.title = title
self.dateOfPublication = dateOfPublication
self.numberOfPages = numberOfPages
self.author = author
def getInput():
splitInputByBook = []
print("Enter books formatted as Title,Date,Author,Length with date formatted as mm/dd/yyyy or mm-dd-yyyy")
while True:
userInput = input()
splitInputByBook.append(userInput)
if userInput == "":
splitInputByBook.pop()
break
return splitInputByBook
def parseInput(userInput):
if userInput == None or len(userInput) == 0:
raise ValueError("Invalid Input") from None
else:
booksArray = []
for book in userInput:
tempList = book.split(",")
if len(tempList) == 4:
booksArray.append(tempList)
if len(booksArray) == 0:
raise ValueError(
"Invalid Input: Book must be formatted as title,date,author,length")
booksArrayByDetails = []
for book in booksArray:
try:
book[1] = parser.parse(book[1])
except:
raise ValueError(
"Date must be formatted in mm/dd/yyyy or mm-dd-yyyy") from None
try:
book[3] = int(book[3])
except ValueError:
raise ValueError(
"Invalid Input: Number of pages must be a valid number") from None
booksArrayByDetails.append(
Book(book[0], book[1], book[2].lower(), book[3]))
return booksArrayByDetails
def findAuthorWithMostBooks(books):
dictionary = {}
for book in books:
if book.author not in dictionary:
dictionary[book.author] = 1
else:
dictionary[book.author] += 1
maxNumberOfBooks = max(dictionary.values())
mostBooksAuthorsList = []
for author in dictionary:
if dictionary[author] == maxNumberOfBooks:
mostBooksAuthorsList.append(author)
mostBooksAuthorObjects = []
for author in mostBooksAuthorsList:
for book in books:
if book.author == author:
mostBooksAuthorObjects.append(book)
return mostBooksAuthorObjects
def findOldestBook(books):
neededBook = books[0]
oldestBook = parser.parse("12/31/2500")
for book in books:
currentDate = book.dateOfPublication
if currentDate < oldestBook:
oldestBook = currentDate
neededBook = book
return neededBook
def getGenre():
try:
genre = authors[neededBook.author]
except KeyError as e:
raise KeyError(
f"Sorry but {e} is not in the given list of authors") from None
print(f"{neededBook.title}, written by {genre} writer {neededBook.author.title()} on {neededBook.dateOfPublication.strftime('%m/%d/%Y')} is {neededBook.numberOfPages} pages long")
#Calling Functions
splitInputByBook = getInput()
books = parseInput(splitInputByBook)
mostBooksAuthorObjects = findAuthorWithMostBooks(books)
neededBook = findOldestBook(mostBooksAuthorObjects)
getGenre()