Implement a simplified version of a crypto broker platform. Initially, there is an array of users, with each user depositing only cash - deposits[i] is the amount of cash the (i + 1)th user has deposited.
Assume that for now the platform contains just a single stock and all operations are done with this stock only. All operations are supposed to be in the same currency, and currency exchange is not supported for now.

The platform should support the following 3 types of operations:

"deposit " - deposits money to the specified user account.
"buy " - the specified user buys the specified amount of crypto at the specified price. The platform should make sure the user has enough money in their account to process the operation.
"sell " - the specified user sells the specified amount of crypto at specified price. The platform should make sure the user has enough cryptos in their account to process the operation.
Your task is to return an integer array representing the total amount of money in the specified user's account after each operation.

Example

For deposits = [9, 7, 12] and

operations = [
"buy 1 3 2",
"sell 1 4 10",
"deposit 2 12",
"buy 2 10 2",
"buy 2 6 3"
]

the output should be cryptoTrading(deposits, operations) = [3, 3, 19, 19, 1].

Let's consider all operations:

The user with user_id = 1 wants to buy 3 shares of the stock at price = 2 - the required amount of money is needed_money = 3 * 2 = 6. The user has 9 ≥ 6 units of money in their account, so this operation will be processed successfully. After this operation, the user has 3 units of money remaining.
The user with user_id = 1 wants to sell 4 shares of the stock at price = 10 - this operation will not be processed successfully because the user does not have 4 shares of the stock. Since the operation is not processed successfully, the user still has 3 units of money remaining.
The user with user_id = 2 deposits 12 units of money into their account. After this operation, the user has 19 units of money.
The user with user_id = 2 wants to buy 10 shares the stock at price = 2 - the required amount of money is needed_money = 10 * 2 = 20. The user has 19 < 20 units of money in their account, so this operation will not be processed successfully. The user still has 19 units of money remaining.
The user with user_id = 2 wants to buy 6 shares of the stock at price = 3 - the required amount of money is needed_money = 6 * 3 = 18. The user has 19 ≥ 18units of money, so this operation will be processed successfully. After this operation, the user has 1 unit of money remaining.
So, the output is [3, 3, 19, 19, 1].

Input/Output

[execution time limit] 0.5 seconds (cpp)

[input] array.integer deposits

An array of integers representing the amount of money (in cash) that users deposit to their accounts.

Guaranteed constraints:
1 ≤ deposits.length ≤ 104,
1 ≤ deposits[i] ≤ 105.

[input] array.string operations

An array of strings representing the operations described above. Specifically, it's guaranteed that:

In buy and sell operations - amount and price are integers.
In all operations - user_id will be integers between 1 to deposits.length.
After each operation, the total amount of money in each user's account will be an integer.
[output] array.integer

An array of integers where the ith element of the array represents the total amount of money in the specified user's account after the ith operation.

CAN YOU USE python to solve this def cryptoTrading(deposits, operations) :


Answer :

To implement a simplified version of a crypto broker platform. Use #include<bits/stdc++.h>

using namespace std;

//This is cryptoTrading function

vector<int> cryptoTrading(vector<int> deposits, vector<string> operations) {

   //res vector will store the final result

   vector<int>res;

   //This unordered map value will store the current deposit of each person

   unordered_map<int,int>value;

   //This unordered map value will store the current number of stock of each person

   unordered_map<int,int>stock;

   //First traverse the deposits vector

   for(int i=0;i<deposits.size();i++){

       //Here,person number which is (i+1) is key, and it's corresponding deposit is value

       value[(i+1)]=deposits[i];

   }

   //Now start traversing the operations vector

   for(int i=0;i<operations.size();i++){

       //task string will store the operation to perform, initially it is an empty string

       string task="";

       //index variable will keep track the spaces in sentence

       int index=-1;

       //Now start traverse the current operation sentence

       for(int j=0;j<operations[i].length();j++){

           //It will stop when a space is found

           if(operations[i][j]==' '){

               //And store the next index of that space in index variable

               index=j+1;

               break;

           }

           //Otherwise concatenate the character with task

           task=task+operations[i][j];

       }

       //If task is "buy" then

       if(task=="buy"){

           //person variable will store the person number

           int person=0;

           //Again traverse the operation sentence, from index

           for(int j=index;j<operations[i].length();j++){

               //It will stop when a space is found

               if(operations[i][j]==' '){

                     //store the next index of that space in index variable

                   index=j+1;

                   break;

               }

               //Otherwise add numbers with person variable

               person=(person*10)+(operations[i][j]-'0');

           }

           //num will store the number of stock to buy

               }

                //Otherwise add numbers with num variable

               num=(num*10)+(operations[i][j]-'0');

           }

            //val will store the value of that stock

           int val=0;

           //Repeat the same operations

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   break;

               }

               //add numbers with val

               val=(val*10)+(operations[i][j]-'0');

           }

           //if that person's current deposit is less than (num*val) then

           if(value[person]<(num*val)){

               //push that person's current deposit in res vector

               res.push_back(value[person]);

           }

           //Otherwise

           else if(value[person]>=(num*val)){

               //subtract (num*val) from that person's current deposit

               value[person]=value[person]-(num*val);

               //And add num with person's current stock

                stock[person]=stock[person]+num;

                //push that person's current deposit in res vector

               res.push_back(value[person]);

           }

       }

        //If task is "sell" then

       else if(task=="sell"){

           //Repeat the same process to find person's number

           int person=0;

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   index=j+1;

                   break;

               }

               person=(person*10)+(operations[i][j]-'0');

           }

           //Repeat the same process to find number of stocks

           int num=0;

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   index=j+1;

                   break;

               }

               num=(num*10)+(operations[i][j]-'0');

           }

           //Repeat the same process to find value of stock

           int val=0;

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   break;

               }

               val=(val*10)+(operations[i][j]-'0');

           }

            //if that person's current stock is less than num then

           if(stock[person]<num){

               //push that person's current deposit in res vector

               res.push_back(value[person]);

           }

           //Otherwise

           else if(stock[person]>=num){

              //subtract num from that person's current stock

               stock[person]=stock[person]-num;

               //And add (num*val) with person's current deposit

               value[person]=value[person]+(num*val);

               //push that person's current deposit in res vector

               res.push_back(value[person]);

           }

       }

       //If task is "deposit" then

       else if(task=="deposit"){

           //Repeat the same process to find person's number

           int person=0;

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   index=j+1;

                   break;

               }

               person=(person*10)+(operations[i][j]-'0');

           }

           //Repeat the same process to find value to deposit

           int val=0;

           for(int j=index;j<operations[i].length();j++){

               if(operations[i][j]==' '){

                   break;

   }

   //At the end return res vector

   return res;

}

//This is main function

int main()

{

//Do a sample run of above method

vector<int>deposits{9,7,12};

vector<string>operations{"buy 1 3 2","sell 1 4 10","deposit 2 12","buy 2 10 2","buy 2 6 3"};

vector<int>res=cryptoTrading(deposits,operations);

//Display the result

cout<<"Result: ";

for(int i=0;i<res.size();i++){

   cout<<res[i]<<" ";

}

return 0;

}

To learn more about crypto

https://brainly.com/question/15084188

#SPJ4

View image ASMASAADATKHAN