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
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.
This solution is really well explained! I appreciate the step-by-step breakdown of the coding question. It helped me understand the concepts better. Thanks for sharing!
This post was incredibly helpful! I struggled with the problem initially, but your step-by-step breakdown of the solution made it much clearer. Thanks for sharing the insights and code snippets; they were very useful for understanding the approach! Looking forward to more posts like this.
Great breakdown of the TCS Xplore CPA Python coding question! The step-by-step explanation really helped clarify the solution. I particularly liked the way you approached the problem; it made it much easier to understand. Looking forward to more posts like this!
Great explanation of the coding question! The step-by-step breakdown really helped me understand the solution better. Thanks for sharing this valuable resource for aspiring coders!
Great explanation of the coding problem! The step-by-step breakdown of the solution made it much easier to understand. I appreciate how you highlighted the key concepts of Python relevant to the challenge. Looking forward to more posts like this!
Great job on breaking down the Python coding problem! The step-by-step solution really clarifies the logic behind the code. I appreciate the detailed explanation, especially how you approached the data structures. Looking forward to more insightful posts like this!