Wednesday 20 July 2016

Printing all the permutation of the string using recursion


           dog
   dog ,  odg ,  god    (in first call 1st character is swapped with all postions (1st to last))
dgo     ogd       gdo   (in 2nd call ,2nd character is swapped with all postions(2nd to last) )




#include<bits/stdc++.h>

using namespace std;

void permu(string k,int fst,int lst)
{
    if(fst==lst)
        cout<<k<<endl;

    for(int i=fst;i<=lst;i++)
    {
        swap(k[fst],k[i]);
        permu(k,fst+1,lst);
        swap(k[fst],k[i]);
    }
}

int main()
{
    string s;
    cin>>s;
    int lst=s.size()-1;
    permu(s,0,lst);
    return 0;
}

Reverse String using Recursion C++


Here the Logic is simple to print the last character of the string for each function call and again call the reverse function again ,this time the string doesn't contain last character.


#include<bits/stdc++.h>

using namespace std;

void reve(string k)
{

  int sz=k.size();
  if(sz==1)
    cout<<k[sz-1];
  else
  {
     cout<<k[sz-1];
     string p=k.substr(0,sz-1);
     reve(p);
  }
}

int main()
{
    string s;
    cin>>s;
    reve(s);

    return 0;
}

Friday 15 July 2016

LeadSquared Interview Questions

On 12th july , I got a chance to sit for the written round of Leadsquared drive.
In 1 hour we have to do 3 java questions.

1st question->Excel sheet problem
                      ex. 1 -> A , 2->B , 26->Z ,27->AA 

 import java.util.*;

public class excel
{
public static void main(String args[])
{
  Scanner s=new Scanner(System.in);
       int r,n=s.nextInt();
  String k=" ";
       while(n>0)
  {
r=n%26;
          if(r==0)
 {
String ch="Z";
            k=k+ch;
n=n/26 -1;
 }
          else
 {
 int ch=r-1+(int)'A';
 char p=(char)ch;
 String h= Character.toString(p);
              k=k.concat(h);    
     n=n/26;
 }
  }
 
  int len=k.length();
  for(int i=len-1;i>=0;i--)
  System.out.print(k.charAt(i));
       // System.out.println(k);  
}
}



2nd Question-> find a number just bigger than the given number, with the same digits combination.
                          example  : 756 -> 765  ,  342-> 423 , 987->"Not possible"


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string k;
    cin>>k;
    int l=k.length();
    int a[l];

    for(int i=0;i<l;i++)
     a[i]=k[i]-'0';

    int lst=a[l-1],fl=0,rem;

    for(int p=l-1;p>=1;p--)
    {
     for(int i=p-1;i>=0;i--)
     {
      if(a[p]>a[i])
       {
          fl=1;
          int tmp=a[i];
          a[i]=a[p];
          a[p]=tmp;
          rem=i+1;
          break;
       }
     }
     if(fl==1) break;
    }


    if(fl==0) cout<<"Not possible"<<endl;
    else
    {
         sort(a+rem,a+l);
        for(int i=0;i<l;i++) cout<<a[i];
        cout<<endl;
    }
    return 0;
}



3rd Question: Parenthesis Balance Checking Problem.
                       {([]{})}-> Balanced , {}() ->Balanced , {(})-> Not Balanced



import java.util.*;

public class balanced_parenthesis
{
public static void main(String arg[])
    {
 Scanner s=new Scanner(System.in);
      String k=s.nextLine();
      int l=k.length(),fl=0;
     
 Stack st = new Stack();
     
      for(int i=0;i<l;i++)
      {
        if(k.charAt(i)=='(' || k.charAt(i)=='{' || k.charAt(i)=='[')
         {
st.push(new Character(k.charAt(i)));
         }
        else if(k.charAt(i)==')' || k.charAt(i)=='}' || k.charAt(i)==']')
        {
          if(st.empty())
          {
              fl=1; break;
          }
 char a = (Character) st.pop();
          if((k.charAt(i)=='(' && a!=')') || (k.charAt(i)=='[' && a!=']') || (k.charAt(i)=='{' && a!='}'))
            {
                fl=1; break;
            }
       }
     }  

if(!st.empty() || fl==1)
System.out.println("Not balanced");
    else
        System.out.println(" balanced ");

}
}


Wednesday 6 July 2016

c++ Program to reverse the sentence

C++ Program to reverse the sentence

Input- "My name is Nikhil"
Output "Nikhil is name My"


Here the idea to reverse each word in 1st phase and in 2nd phase reverse the whole sentence.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    getline(cin,s);

    cout<<s<<endl;


    int l=s.size();

    int pre=0;
    for(int i=0;i<l;i++)
    {
      if(s[i]==' ')
      {
        int k=i-1;
        while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }

        pre=i+1;
      }
      else if(i==l-1)
      {
        int k=i;
        while(pre<k){char c=s[pre]; s[pre]=s[k]; s[k]=c; pre++; k--; }
      }
    }

    int bck=0;
    int nxt=l-1;

    while(bck<nxt)
    {
     char c=s[bck];
     s[bck]=s[nxt];
     s[nxt]=c;

     nxt--; bck++;
    }

    cout<<s<<endl;

    return 0;
}

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...