Ticker

6/recent/ticker-posts

TCS IRA 19TH JULY 2021 PYTHON QUESTION AND ANSWER

 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.

1. Xplore IRA Python 19 July 2021

Create a class Food with the below attributes.

name of type String 
proteins of type Number 
fat of type Number 
carbohydrates of type Number
energy of type Number 
status of type String

Create the__init_method which takes the parameters name, proteins, fat and carbohydrates in the above sequence. The method should set the value of attributes to parameter values and set default value of energy attribute as and status to none.

Create another class Nutrition with the below two attributes:
1. energydict of type dictionary having the Status(a string value) as the key and a tuple having two numbers (lower limitand upper representing lower and upper limits for tra
particular status as value. 

Note: The value for the upper limit cannot lower than the lower limit Code shoulder input data and accordingly set the  upper and lower limits are given in reverse order
input data and accordingly see the values in upper and lower limits are given in reverse
order.
2. foodlist of type List having Food objects
Create the init_method which takes all parameters in the above sequence. The method should set the value of attributes to parameter
values inside the method.
Create another method inside the Nutrition class with the name setEnergy The method calculates the energy value for all
Food in the foodlist of the Nutrition class and accordingly sets the values for the attribute energy of each Food,

Formula for energy calculation: 
energy = proteins+fat+carbohydrates

Method does not return any value.
Note:
• Method will calculate the energy for all food
in the foodlist of the Nutrition object and update the value for the energy attribute
and status of each Food.
• Method will find the status from the
energydict dictionary of the Nutrition class based on the energy value calculated for the Food
• All comparisons should be case insensitive

Create another method gettverglichford
This method will take a number representing the energy as argument and returns the list do objects from the foodList of the writion where energy value is less than or equal to value passed as the argument
If no Food fulfilling the conditions mentioned is found, then the method should return None.


Instructions to write 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 Food and Nutrition) with the given sequence of arguments to fulfill the init_method requirement defined in the respective classes referring to the below instructions.

1. Create a list of Food objects. To create the
list,

a. Read a number representing how many Food objects to be created.

b. Read data related to atributes too! (.e. name, proteins, fat and carbohydrates) create the Food object and add the object to the
list of Food objects which will be provided to the
Nutrition object. This point repeats for the number of Food objects to be created
(considered in the first line of input, 81a).

2. Create a dictionary (keysalue pairs) having the energystatus (a string) as key and range of energy(a tuple having two numbers representing
the lower limit and the upper limit of energy as
value. To create the dictionary:

a. Read a number for how many elements to be there in the dictionary.

b. Read a string representing the
energystatus (key) and two numbers (the lower limit and the upper limit) to create a tuple as the value and add the key value pair in the dictionary. This point repeats for the
number of elements (key:value pairs) to be added to the dictionary (considered in the input of point, 

3. Create Nutrition object by passing the List of Food objects (created and as mentioned in
above point #fc.1)) and the dictionary created in point #c2 as the arguments.

d. Take a number value as input representing the energy of food to be provided as argument to the method getEnergyRich Food

 e. Call the method setervergy of Nutrition cop
f. Display a message "Energy of Foods (excluding
quotes).

 h. Print the name energy and energystatus of all the Food in the foodlist of the Nutrition object created in point #3 in the order of input taken

1. Call the method getEnergyRichFood mentioned above from the main section 

g. Display a message "Food within given criteria (excluding the quotes). Followed by this, Print the name and energystatus of all the Foods in
the list returned by the method getEnergyRich Food. If the method returns None print the message 'No Food Found'(excluding the quotes
You can use/refer to the below given sample input and output to verify your solution using Test against Custom input option in Hackerrank.

Input Format for Custom Testing 
1. The 1st input taken in the main section is the number of Food objects to be added to the list of Food 
2. The next set of inputs are the name proteins
fat and carbohvdrares for each food take

3. The next input is the number of rebus pairs for energystatus:
Mis to be added to the dictionary
4. The next set of inputs are the status name. lower limit and upper limit for each keyvalue pair i.e.each element to be added to the dictionary taken one after other and is repeated
for number of pairs given in the point #3 5. The last line of input is the energy status value to be passed as argument to the method
getEnergy Rich Food.

Sample Input 1:
5
Apple
2.0 
1.0
25.1 
Beans 
10.5 
5.0
6.5 
Meat
41.5
44.5 
56.0
Milk 
35.5 
41.4 
45.0
Chocolate 
10.0
30.5
35.0
4
Low
0
50
Medium
51
100
High
101
150
Very High
151
200
40

Output: 

Energy of Food: 
Apple - 28.1 - LOW 
Beans - 22.0 - Low
Meat - 142.0 - High 
Milk - 121.9 - High 
Chocolate - 75.5 - Medium
 
Food within given criteria:
Apple : 28.1 
Beans: 22.0

Sample Input 2:
Apple
2.0
1.0
25.1 
Beans 
10.5 
5.0
6.5
Meat
41.5
44.5 
56.0
Milk 
35.5 
41.4 
45:0 
Chocolate 
10.0 
30.5
35.0
Low
1
50
Medium
51
100 
High 
101
150
Very High
151
200
20

Output: 
Energy of Food: 
Apple - 28.1 - Low 
Beans - 22.0-Low
Meat - 142.0 - High 
Milk - 121.9 - High 
Chocolate - 75.5 - Medium 
No Food Found


SOLUTION FOR THIS QUESTION IS EXPLAINED IN MY VIDEOS


HOPE YOU GUYS LIKE MY WORK AND IF YOU WANT TO GET NOTIFIED IN EVERY RELEASE OF MY BLOG AND VIDEOS.PLEASE DO SUBSCRIBE TO MY CHANNEL.IT MOTIVATES ME A LOT AND FOLLOW MY BLOG TO GET ALL PREVIOUSLY ASKED QUESTIONS.CHEERS !!!!!!

Post a Comment

0 Comments