Question 1.Python_SBQ_IRA_24th January
1. Python_SBQ_IRA_24th January
Class 1st: Write the code to define a class to Create Box objects with the following attributes:-
• an unique id for the Box
• weight of the box
• status of the box
• Quantity, represents the available quantity of the box
• Cost of the box
• Customer details, a Dictionary of list of CityName as key and CustomerNames as value.
(customers who have purchased one box from which city)
For example : suppose there are 3 customers from Delhi named Radha, Zubin and Pooja and 2 customers from the city Kolkata named Shudha and Rajesh, then the dictionary will be as below:
{Delhi:(Radha, Zubin, Pooja),Kolkata:(Shudha, Rajesh)
Note:-customer name should be unique
Write the code to Define the method in the class which takes as argument values for all attributes in the above sequence and set the value of attributes to parameter values while creating a Box object.
Method will set the attribute "status" as "Available" for the Box object created. If quantity is zero set status as "Sold Out"
Class 2nd: Write the code to define a class to Create ShippingCompany objects with attributes:-
• a List of Box objects
• details of city wise shipping charges stored as a dictionary of city: shipping charges as "key: value" pairs.
Write the code to define the method in the class which takes as argument values for all attributes in the above sequence and set the value of attributes to parameter values while creating an ShippingCompany object.
Define two methods as below in the class :
1) Define the first method. This method will take as argument a String representing the customer Name and a number as box id.Each customer can order only one box of each ID. Method will find the Box Objects in the Box List of the Shipping Company where below rules are satisfied:
1.Box having the given box id whose status is Available"
2. Customer with the given Name has not ordered the box before (e.i the given name is not present in the list of customers of the Customer details of the Box)
If a Box object fulfilling the above 2 conditions are found, then deduct 1 from the Quantity and update the Quantity of the box object.
The method will also update the status of the Box object to "Sold Out" if the Quantity after update becomes 0.
If all above conditions are satisfied method will return True. If the box id passed as argument does not exists or if the customer name is found in the list of Customer Names of the Customer Details of the Box with the given ID, method will return False.
2) Define the second Method :- This method will calculate city wise the future total sale of boxes in the Box list of the Shipping Company and return as a dictionary of city : future total sale as key value pairs.
To find out the future total sale for a city, find the summation of the future total sale of the city for all boxes considering the city name from the Customer details of the box. Method will use the following formula to calculate the future total sale:
(Cost *quantity)+ shippingCharges +increasedShippingCharge
shippingCharges to be derived from ShippingCompany's city wise shipping charges (the dictionary of city: shipping charges)
where increasedShippingCharge can be derived based on the below conditions: -
If weight >=1 kg increasedShippingCharge will be shipping Charges increased by 10 Rs
If weight >=5 kg increasedShippingCharge will be shipping Charges increased by 25 Rs
If weight >=10 kg increasedShippingCharge will be shipping Charges increased by 100 Rs
Method will calculate the total future sale of city wise for all boxes.
Note: All the string comparisons are case insensitive
These methods should be called from the main section.
Instructions to write the main section of the code
Instruction to define the main function:
A). You would require to write the main section completely, hence please follow the below instructions for the same.
B). You would require to write the main program which is inline to the "sample input description section" mentioned below and to read the data in the same sequence.
C). Create the respective objects( Box and Shipping Company ) with the given sequence of arguments to fulfill defined in the respective classes referring to the below instructions.
1. Create a list of Box objects which will be provided to the Shipping Company object to be created. To create the List,
a. Read a number for the count of Box objects to be created and added to the list.
b. Create a Box object after reading the data related to it and add the object to the list of Box objects.
For Customer Details of the Box,
a. Read a number N for the count of city and customerName pair to be added to the dictionary for Customer Details of the Box.
b. Read a city name followed by that read a number representing the count of customers who purchased the box from that city. Now read that many count of unique customer Names and create a list.
c. Add the city name: customer name list to the dictionary.
Repeat step #c.1.b.b and #c.1.b.c for N number of pairs to be created and added to the dictionary.
This point repeats for the number of Box objects to be added to the list (considered in the first line of input) point #c.1.a.
2. The next line of input is the number of dictionary pairs for a dictionary of shippingCharges as value and city as key and input for this dictionary(city: shippingCharges) for each dictionary item taken one after other.
Create ShippingCompany object by passing the List of Box objects (created and as mentioned in above point# c.1) and dictionary of city: shipping Charges (created above in point #c.2) as an argument.
D). Read a string value as input depicting the customerName to be provided as an argument to the method first method defined in the ShippingCompany class.
E). Read a number value as input depicting the box Id to be provided as arguments to the first method defined in the ShippingCompany class...
F). Call the first method defined in the ShippingCompany class from the main section. If the value returned by the method is is false display "No data found without quotes else display the box id, status and quantity of all the boxes in the Box list of the ShippingCompany
G). Call the second method defined in the Shipping Company class to return city wise future totalSale for cities box is sold. Print the values returned by the method. In the output, the key and values are printed separated by a ":" from the dictionary.e.g. city:totalSale.
Refer to the sample input and output below.
Sample Input (below) description:
a. The 1st input taken in the main section is the number of Box objects to be added to the list of boxes for the Shipping Company.
b. The next set of inputs are the box id, weight, quantity, cost, number of dictionary pairs for customer name and city and input for this dictionary(city, size of customerNameList and iterate for the size of customerNameList to form the customerNameList) for each Box object taken one after other and is repeated for number of Box objects given in the first line of input
c. The next line of input is the number of dictionary pairs for a dictionary of shippingCharges as value and city as key and input for this dictionary(city and shipping Charges) for each dictionary item taken one after other.
d. The next line of input is the string representing the cutomerName and number as boxld to be supplied as an argument to first Method.
You can consider below sample input and output to verify your implementation before submitting in Hackerrank.
Sample Testcase1:
Input | Output |
---|---|
3 #number of Box objects 101 box_id 20 weight 100 quantity 1000 cost 3 # No. of Dictonary items Delhi #Key 2 #How many values to its key Hari # sham # Kolkata 1 Tom Bangalore 1 David #Box object 2 starting 102 20 5 3000 2 # No. of Dictonary items Delhi #key 1 #How many values to its key Sam # Bangalore #key 1 #How many values to its key John # #box object 3 starting 103 20 0 3000 2 # No. of Dictonary items Delhi #Key 1 #How many values to its key John Bangalore #Key 100 #How many values to its key Kartik #key 50 #numbers of dictonary Pair of ShippingCharge Hydrabad #key 150 #value Heema # customerName to be supplied as an argument to first Method. 101 # boxld to be supplied as an argument to first Method. |
101 Avaliable 99 102 Avaliable 5 103 Sold 0 Delhi : 114320 Kolkata : 99150 Bangalore : 114400 |
Answer
class Box: def __init__(self, Box_id,weight , quantity, cost, cust_Dict ): self.box_id=Box_id self.weight=weight self.status="Available" self.quantity=quantity self.cost=cost self.cust_Dict=cust_Dict self.flag=0
class ShippingCompany : def __init__(self, box_obj_list ) : self.box_obj=box_obj_list def method_1 (self, Cust_name, box_id ) : flag=0 is_Box_id_found=False for i in self.box_obj: if box_id==i.box_id: is_Box_id_found=True if is_Box_id_found==False: return False for i in self.box_obj: if box_id==i.box_id: for k,v in i.cust_Dict.items(): flag=[ 1 for z in v if z==Cust_name] if flag==[] : i.flag=0 else: i.flag=flag[0] return False if i.flag==0: for k,v in i.cust_Dict.items(): l=list(v) l.append(Cust_name) i.cust_Dict[k]=tuple(l) break if 0<i.quantity: i.quantity-=1 if i.quantity==0: i.status="Sold Out" print (i.box_id, i.status, i.quantity) else: i.status="Sold" print (i.box_id, i.status, i.quantity) else: print (i.box_id, i.status, i.quantity) def method_2 (self,Ship_Charge_dict ): flag=1 dicc={} dicc2={} dicc3={} #er={} shippingCharges=[] increasedShippingCharge=0 for i in self.box_obj: for z in i.cust_Dict.keys(): for j in Ship_Charge_dict.keys(): if z==j: key=z shippingCharges=Ship_Charge_dict[j] if i.weight>=10: increasedShippingCharge=Ship_Charge_dict[j]+100 elif i.weight>=5: increasedShippingCharge=Ship_Charge_dict[j]+25 elif i.weight>=1: increasedShippingCharge=Ship_Charge_dict[j]+10 else: increasedShippingCharge=Ship_Charge_dict[j] + increasedShippingCharge total_sale=(i.cost * i.quantity) + shippingCharges + increasedShippingCharge dicc[key]=total_sale else : flag=0 for x in set(dicc2).union(dicc): er=dicc.get(x, 0) + dicc2.get(x, 0) dicc3[x]=er dicc2=dicc3 dicc3={} for keys,values in dicc2.items(): print(keys,":",values) return None if flag==0 else True
if __name__=="__main__": N=int(input("BOX object N: " )) a=[] for i in range(N): Box_id=int(input("Box_id")) weight =int(input("Weight : ")) quantity=int(input(" Quantity: ")) cost=int(input("Cost: ")) n=int(input("Dictionary Limit: = ")) dic={} for i in range(n): z=[] key=input("City") m=int(input("No of Cus :")) for i in range (m): b=input("Cus Name") z.append(b) dic[key] =tuple(z) del z[:] a.append(Box(Box_id,weight,quantity,cost,dic)) ShippingChargeRange=int(input("ShippingChargeRange")) Ship_Charge_dict={} for i in range(ShippingChargeRange): key=input("City") value=int(input("Shipping Charge: ")) Ship_Charge_dict[key] =value CustName_search=input("Custumer Name for Search") Box_id_search=int(input("BoxId : ")) ship_obj=ShippingCompany(a) result1=ship_obj.method_1( CustName_search, Box_id_search) result2=ship_obj.method_2(Ship_Charge_dict) if result1==False: print("False")
Question 2. Xperience IRA 7th Feb 2022_ Python: Product and Shipping
Write the code to define a class to Create a Product object with following attributes:
• Product Id
• Product Brand
• Product Type
• Unit Price
• Quantity in hand
• Status
Write the code to define a method which takes parameters in above sequence for Product Id, Product Brand, Product Type, Unit Price and Quantity in hand and sets the values for attributes while creating a Product object. For the attribute Status a default value of "Available" is set.
Write the code to define another class to create a Shipping Company object with attributes as below:
• A List of Product Objects
• Shipping Charge List. A list which maintains the names of the cities to which product is shipped along with the applicable shipping charge as "city name : shipping charge" as key : value pairs.
Write the code to define the method to initialize the attributes in the above sequence while creating a Shipping Company object.
Create a second method inside the Shipping Company class to calculate the bill for an order for a Product of a given brand, type in a given quantity to be shipped to a given city.
The method will take 4 arguments as below and return the total bill -
- A value depicting a Product brand as the 1st argument
- A value depicting a Product type as the 2nd argument
- A value representing the name of a city as the 3rd argument
- A value representing the required quantity of Products as the 4th argument
From the Product List of the Shipping Company, method will look for a Product having Product Brand and Product Type value same as the values passed as 1st and 2nd arguments and the quantity in hand of the Product should be higher than or equal to the required quantity value passed as the 4th argument.
If a Product fulfilling these conditions is found, method will find the applicable Shipping charge from the Shipping charge list of the Shipping Company for the city passed as the 3rd argument and calculate the total bill as below:
total bill = Unit price * required quantity of products (value passed as the 4th argument) + Shipping Charge*required quantity of products (value passed as the 4th argument).
This total bill will be calculated only if the name of the city passed as the 3rd argument is present in the Shipping charge list of the Shipping Company.
If the bill can be calculated, method will return the calculated bill and update the value of quantity in hand of the Product found in the Product List of the Shipping Company by deducting the required quantity (value passed as the 4th argument) from it. If the updated quantity in hand becomes 0, the status of the Product will be made "unavailable"
If no Product having the given product brand and product type or having sufficient quantity in hand is available, method will return None.
Also, if the name of the city passed as the 3rd argument is not present in the Shipping charge list of the Shipping Company, method will return None.
Note: • All string comparisons should be case insensitive • Assume there can be more than one Products of same type but there will not be more than one Products of same type from same brand.
Create a third method inside the Shipping Company class to find the product type wise count of brands for which Product is available in the updated Product List of the Shipping Company.
Method will return as a list of key:value pairs the product type wise count of available product brands. (product type: count of available brands)
If no valid Products found, method will return None.
Note:
In the list to be returned, the first character of the string for Product Type should be in upper case. For more clarity please refer to the sample input and output below.
Instructions to write main section of the code:
A. You would require to write the main section completely, hence please follow the below instructions for the same.
B. You would require to write the main program which is inline to the sample input description section" mentioned below and to read the data in the same sequence.
C. Create the respective objects (Product and Shipping Company) with the given sequence of arguments to fulfill the method requirement defined in the respective classes referring to the below instructions.
-
i. Create a list of Products. To create the list
- 1. First read the number of Products you want to store in the list.
- 2. Read the values for the attributes of the Product (Product Id, Product Brand, Product Type, Unit Price, Quantity in hand) in the same sequence, create the product object and add it to the product list. This point repeats for the number of Products (consider the input taken in point #1 above) to be created.
- 1. First read the number of elements you want to store in the list.
- 2. Read the values for the city name and corresponding shipping charge and add the values as key:value pair to the list. This point repeats for the number of elements (consider the input taken in point #ii.1 above) to be created.
E. Call the 2nd method defined in the Shipping Company class by using the Shipping Company object created in point #c.iii from the main section.
F. Display the message "Bill Calculated:" (excluding the quotes). Display the bill value returned by the method called in point #e in the same line. If the method returns None, display the message "Product Not Found." (excluding the quotes).
G. Call the 3rd method defined in the Shipping Company class by using the object created in point #c.iii from the main section.
H. Display the values returned by the method called in point #g as <Product Type> <Count of available brands> in separate lines in ascending order of the Product Type.
If the method returns None, display the message 'No Products found' (excluding the quotes).
Please refer to the sample output given for reference for more clarity on output formats.
Input Format for Custom Testing...
A. The 1st input taken in the main section is the number of Product objects to be added to the list of Products.
B. The next set of inputs are the values related to attributes of Product objects to be added to the list.
Product Id, Product Brand, Product Type, Unit Price, Quantity in hand.
The above point repeats for the number of Product objects, read in point#a
C. The next input is the count of elements (key:value pairs) to be added to the Shipping Charges List of the Shipping Company class.
D. The next set of inputs are the values for city name and corresponding shipping charge which is repeated for the number of elements mentioned in point #c.
E. The last four lines of input refer to the input Product Brand, Product Type, city name and required product quantity to be passed as argument to the 2nd method of the Shipping Company Class.
You can consider below sample input and output to verify your implementation before submitting in Hackerrank.
Input | Output |
---|---|
5 1011 Leo Electronic 300 20 1012 Ledo Electronic 350 50 1021 Leo Puzzle 100 20 1022 Leo mechanics 400 30 1023 FunkCool puzzle 350 20 3 Guwahati 50 Kolkata 30 Shillong 100 Leo Electronic Shillong 20 |
Bill Calculated:8000 Electronic:1 Mechanics:1 Puzzle:2 |