Wednesday 27 March 2013

There is a sequence where alphabets are written like this.. a,b,c,d,.......,x,y,z,aa,ab,ac........,az,ba,bb,bc,bd......bz,ca,cb.........cz........,aaa,aab,aac.....aaz,............zzz,aaaa...........zzzz..... and so on..WAP to find out the string value at kth position. like if k= 28 the string on 28 will be "ab".


/*
 * This question is picked from Amazon Interview question on career cup.
 * The question can also be rephrased as convert number from decimal (base 10) number system to base 26 number system.
 *
 * ALGORITHM
 * 1. Get input number in variable “num”
 * 2. Call ConvertDecimal function and pass “num-1” and “base = 26” :
 * a. Initialize “len” = No. of digits, base 26 representation of number num will occupy.
 * b. Create an integer array of “len” number of elements and store each digit of base26 representation of num in this array.
 * 3. Initialise i=len and while i>=0
 * 4. Print character (base26[i]+97).
 *
 * PS:
 * we converted num-1 to base 26 and not num. consider num = 26  when we convert it to base26 it is 10
 * in our base26 representation 0 = a and 1=b so it will print ba but it should print z.
 * so to produce desire result we pass num-1 and not num.
 * if num = 26 we convert 25 to base26 which is z.  
 * It will always produce the desire result.
 */

  int getLength(int number, int base)
  {
      int length = 0;
      while(number > 0 )
      {
                   number /= base;
                   length++;
      }  
      return length;
  }
 
  int* convertDecimal(int toBase, int number)
  {
       int len = getLength(number,toBase);
       int *base26 = new int[len];
       int remainder;
       for(int i=0; i < len ; i++)
       {
                       base26[i] = number % toBase;
                       number /= toBase;
       }
       return base26;
  }
 
  int main()
  {
      int number ;
      cout<<"Enter the number : ";
      cin>>number;
      int *base26 = convertDecimal(26,number-1);
      int length = getLength(number-1,26);
      for(int i=length-1; i>=0; i--)
      {
              cout<<"  "<<char(97+base26[i]);
      }
      cout<<"\n\n";
      system("pause");
  }

3 comments:

  1. #include
    using namespace std;
    void sequence(int n,string &s)
    {
    int t;
    char ch;
    while(n>26)
    {
    t=n/26;
    ch=96+t;
    s+=ch;
    n=n%26;
    }
    ch=97+n;
    s+=ch;
    }
    int main()
    {
    int n;
    string s="";
    cout<<"enter the number. \n";
    cin>>n;
    sequence(n-1,s);
    cout<<s;
    return 0;
    }

    //even this works .... can u kindly check if any mistake..
    chaitanya

    ReplyDelete
  2. sorry, there's a bug... will post the crctd answer soon..

    ReplyDelete
  3. #include

    void tobase26(int n)
    {
    int mod;
    if(n<0)
    return ;
    mod=n%26;
    tobase26((n/26)-1);
    printf("%c",97+mod);
    }

    int main()
    {
    system("clear");
    int num,mod;
    printf("Enter the num : ");
    scanf("%d",&num);
    printf("Required format is : ");
    tobase26(num-1);
    printf("\n");
    return 0;
    }

    ReplyDelete