Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

generator sequencial strings

Thread view: 
Aryeh M. Friedman - 22 Oct 2007 02:17 GMT
For test data I need a string generator that will generate strings
such that the order will be:

a
b
....
z
aa
ab
....
az
ba
bb
....
zz
aaa
...
zzz
...

Does anyone know of a good way to do this?
Arne Vajhøj - 22 Oct 2007 02:27 GMT
> For test data I need a string generator that will generate strings
> such that the order will be:
[quoted text clipped - 17 lines]
>
> Does anyone know of a good way to do this?

public class WGen {
    private static void gen(char[] c, int ix, int n, char[] base) {
        if(ix < n) {
            for(int i = 0; i < base.length; i++) {
                c[ix] = base[i];
                gen(c, ix+1, n, base);
            }
        } else {
            System.out.println(new String(c));
        }
    }
    private static void gen(int n, char[] base) {
        char[] c = new char[n];
        gen(c, 0, n, base);
    }
    public static void main(String[] args) {
        char[] base = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        gen(1, base);
        gen(2, base);
    }
}

Arne
Aryeh M. Friedman - 22 Oct 2007 03:27 GMT
On Oct 21, 9:27 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > For test data I need a string generator that will generate strings
> > such that the order will be:
[quoted text clipped - 42 lines]
>
> Arne

First of thanks this very close to what I want and I played with it a
little to get it to do what I want... it needs to be able to produce 1
string per call to gen (I am using it to generate sequencial primary
keys to test a DB)
Arne Vajhøj - 22 Oct 2007 03:43 GMT
>>> For test data I need a string generator that will generate strings
>>> such that the order will be:
[quoted text clipped - 42 lines]
> string per call to gen (I am using it to generate sequencial primary
> keys to test a DB)

Do the insert where the System.out.println is. Or more sophisticated
add an extra argument to the gen methods which is an interface with
a method doing the processing.

Arne
rossum - 22 Oct 2007 16:34 GMT
>First of thanks this very close to what I want and I played with it a
>little to get it to do what I want... it needs to be able to produce 1
>string per call to gen (I am using it to generate sequencial primary
>keys to test a DB)
Have a look for a Java implementation of the C++ next_permutation()
STL method.  That seems to do exactly what you want.

rossum
Stefan Ram - 22 Oct 2007 14:22 GMT
>For test data I need a string generator that will generate strings
>such that the order will be:
[quoted text clipped - 3 lines]
>z
>aa

 See

http://www.purl.org/stefan_ram/java/Main.java

 . The file should be accessible under this URI temporarily,
 but at least for several days.
Roedy Green - 22 Oct 2007 18:02 GMT
I am not quite sure what you want.  However, these tools may prove
useful.

http://mindprod.com/jgloss/randomnumbers.html
http://mindprod.com/jgloss/permutation.html
http://mindprod.com/jgloss/combination.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Aryeh M. Friedman - 22 Oct 2007 19:32 GMT
After some trial and error and using the ideas from people posted here
I came up with a non-recursive one string per call solution (one small
annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
then str[str.length-2]++ when it should happen on z):

public class WGen
{
    public static void main(String[] args)
    {
        while(true)
            System.out.println(new String(getNext()));
    }

    private static char[] getNext()
    {
        if(str[str.length-1]>='z') {
            char[] tmp=new char[str.length+1];
            System.arraycopy(str,0,tmp,0,str.length);
            tmp[tmp.length-1]='a';
            str=tmp;
            str[0]='a'-1;
        }

        for(int i=0;i<str.length;i++) {
            if(str[i]>='z')
                str[i]='a'-1;

            str[i]++;

            if(str[i]<'z')
                break;
        }

        char[] out=new char[str.length];
        int j=0;

        for(int i=str.length-1;i>=0;i--)
            out[j++]=str[i];

        return out;
    }

    private static char[] str={'a'};
}
Aryeh M. Friedman - 22 Oct 2007 19:32 GMT
After some trial and error and using the ideas from people posted here
I came up with a non-recursive one string per call solution (one small
annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
then str[str.length-2]++ when it should happen on z):

public class WGen
{
    public static void main(String[] args)
    {
        while(true)
            System.out.println(new String(getNext()));
    }

    private static char[] getNext()
    {
        if(str[str.length-1]>='z') {
            char[] tmp=new char[str.length+1];
            System.arraycopy(str,0,tmp,0,str.length);
            tmp[tmp.length-1]='a';
            str=tmp;
            str[0]='a'-1;
        }

        for(int i=0;i<str.length;i++) {
            if(str[i]>='z')
                str[i]='a'-1;

            str[i]++;

            if(str[i]<'z')
                break;
        }

        char[] out=new char[str.length];
        int j=0;

        for(int i=str.length-1;i>=0;i--)
            out[j++]=str[i];

        return out;
    }

    private static char[] str={'a'};
}
Aryeh M. Friedman - 22 Oct 2007 19:32 GMT
After some trial and error and using the ideas from people posted here
I came up with a non-recursive one string per call solution (one small
annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
then str[str.length-2]++ when it should happen on z):

public class WGen
{
    public static void main(String[] args)
    {
        while(true)
            System.out.println(new String(getNext()));
    }

    private static char[] getNext()
    {
        if(str[str.length-1]>='z') {
            char[] tmp=new char[str.length+1];
            System.arraycopy(str,0,tmp,0,str.length);
            tmp[tmp.length-1]='a';
            str=tmp;
            str[0]='a'-1;
        }

        for(int i=0;i<str.length;i++) {
            if(str[i]>='z')
                str[i]='a'-1;

            str[i]++;

            if(str[i]<'z')
                break;
        }

        char[] out=new char[str.length];
        int j=0;

        for(int i=str.length-1;i>=0;i--)
            out[j++]=str[i];

        return out;
    }

    private static char[] str={'a'};
}
Aryeh M. Friedman - 22 Oct 2007 19:46 GMT
After some trial and error and using the ideas from people posted here
I came up with a non-recursive one string per call solution (one small
annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
then str[str.length-2]++ when it should happen on z):

public class WGen
{
    public static void main(String[] args)
    {
        while(true)
            System.out.println(new String(getNext()));
    }

    private static char[] getNext()
    {
        if(str[str.length-1]>='z') {
            char[] tmp=new char[str.length+1];
            System.arraycopy(str,0,tmp,0,str.length);
            tmp[tmp.length-1]='a';
            str=tmp;
            str[0]='a'-1;
        }

        for(int i=0;i<str.length;i++) {
            if(str[i]>='z')
                str[i]='a'-1;

            str[i]++;

            if(str[i]<'z')
                break;
        }

        char[] out=new char[str.length];
        int j=0;

        for(int i=str.length-1;i>=0;i--)
            out[j++]=str[i];

        return out;
    }

    private static char[] str={'a'};
}
Aryeh M. Friedman - 22 Oct 2007 19:58 GMT
After some trial and error and using the ideas from people posted here
I came up with a non-recursive one string per call solution (one small
annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
then str[str.length-2]++ when it should happen on z):

public class WGen
{
    public static void main(String[] args)
    {
        while(true)
            System.out.println(new String(getNext()));
    }

    private static char[] getNext()
    {
        if(str[str.length-1]>='z') {
            char[] tmp=new char[str.length+1];
            System.arraycopy(str,0,tmp,0,str.length);
            tmp[tmp.length-1]='a';
            str=tmp;
            str[0]='a'-1;
        }

        for(int i=0;i<str.length;i++) {
            if(str[i]>='z')
                str[i]='a'-1;

            str[i]++;

            if(str[i]<'z')
                break;
        }

        char[] out=new char[str.length];
        int j=0;

        for(int i=str.length-1;i>=0;i--)
            out[j++]=str[i];

        return out;
    }

    private static char[] str={'a'};
}
Arne Vajhøj - 27 Oct 2007 04:22 GMT
> After some trial and error and using the ideas from people posted here
> I came up with a non-recursive one string per call solution (one small
> annoyance [not critical] is if str.length>1 and str[str.length-1]>'y'
> then str[str.length-2]++ when it should happen on z):

I still think the recursive approach with a class going in is
best. See WGenFlex below.

But yes a non recursive version can be made. Also that produces
the exact same output. See WGenNonRec below.

Arne

===========================================

package october;

public class WGenFlex {
    public interface WGenProcesser {
        public void process(String w);
    }
    private static void gen(char[] c, int ix, int n, char[] base,
WGenProcesser p) {
        if(ix < n) {
            for(int i = 0; i < base.length; i++) {
                c[ix] = base[i];
                gen(c, ix+1, n, base, p);
            }
        } else {
            p.process(new String(c));
        }
    }
    private static void gen(int n, char[] base, WGenProcesser p) {
        char[] c = new char[n];
        gen(c, 0, n, base, p);
    }
    public static void gen(int n1, int n2, char[] base, WGenProcesser p) {
        for(int i = n1; i <= n2; i++) {
            gen(i, base, p);
        }
    }
    public static void main(String[] args) {
        gen(1, 2, "abcdefghijklmnopqrstuvwxyz".toCharArray(),
            new WGenProcesser() {
                public void process(String s) {
                    System.out.println(s);
                }
            });
    }
}

package october;

public class WGenNonRec {
    private int n;
    private char first;
    private char last;
    private char[] c;
    public WGenNonRec(int n, char first, char last) {
        this.n = n;
        this.first = first;
        this.last = last;
        c = new char[n];
        for(int i = 0; i < n - 1; i++) {
            c[i] = first;
        }
        c[n-1] = (char)(first - 1);
    }
    public String getWord() {
        int ix = n - 1;
        while(ix >= 0) {
            c[ix] = (char)(c[ix] + 1);
            if(c[ix] > last) {
                c[ix] = first;
                ix--;
            } else {
                return new String(c);
            }
        }
        return null;
    }
    private static void gen(int n1, int n2, char first, char last) {
        for(int i = n1; i <= n2; i++) {
            String w;
            WGenNonRec g = new WGenNonRec(i, 'a', 'z');
            while((w = g.getWord()) != null) {
                System.out.println(w);
            }
        }
    }
    public static void main(String[] args) {
        gen(1, 2, 'a', 'z');
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.