HELLO DORAEMONS,
IN THIS BLOGPOST ,I HAVE SHARED THE RECENTLY ASKED PYTHON QUESTION AND SOLUTION WHICH IS CERTAINLY USEFUL WHEN YOU WANT TO PRACTICE.HOPE THIS BLOG IS USEFUL TO YOU
Write the code to define a class to Create Box objects with 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 Customer Names as value and city name as key.(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".
Write the code to define a class to Create Shipping Company objects with attribute :
• a List of Box objects
• details of city wise shipping charges stored as a
dictionary of city: shipping charge 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 Shipping Company 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 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)+ shipping charges +increasedShipping Charge shipping Charges to be derived from Shipping Company's city wise shipping charges (the dictionary of city: shipping charges) where increasedShipping Charge can be derived based on below conditions: - If weight >=1 kg increasedShipping Charge will be shipping Charges increased by 10 Rs If weight >=5 kg increasedShipping Charge will be shipping Charges increased by 25 Rs If weight >=10 kg increasedShipping Charge 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 comparison are case insensitive These methods should be called from the main section. Instructions to write main section of the code Instruction to define 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 dictionary of shipping Charges as value and city as key and input for this dictionary(city: shippingCharges) for each dictionary item taken one after other.
Create Shipping Company 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 argument.
d. Read a string value as input depicting the customer Name to be provided as argument to the method first method defined in the Shipping Company class.
e. Read a number value as input depicting the box Id to be provided as arguments to the first method defined in the Shipping Company class ..
f. Call the first method defined in the Shipping Company class from the main section. If 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 Shipping Company
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 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 dictionary of shipping charges as value and city as key and input for this dictionary(city and shippingCharges) for each dictionary item taken one after other.
d. The next line of input is the string representing the cutomerName and number as boxid 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 :
3
101
20
100
1000
3
Delhi
2
Hari sham
Kolkata
1
Tom
Bangalore
1
David
102
20
5
3000
2
Delhi
1
Sam
Bangalore
1
John
103
20
0
3000
2
Delhi
1
John
Bangalore
1
David
4
Delhi
60
Bangalore
100
Kolkata
50
Hyderabad
150
Hema
101
output:
101 Available 99
102 Available 5
103 Sold 0
Delhi : 114320
Kolkata : 99150
Bangalore : 114400
For solution check:
0 Comments