PA Programming Solution - 15th January 2020
Q1.Inside a Class create 2 functions.
Function1-takes a string as input and returns a dictionary containing word: occurrence as key: value pairs.
Function2- takes a string as input and find the word having maximum number of occurrences. Use function 1 here.
Sample Input1
Good morning Good evening
Sample Output1
Good
Sample Input2
Hello Bye Hi TCS Hello Xplore Hello Bye
Sample Output2
Hello
Solution:
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. It has a constructor. Class Library has 3 attributes-library_id, address and a list of book objects. It 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.
Sample Input:
3
100
C++ Programming
200
Introduction to SQL 300
Core Java
C
2
Introduction to SQL
Python Programming
Sample 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)
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)
Comments
Post a Comment