TCS Xplore CPA Python Coding Question – 3 with Solution

TCS Xplore CPA Python Coding Question – 3 with Solution

In this post, we will see the solution of the Python Coding Question which is already asked in the TCS Xplore CPA Exam.

This question purely based on object-oriented programming.

Let’s see the Question:
Create a class DairyProduct with below attributes:

dairyld -> Number
dairyBrand  -> String
productType -> String
price -> Number
grade -> String

TCS python paper - 1

TCS python paper - 2

TCS python paper - 3

TCS python paper - 4

TCS python paper - 5

TCS python paper - 6

TCS python paper - 7

Sample Test Cases:
Input 1:

5
011
Ankit
Milk
25
grade3
131
Rahul
Curd
35
Grade1
021
Heritage
Milk
27
Gaade2
055
Kwality
lazzi
40
Grade1
060
Amul
Yogurt
35
Grade1
3
Grade1
10
Grade2
20
Grade3
5
Amul
Milk

Output 1:

No dairy product found

Input 2:

5
011
Aavin
Milk
25
grade3
131
Hatsun
Curd
35
Grade1
021
Heritage
Milk
27
Grade2
055
Kwality
lazzi
40
Grade1
060
Amul
Yogurt
35
Grade1
3
Grade1
25
Grade2
15
Grade3
5
Amul
Yogurt

Output 2:

Dairy Brand: Amul
Price: 43.75

Now let’s see the implementation for this problem statement:

# Create a DairyProduct 
# class with its attributes
class DairyProduct :

    # Define a constructor method
    # for initializing its attributes
    def __init__(self, *args) :

        # * is generally used for taking
        # variable number of arguments
        # in function/method
        
        # args is tuple of arguments
        # so we can access these arguments
        # with indices
        self.dairyId = int(args[0])
        self.dairyBrand = args[1]
        self.productType = args[2]
        self.price = int(args[3])
        self.grade = args[4]

# Create a ProductGrade 
# class with its attributes
# and method
class ProductGrade :

    # Define a constructor method
    # for initializing its attributes
    def __init__(self, *args) :

        self.dairyList = args[0]
        self.weightageDict = args[1]

    # Define a method for updating the price
    # as per requirement and return list of
    # tuple of brand name and its updated price
    def priceBasedOnBrandAndType(self, brand, ptype) :

        # empty list
        ans = []

        # iterate through each DairyProduct
        # objects present in the list
        for obj in self.dairyList :

            # if given condition is satisfied then
            # update the price for that brand
            # and appending into the list 
            if obj.dairyBrand == brand and obj.productType == ptype :

                obj.price += (obj.price * self.weightageDict[obj.grade]) / 100

                ans.append((brand, obj.price))

        # if list is empty then return
        # None otherwise return
        # that list
        if len(ans) :
            return ans
        else :
            return None

# Main program starts here
if __name__ == "__main__" :

    # read the number of DairyProduct 
    n1 = int(input())

    # empty list
    dairyProducts = []

    # Create a n1 number of
    # DairyProduct objects
    for i in range(n1) :

        # reading the data related
        # to DairyProduct class
        dId = input()
        dBrand = input()
        pType = input()
        price = input()
        grade = input().lower()

        # Create DairyProduct object 
        # with its attributes
        dpObj = DairyProduct(dId, dBrand, pType, price, grade)

        # adding DairyProduct object
        # to the list
        dairyProducts.append(dpObj)

    #  read the number of dictionary elements
    n2 = int(input())

    # empty dictionary
    weightageDict = {}

    
    for i in range(n2) :

        # reading the key,value
        # pair for a dictionary
        grade = input().lower()
        value = int(input()) 

        # insert key,value pair
        # to the dictionary
        weightageDict[grade] = value

    # Create ProductGrade object 
    # with its attributes
    pgObj = ProductGrade(dairyProducts, weightageDict)

    # reading brand name and product type
    dairyBrand = input()
    producttype = input()

    # priceBasedOnBrandAndType() method called
    rslt = pgObj.priceBasedOnBrandAndType(dairyBrand, producttype)

    # if rslt is not empty
    # then print required outputs
    # otherwise print message
    # "No dairy product found"
    if rslt :
        
        for item in rslt :
            print("Dairy Brand:", item[0])
            print("Price:", item[1])
            
    else :
        print("No dairy product found")
    

Thanks for reading this blog.

Checkout this post also: TCS Xplore CPA Python Coding Question -2 With Solution

If you have any doubts in understanding the above Code or Question then please comment below, we will try to clear your doubts.

Leave a Reply

Your email address will not be published.