class count_word:
def count(self,word):
diction = {}
for i in word.split():
diction[i] = word.count(i)
return diction
def max_word(diction):
for n,c in diction.items():
if(c==max(diction.values())):
return n
if __name__=='__main__':
word = input()
c = count_word()
d = c.count(word)
print(max_word(d))
Q2.Class Book - has 2 attributes - book_id and book_name. Has a constructor.
Class Library - has 3 attributes - library_id, address and a list of book objects. Has 2 functions.
1.One gets a character as input and returns the count of books starting with that character.
2.Second function takes a list of book names as input and removes those books from library if present.
input:
3
100
C++ Programming
200
Introduction to SQL
300
Core Java
C
2
Introduction to SQL
Python Programming
Output:
2
C++ Programming
Core Java
Solution:
class Book:
def __init__(self, bookId, bookName):
self.bookId = bookId
self.bookName = bookName
class Library:
def __init__(self, libId, address, bookList):
self.libId = libId
self.address = address
self.bookList = bookList
def count_book(self, char):
count = 0
for i in bookList:
if (i.bookName[0] == char):
count = count + 1
return print(count)
def remove_book(self, books):
for j in bookList:
if j.bookName not in books:
print(j.bookName)
if __name__ == '__main__':
count = int(input())
bookList = []
for i in range(count):
bookId = input()
bookName = input()
bookList.append(Book(bookId, bookName))
libId = input()
address = input()
lib = Library(libId, address, bookList)
char = input()
books = []
cnt = int(input())
for j in range(cnt):
books.append(input())
out1 = lib.count_book(char)
lib.remove_book(books)