Ticker

6/recent/ticker-posts

TCS IRA | CPA PYTHON 22-FEB-2022 QUESTION AND SOLUTION

 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.

Python Define a class Trade having the following attributes: 

Unique Trade ID for each Trade of type integer 

Amount for each Trade of type Float 

Names of coins in the Trade of type List of strings 

Date of Trade of type String in format dd/mm/yyyy 

Trade type of type string with values as 'Buy' or ‘Sell'. 

Note: Define the __int__method to initialize the attributes of the objects to be created. 

Create another class Crypto with following attributes

 a List of Trade Objects

 Create below functions inside the Crypto class.

  • This function takes the year of Trade (as number) as input parameter. It returns total Amount of "buy" Trades from the list of Trades of the Crypto class where year from the date of Trade matches with the passed argument and any of the item names in the coins List attribute matches "Bitcoin". 

 If no Trade fulfilling the conditions mentioned is found, then the function should return None. 


  • This function takes an amount value as an input parameter. It then finds the Trade objects from the list of Trade objects of the Crypto class, with Amount more than the passed parameter. It then takes the names of all the coins from those trade objects with their count and then returns the same in the form of a dictionary, with coin name as Key and number of corresponding coins as Value. 



Sort the data in the dictionary in the alphabetic order of the coin names. Return the dictionary to the main method

 If no Trade object with amount more than the given amount is present in the list of Trade objects, then the method should return None

 Note: All string comparisons should be case insensitive


 Instructions to write main section of the code

  • 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 (Trade and Crypto) with the given sequence of arguments to fulfill the __int__method requirement defined in ALL the respective classes referring to the below instructions

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

 a. Read a number for the count of Trade objects to be created and added to the list.

 b. Create a Trade object after reading the data related to it and add the object to the list of Trade objects. This point repeats for the number of Trade objects (considered in the first line of input) point #c.i.a 

c. For creating the list of names of coins, an attribute in the Trade object 

1. Read a number for the count of coins in the Trade

 2. Read the value for coin name and add it to the list of coins. This point will repeat for the number of coins taken in above point.

 II. Create Crypto object by passing the List of Trade objects created in above point #c.as the argument.

 d. Read an integer value as input depicting the year to be provided as argument to the first function described above. 

 e. Read a float value depicting the amount of the Trade to be provided as argument to the second function described above.

 f. Call the methods described above from the main section with appropriate values as arguments from the main section

 g. Display the total amount returned by the first function, with a single decimal value. You may refer to the sample output for the display format. 

If None is returned by the function, then display the message 'No trade found with given criteria'(excluding the quotes)

 h. Display the coin name and the count from the dictionary returned by the second function. You may refer to the sample output for the display format. If function returns None, then display the message "No coin found with given criteria' (excluding the quotes). You can use/refer the below given sample input and output to verify your solution using Test against Custom Input option in HackerRank


.. Input Format for Custom Testing

a. The 1st input taken in the main section is the number of Trade objects to be added to the list of Trade, say x

 b. The next line of input is the id of the Trade.

 c. Next input denotes the amount of the trade

 d. Next line of input denotes number of coins present in the Trade say ‘n'

e. Next n lines of inputs are the coin names taken one after the other

. f. Next set of inputs are the date of Trade and type of the Trade ("Buy" or "Sell"). Point b to f is repeated for x times (where x is the number of Trade objects given in the first line of input). 

 g. The next line of input refers the year of Trade date required to be passed as argument to the first function

. h. The next line of input refers the amount required to be passed as argument to the second function.. 

You can consider below sample input and output to verify your implementation before submitting in Hackerrank 


Sample input 1:

5

101

4000

Bitcoin

 Etherium 

21/11/2021

 Buy

102

 8900

4

 Solana

 Cardano

 Etherium

 Bitcoin

 01/12/2021

 Buy

 103

 3500

 3  

Dogecoin

 Shiba Inu 

Bitcoin

 11/09/2020 

Sell

 104

 13000

 Polygon

 Etherium 

02/04/2021 

Buy

 105 

900 

Bitcoin

USDT

06/07/2014

Sell 

2021 

5000

 



 Output: 

12900.0

Bitcoin 1

Cardano 1

 Etherium 2

 Polygon 1

 Solana 1


 ALL Sample Input 2:

5

101 

4000  

2

Bitcoin

Etherium 

21/11/2021 

Buy

102

8900

 Solana

 Cardano

 Etherium 

Bitcoin 

01/12/2021

 Buy 

103

 3500 

3

 Dogecoin

 Shiba Inu 

Bitcoin

 11/09/2021

 Buy 

104 

13000 

2  

Polygon

 Etherium

 02/04/2020

 Sell 

105 

900

 2 

Bitcoin

 USDT 

06/07/2019

 Sell 

2021 

25000


 Output 

16400.0

No coin found with given criteria 





IF YOU WANT TO KNOW THE APPROACH TO SOLVE THE ABOVE QUESTION REFER TO THIS VIDEO:








HOPE THIS VIDEO IS HELPFUL. TO KNOW MORE ABOUT HOW TO APPROACH THIS KIND OF QUESTION DO FOLLOW. IF YOU HAVE ANY DOUBTS, DON'T HESTITATE TO COMMENT DOWN BELOW.

Post a Comment

0 Comments