Last active
July 8, 2022 10:29
-
-
Save KhaledElshamy/cc04ca0b1d261b0bb940d2cf74aba9de to your computer and use it in GitHub Desktop.
Problem: Check brackets in the code (Data Structures course at coursera)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| int main() | |
| { | |
| string str1; | |
| int index=0,balance=true,a; | |
| char top; | |
| cin>>str1; | |
| stack<char>s1; | |
| while(index<str1.length()) | |
| { | |
| if (s1.empty()) | |
| a=index; | |
| if(str1[index]=='['||str1[index]=='{'||str1[index]=='(') | |
| s1.push(str1[index]); | |
| else | |
| { | |
| if((str1[index]==']'||str1[index]=='}'||str1[index]==')')) | |
| { | |
| if(s1.empty()) | |
| { | |
| balance=false; | |
| break; | |
| } | |
| else | |
| { | |
| top=s1.top(); | |
| s1.pop(); | |
| if(( top =='[' && str1[index]!=']')||( top=='{' && str1[index]!='}')||( top=='(' && str1[index]!=')')) | |
| { | |
| balance=false; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| index+=1; | |
| } | |
| if(balance&&s1.empty()) | |
| cout<<"Success"<<endl; | |
| else | |
| { | |
| if((index>=str1.size())&&!s1.empty()) | |
| cout<<a+1<<endl; | |
| else | |
| cout<<index+1<<endl; | |
| } | |
| return 0; | |
| } |
public static Decimal onboardingCostPricing(OpportunityLineItem oppItemObj, string productName , decimal licProductAddedNowPriceSum ,list<OpportunityLineItem> lineItemLst){
Integer NoOfOnboardingProductBefore = 0;
Integer NoOfOnboardingProductsNow = 0;
Integer TotalNoOfOnboardingProducts = 0;
decimal Additionalprice =0.0 ;
list<OpportunityLineItem> oppProductsToUpdate = new list <OpportunityLineItem>();
list<Pricing_Master__c > pricingMasterList = new list <Pricing_Master__c >();
Integer eachPercentage = 0;
Decimal acvTenPecentage = 0.0;
Decimal eachPrice = 0.0;
Decimal TotalLicSum = 0.0;
Decimal LicSumBefore = 0.0;
Decimal finalPrice2 = 0.0;
List<Opportunity> oppObjList =new List<Opportunity>();
String oppCurrency ;
// All Product Map
Map<id,Product2> allProductMap = new Map<id,Product2>([SELECT Id , Name ,Family,CurrencyIsoCode from Product2 ]);
string oppId ;
oppId = oppItemObj.OpportunityId ;
// CALCULATE HOW MANY ONBOARDING PRODUCTS ADDED EXCEPT CRM/CDP/THIRD PARTY
for(OpportunityLineItem oppLineItem : lineItemLst){
IF(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name !='CRM/CDP/Third Party' ){
NoOfOnboardingProductsNow = NoOfOnboardingProductsNow + 1 ;
}
}
//Iterate every line item THAT WAS ALREADY ADDED ON OPPORTUNITY EXCEPT CRM/CDP/THIRD PARTY
for(OpportunityLineItem oppLineItem :[select id,Name,Product_Family__c,OpportunityId,Product2Id from OpportunityLineItem where OpportunityId =:oppId ]){
If(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name !='CRM/CDP/Third Party'){
NoOfOnboardingProductBefore = NoOfOnboardingProductBefore + 1 ;
}
}
TotalNoOfOnboardingProducts = NoOfOnboardingProductBefore + NoOfOnboardingProductsNow ;
system.debug('NoOfOnboardingProductBefore == >'+NoOfOnboardingProductBefore);
system.debug('NoOfOnboardingProducts now == >'+NoOfOnboardingProductsNow);
system.debug('TotalNoOfOnboardingProducts == >'+TotalNoOfOnboardingProducts);
// FETCH License_Products_Cost_sum__c FROM OPPORTUNITY
oppObjList = [Select Id,Name,New_Volume_Email__c ,ACV__c,Type,License_Products_Cost_sum__c,CurrencyIsoCode,New_Volume_SMS__c,gaconnector_Country__c ,New_Volume_Whatsapp__c ,Subscription_Start_Date__c ,Subscription_End_Date__c,Vendor_Account__c,Vendor_Account__r.name ,Region__c,Whatsapp_Msg_Cost_Type__c from Opportunity
where Id =:oppId
];
// roll up sum of already added licence products
if(oppObjList.size()>0 && oppObjList[0].License_Products_Cost_sum__c >0 ){
LicSumBefore = oppObjList[0].License_Products_Cost_sum__c ;
oppCurrency = oppObjList[0].CurrencyIsoCode ;
}
TotalLicSum = LicSumBefore + licProductAddedNowPriceSum ;
// if opp type is upsell , add previous Acv also.
if(oppObjList[0].ACV__c > 0 && oppObjList[0].Type == 'Upsell'){
TotalLicSum = TotalLicSum + oppObjList[0].ACV__c ;
}
system.debug('LicSumBefore ==>' + LicSumBefore);
system.debug('licProductAddedNowPriceSum ==>' + licProductAddedNowPriceSum);
system.debug('TotalLicSum ==>' + TotalLicSum);
if(TotalLicSum > 0 ){
acvTenPecentage = TotalLicSum * 0.10 ;
if(TotalNoOfOnboardingProducts > 0){
eachPercentage = 100 / TotalNoOfOnboardingProducts ;
system.debug('eachPercentage ==>'+eachPercentage);
system.debug(' eachPercentage / 100 ==>'+ eachPercentage / 100);
eachPrice = (acvTenPecentage * eachPercentage) / 100 ;
system.debug('eachPrice ==>'+ eachPrice);
}
}
// update that onboarding products also which are added before
if(NoOfOnboardingProductBefore > 0 && eachPrice > 0 ){
//Iterate every line item THAT WAS ALREADY ADDED ON OPPORTUNITY
for(OpportunityLineItem oppLineItem :[select id,Name,Product_Family__c,OpportunityId,Product2Id from OpportunityLineItem where OpportunityId =:oppId ]){
If(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name !='CRM/CDP/Third Party'){
OpportunityLineItem lineItemobj = new OpportunityLineItem();
lineItemobj.Id = oppLineItem.Id ;
lineItemobj.UnitPrice = eachPrice ;
oppProductsToUpdate.add(lineItemobj);
}
}
// update onboarding products
try
{
system.debug('oppProductsToUpdate ==> '+oppProductsToUpdate);
if(oppProductsToUpdate.size()>0){
update oppProductsToUpdate;
}
}catch(DmlException e)
{
System.debug('The following exception has occurred: '+ e.getMessage());
}
}
// Calculation of Onboarding Products If Partner,Platform is chosen ,
// THEN PRICE WILL BE HALF + Additional Charge
if( oppItemObj.Platform__c != Null && productName =='Website'){
// Case when partner not chosen ,but platform is custom and product is website then we have to add some additional cost
// by fetching price master
if(oppItemObj.Partner__c == Null && oppItemObj.Platform__c == 'Custom'){
eachPrice = eachPrice * 0.5 ;
System.debug('website price when Custom ==>'+eachPrice);
// Fetch pricing master
pricingMasterList = [select Id,Partner__c, Platform__c, Price__c ,CurrencyIsoCode,RecordType.Name from Pricing_Master__c where RecordType.DeveloperName ='Onboarding_Integration_Cost' and Platform__c ='Custom' ];
if(pricingMasterList.size()>0 && pricingMasterList.size() == 1){
system.debug('Unique Price Found ');
// Currency conversion
String defaultCurrency = [SELECT IsoCode FROM CurrencyType WHERE IsCorporate = true].IsoCode;
List<CurrencyType> currencyList = new List<CurrencyType> ();
map<string,decimal> ISoWithRatesMap = new map<string,decimal>();
Decimal conversionRate = 0.0;
String pricingMasterCurrency = pricingMasterList[0].CurrencyIsoCode;
Additionalprice = pricingMasterList[0].Price__c;
System.debug('DefaultCurrencyCode-->' +defaultCurrency);
System.debug('pricingMasterCurrency-->' +pricingMasterCurrency);
System.debug('oppCurrency-->' +oppCurrency);
//check if opp and pricing master currency are same
if(oppCurrency == pricingMasterCurrency && Additionalprice != 0.0){
}
else if (oppCurrency != pricingMasterCurrency && oppCurrency !=null && Additionalprice != 0.0 ){
system.debug('Additionalprice ==>'+Additionalprice);
//opp and pricing master currency are diff ,converting price acc to rate
currencyList = [SELECT Id,ISOCode,ConversionRate, DecimalPlaces,IsCorporate FROM CurrencyType WHERE IsActive=TRUE];
if(currencyList.size()>0){
for(CurrencyType currencyobj :currencyList){
ISoWithRatesMap.put(currencyobj.ISOCode,currencyobj.ConversionRate);
}
system.debug('ISoWithRatesMap==>'+ISoWithRatesMap);
//Convert acc to pricing master and opp currency
if( ISoWithRatesMap.containskey(oppCurrency)){
conversionRate = ISoWithRatesMap.get(oppCurrency);
decimal tempRate = ISoWithRatesMap.get(pricingMasterCurrency);
system.debug('conversionRate==>'+conversionRate);
system.debug('tempRate==>'+tempRate);
Additionalprice = Additionalprice * (conversionRate / tempRate ) ;
system.debug('Additionalprice in currency'+oppCurrency+'==>'+Additionalprice);
}
}
}
system.debug('Additionalprice after conversion ==>'+Additionalprice);
}
}
}
// Case when partner, platform Is chosen and product is website then we have to add some additional cost
// by fetching price master
if(oppItemObj.Partner__c != Null && oppItemObj.Platform__c != Null ){
String Partner ;
list <Account> acclist = new list<Account>();
acclist = [select Id ,Name from Account where Id =:oppItemObj.Partner__c Limit 1] ;
system.debug('acclist.size()==>'+acclist.size());
if ( acclist.size()>0){
Partner = acclist[0].Name;
// Fetch pricing master
pricingMasterList = [select Id,Partner__c, Platform__c, Price__c ,CurrencyIsoCode,RecordType.Name from Pricing_Master__c where RecordType.DeveloperName ='Onboarding_Integration_Cost'
and Platform__c =:oppItemObj.Platform__c
and Partner__c =:Partner ];
if(pricingMasterList.size()>0 && pricingMasterList.size() == 1){
system.debug('Unique Price Found ');
// Currency conversion
String defaultCurrency = [SELECT IsoCode FROM CurrencyType WHERE IsCorporate = true].IsoCode;
List<CurrencyType> currencyList = new List<CurrencyType> ();
map<string,decimal> ISoWithRatesMap = new map<string,decimal>();
Decimal conversionRate = 0.0;
String pricingMasterCurrency = pricingMasterList[0].CurrencyIsoCode;
Additionalprice = pricingMasterList[0].Price__c;
System.debug('DefaultCurrencyCode-->' +defaultCurrency);
System.debug('pricingMasterCurrency-->' +pricingMasterCurrency);
System.debug('oppCurrency-->' +oppCurrency);
//check if opp and pricing master currency are same
if(oppCurrency == pricingMasterCurrency && Additionalprice != 0.0){
}
else if (oppCurrency != pricingMasterCurrency && oppCurrency !=null && Additionalprice != 0.0 ){
system.debug('Additionalprice ==>'+Additionalprice);
//opp and pricing master currency are diff ,converting price acc to rate
currencyList = [SELECT Id,ISOCode,ConversionRate, DecimalPlaces,IsCorporate FROM CurrencyType WHERE IsActive=TRUE];
if(currencyList.size()>0){
for(CurrencyType currencyobj :currencyList){
ISoWithRatesMap.put(currencyobj.ISOCode,currencyobj.ConversionRate);
}
system.debug('ISoWithRatesMap==>'+ISoWithRatesMap);
//Convert acc to pricing master and opp currency
if( ISoWithRatesMap.containskey(oppCurrency)){
conversionRate = ISoWithRatesMap.get(oppCurrency);
decimal tempRate = ISoWithRatesMap.get(pricingMasterCurrency);
system.debug('conversionRate==>'+conversionRate);
system.debug('tempRate==>'+tempRate);
Additionalprice = Additionalprice * (conversionRate / tempRate ) ;
system.debug('Additionalprice in currency'+oppCurrency+'==>'+Additionalprice);
}
}
}
system.debug('Additionalprice after conversion ==>'+Additionalprice);
}
}
}
if(Additionalprice > 0){
eachPrice = eachPrice + Additionalprice ;
system.debug('website price after adding additonal price ==>'+eachPrice);
Additionalprice =0.0 ;
}
//********************************************************************************
//IF PRODUCT IS Additional Onboarding Support (Days) THEN CALCULATIONS WILL BE DIFF
// IS WILL BE BASED ON ONBOARDING SUM ,ADDON SUM ( EXCEPT THIS ONE ) / TIME FRAME
// *******************************************************************************
if( productName =='Additional Onboarding Support (Days)'){
integer Timeframe = 0;
Boolean firstTimeApp = True ;
List<OpportunityLineItem> lineItemLst2 = [select Id ,OpportunityId, Product2.Family, Product2.Name from OpportunityLineItem where (Product2.Family ='Onboarding' or Product2.Family ='Onboading Add on')
AND OpportunityId =:oppId];
// Adding previous and Now opp products together so that we can Calculate Time Frame
lineItemLst2.addAll(lineItemLst);
system.debug('lineItemLst2==>'+lineItemLst2);
system.debug('lineItemLst2 Size ==>'+lineItemLst2.size());
// TIME FRAME CALCULATIONS
For(OpportunityLineItem oppLineItem : lineItemLst2 ){
// For website it is 30
if(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name =='Website' ){
Timeframe = Timeframe + 30 ;
}
// For app(Ios/Android) it is 15
if(oppLineItem.Product_Family__c == 'Onboarding' && firstTimeApp && allProductMap.ContainsKey(oppLineItem.Product2Id) && ( allProductMap.get(oppLineItem.Product2Id).Name =='App (Android)'||allProductMap.get(oppLineItem.Product2Id).Name =='App (iOS)') ){
Timeframe = Timeframe + 15 ;
firstTimeApp = False ;
}
// For CRM/CDP/Third Party it is 15
if(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name =='CRM/CDP/Third Party' ){
Timeframe = Timeframe + 15 ;
}
// Additional Onboarding Support (Days) it is 15
if(oppLineItem.Product_Family__c == 'Onboarding' && allProductMap.ContainsKey(oppLineItem.Product2Id) && allProductMap.get(oppLineItem.Product2Id).Name =='Additional Onboarding Support (Days)' ){
Timeframe = Timeframe + 15 ;
}
}
// check Timeframe min 30 , max it 60
if(Timeframe < 15 ){
Timeframe = 30 ;
}else if(Timeframe > 60){
Timeframe = 60 ;
}
system.debug('Timeframe ==> '+ Timeframe);
}
finalPrice2 = eachPrice ;
eachPrice = 0.0 ;
system.debug('finalPrice2 ==>'+finalPrice2);
return finalPrice2 ;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks bro.