Anjan
Anjan

Reputation: 31

how to create alphabets like in excel coloumns

I try to create a Alphabet series like a,b,c,...z,aa,ab,ac,ad,ae,af,...,az,ba,bb,bc,...,bz,..zz,aaa,aab,aac..up to given number(N)

but when i try to create it using for loops, i have created one for loop each second character in the element that means to print a..z - loop1, aafor every second letter like a , i write a new for loop

is it is possible to create this type series with for loops...

thanks in advance...

Upvotes: 0

Views: 950

Answers (3)

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

Here is how to do it:

    public static string GetExcelColumnName(int index)
    {
        int d = index;
        string name = "";
        int mod;

        while (d > 0)
        {
            mod = (d - 1) % 26;
            name = Convert.ToChar('a' + mod).ToString() + name;
            d = (int)((d - mod) / 26);
        }

        return name;
    }

    public static IEnumerable<string> GetExcelColumns(int n) {
        for (int i = 1; i <= n; i++)
        {
            yield return GetExcelColumnName(i);
        }
    }

    static void Main(string[] args)
    {
        int i = 1;
        foreach (var item in GetExcelColumns(900))
        {
            Console.Write(i++ + ": " + item + " ");
        }
        Console.WriteLine();
    }

Explanation of GetExcelColumnName:

//Suppose index = 1, `GetExcelColumnName` should return `"a"`.
    d = 1
    d > 0
        mod = (d - 1) % 26; //mod now is 0
        Convert.ToChar('a' + mod); // => That's 'a'
        //name now is "a"
        d = (d - mod) / 26 = (1 - 0) / 26 = 0 // d is now 0
    //End of While
    method returns "a"

//Suppose index = 28, `GetExcelColumnName` should return `"ab"`.
    d = 28
    d > 0
        mod = (d - 1) % 26; //mod now is 1 (27 modulo 26)
        Convert.ToChar('a' + mod); // => That's 'b'
        //name now is "b"
        d = (d - mod) / 26 = (28 - 1) / 26 = 1 // d is now 1
    d > 0
        mod = (d - 1) % 26; //mod now is 0 (0 modulo 26)
        Convert.ToChar('a' + mod); // => That's 'a'
        //name now is "ab"
        d = (d - mod) / 26 = (1 - 0) / 26 = 0 // d is now 0
    //End of While
    method returns "ab"

Upvotes: 2

zmbq
zmbq

Reputation: 39013

I think the right way to look at this, is as base-27 numbers, and skipping all numbers with 0. Your digits are _abcdefghijklmnopqrstuvwxyz (_ having the value 0, z having the value 26), and you create the base 27 numbers like this:

nums_generated = 0
next_num = 1
while nums_generated < N:
   number_string = generate_base_27(next_num)
   if not '_' in number_string:
       print number_string
       num_generated = num_generated + 1
   next_num = next_num + 1

That should do the trick. For creating base-27 numbers with a given character set, look for example here , and make the proper corrections as you need base 27.

Upvotes: 0

Mukul Joshi
Mukul Joshi

Reputation: 324

This is very simple with lists and comprehensions

For example in python

alpha = ['a','b','c','d','e','f','g','h','i']

def merge(x,y):
    new = [ a+b for a in x for b in y]
    return new 

print(merge(alpha,alpha))
print(merge(alpha,merge(alpha,alpha)))

will give you what you want. In C# you can find an equivalent (Or in F#)

Upvotes: 0

Related Questions