Python Proctored Assessment Programming solution 15th January

Question 1


Create 2 functions.
Function 1 - takes a string as input and returns a dictionary containing word:occurence as key:value pairs.
Function 2 - takes a string as input and finds the word having maximum no. of occurrences. Use function 1 here.

Input 1 - Good morning Good evening
Output 1 - Good
Input 2 - Hello Bye Hi TCS Hello Xplore Hello Bye
Output 2 - Hello

 Solution:

class count_word:
    def count(self,input_word):
        d={}
        for i in input_word.split():
            d[i]=input_word.count(i)
        return d
def max_word(d):
    for n,c in d.items():
        if(c==max(d.values())):
            return n
if __name__=='__main__':
    input_word=raw_input()
    c=count_word()
    d=c.count(input_word)
    print max_word(d)

Question 2

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,bn,bname):
        self.bn=bn
        self.bname=bname
class Library:
    def __init__(self,lid,add,bl):
        self.lid=lid
        self.add=add
        self.bl=bl
    def count_books(self,char):
        c=0
        for i in self.bl:
            if(i.bname[0]==char):
                c+=1
        return c
    def remove_books(self,names):
        for i in self.bl:
            if i.bname not in names:
                print(i.bname)
               
if __name__=='__main__':
    bl=[]
    t=input()
    for i in range(t):
        bn=input()
        bname=raw_input()
        bl.append(Book(bn,bname))
    l=Library(123,'Mumbai',bl)
    char=raw_input()
    names=[]
    t=input()
    for i in range(t):
        names.append(raw_input())
    print l.count_books(char)
    l.remove_books(names)

 

©2018 by The real one. Proudly created with Wix.com