Google Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
It works!
However instead 64, it should be 65, else the Z doesn't appear in the latter combinations.
Another possible solution(Python code)
num=int(raw_input())
result=""
while(num>=0):
t=num%26
result=chr(ord("A")+t)+result
num=num/26 -1
print result
@somebody, is your comment to reffer my code..
then,if counting start from 0 then 52 is BA
or if counting start from 1 then its AZ
my code will give BA for 52(because I start counting from 0)
if u want to start counting from 1....just add num=num-1 to my code after raw_input() line
please comment if my code give wrong answer
"For 52 it returns BZ where as the it should be AZ."
It's because all cells containing Z have a trick.
Here's my solution in C#:
using System;
using System.Collections.Generic;
using System.Text;
namespace _0_2_ExcelCells
{
class Program
{
static void Main(string[] args)
{
ExcelHelper helper = new ExcelHelper();
Console.WriteLine(helper.IntToCell(1));
Console.WriteLine(helper.IntToCell(20));
Console.WriteLine(helper.IntToCell(33));
Console.WriteLine(helper.IntToCell(47));
Console.WriteLine(helper.IntToCell(52));
Console.WriteLine(helper.IntToCell(53));
Console.WriteLine(helper.IntToCell(16224));
}
}
class ExcelHelper
{
Stack<char> _letters = new Stack<char>();
public string IntToCell(int i)
{
_letters.Clear();
GetNextLetter(i);
return GetCell();
}
private string GetCell()
{
StringBuilder sb = new StringBuilder();
while (_letters.Count > 0)
{
sb.Append(_letters.Pop());
}
return sb.ToString();
}
private void GetNextLetter(int i)
{
if (i == 0)
{
//Console.WriteLine();
return;
}
int m = i % 26;
int n = (int) i / 26;
if (m == 0) // There's a trick with Z cells
{
m = 26;
n--;
}
char letter = (char)(m + 64); // 64 is 'A' - 1
_letters.Push(letter);
GetNextLetter(n);
}
}
}
Java Implementation:
public class ReplaceNumberWithExcelLabel {
public static String convertToExcelLabel (int n) {
String result = "";
int index = 0;
while (n > 0) {
index = (n - 1) % 26;
result = Character.toChars(index + 65)[0] + result;
n = (n - 1) / 26;
}
return result;
}
public static void main(String[] args) {
System.out.println(convertToExcelLabel(1));
System.out.println(convertToExcelLabel(55));
}
}
@Adnan Ahmad,
Could you explain what is happening here? Character.toChars(index + 65)[0] + result;
what is the deal with [0] index array?
public class Excel {
public static char column(int n) {
int d, r;
int A = 'A';
int Z = 'Z';
int numLetters = Z - A + 1;
char s = ' ';
while (n > 0) {
d = (n - 1) / numLetters;
r = (n - 1) % numLetters;
s += A + r;
n = d;
}
return Character.toUpperCase(s);
}
public static void main(String[] args) {
for (int i = 1; i <= 26; i++) {
System.out.println(column(i));
}
for (int i = 1; i <= 26; i++)
for (int j = 1; j <= 26; j++)
System.out.println(column(i) + "" + column(j));
for (int i = 1; i <= 26; i++)
for (int j = 1; j <= 26; j++)
for (int k = 1; k <= 26; k++)
System.out.println(column(i) + "" + column(j) + ""
+ column(k));
}
}
public static String numToColumn(int number){
StringBuilder sb = new StringBuilder();
char c;
char s=0;
int n = number;
while(n > 0){
c = (char) ((n - 1) %26);
s = (char) ((c+65));
n = (n-c)/26;
sb.append(s);
}
return sb.toString();
}
final static char[] alpha = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
static int LENGTH = alpha.length;
public static String generatevariable(int a) {
int t = a;
StringBuffer s = new StringBuffer("");
if (a == 0)
return "";
if (a <= LENGTH)
s.append(alpha[a - 1]);
else {
s.append(generatevariable((a - 1) / LENGTH)); // use recursion
s.append(generatevariable(a - ((a - 1) / LENGTH) * LENGTH));
}
return s.toString();
}
static char [] chars = new char[27];
static {
char c = 'A';
for(int i=0;i<26; i++)
chars[i] = c++;
}
static String getExcelValue(int num) {
return getExcel(num-1);
}
static String getExcel(int num) {
int rem = num % 26;
int quot = num / 26;
if(quot > 0)
return getExcel(quot-1) + chars[rem];
return ""+chars[rem];
}
public class ExcelColumn
{
public static void printColumnName(int n) {
if(n/26>0) { // recurse
printColumnName(n/26-1);
}
//print character
System.out.print(Character.toString((char)('A'+n%26)));
}
public static void main(String[] args) {
printColumnName(Integer.parseInt(args[0])-1);
}
}
public void findXLcolumnByNumber(int number){
List<Character> lst = new ArrayList<Character>();
while (number > 0){
int b = number % 26;
number = number / 26;
b = b == 0 ? 26 : b;
lst.add((char) (b + 64));
}
for (int i = lst.size() - 1; i >= 0; i--)System.out.print(lst.get(i));
System.out.println();
}
in c++ code
void columeInExcel(int number)
{
string dic=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vector<char> result;
while(number)
{
int low = number%26;
int high = number/26;
if(high == 1 && low == 0)
{
result.push_back('z');
break;
}
result.push_back(dic[low]);
number = high;
}
for(int i = result.size()-1; i>=0; i--)
cout << result[i];
cout << endl;
}
public class ExcelColumns {
public static String getExcelColumnName(int n){
if(n < 1) return null;
StringBuilder sb = new StringBuilder();
while(n > 0){
sb.append((char)('A'+(n-1)%26));
n= (n-1)/26;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
for (int i = 1; i < 5000; i++) {
System.out.print(getExcelColumnName(i)+",");
}
}
}
public class ExcelColumns {
public static String getExcelColumnName(int n){
if(n < 1) return null;
StringBuilder sb = new StringBuilder();
while(n > 0){
sb.append((char)('A'+(n-1)%26));
n= (n-1)/26;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
for (int i = 1; i < 5000; i++) {
System.out.print(getExcelColumnName(i)+",");
}
}
}
- muntean.jenea September 25, 2013