Algorithm

Hi,

I am looking for the following..

Given an input

for example a

it shud generate..the following codes

a.1

b

Given a.1 it shud generate

a.1.a

a.2

b

Given a.10.a.1.c.a

it shud gerenate

a.10.a.1.c.a.1

a.10.a.1.c.b

a.10.a.1.d

a.10.a.2

a.10.b

a.11

b

i hope u get the idea//

it wud be nice if u cud give some inputs..

thanks

(alphabets and digits alternate)..always starts with alphabet..

[531 byte] By [rthi99a] at [2007-10-1 21:25:55]
# 1
the output generated shud be stroed in an arraylist..not just printing in the screen..
rthi99a at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 2
So how far have you got with your homework?
sabre150a at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 3
I am seriously working on it..
rthi99a at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 4

static ArrayList codeGen(String st)

{

int idx=0;

String sub="";

int len;

String s="";

ArrayList output=new ArrayList();

if(st.length()==1) s=st+"."+"0";

else s=st;

System.out.println("the length is"+s.length());

for(int i=s.length()-1;i>=0;i--)

{

idx=s.lastIndexOf(".");

if(idx==-1) {char c1=(char)(s.charAt(0)+1);System.out.println("comes here"+c1);output.add(c1+"");return output;}

sub=s.substring(idx+1);

System.out.println("the string sub is"+sub);

len=sub.length();

if(len==1)

{

char c=(char)(s.charAt(idx+1)+1);

output.add(s.substring(0,idx+1)+c);

s=s.substring(0,idx);

}

else if(len > 1)

{

int i1=Integer.parseInt(sub);

i1=i1+1;

String st1=i1+"";

output.add(s.substring(0,idx+1)+st1);

s=s.substring(0,idx);

}

}

return output;

}

the above is my program..the values returned are correct except that it doesnt return

for example

if input a.1

it returns a.2 and b but not a.1.a

rthi99a at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 5

> the above is my program..the values returned are

> correct except that it doesnt return

> for example

> if input a.1

> it returns a.2 and b but not a.1.a

I tried your code but everything comes out wrong.

I should try a different approach. Breaking up your method in smaller pieces would be more clearly. I find your method hard to follow like that.

You could try it like this:

import java.util.ArrayList;

public class StringGen {

/** main. */

public static void main(String [] args) {

String list = "a.10.a.1.c.a";

ArrayList arr = codeGen(list);

for(int i = 0; i < arr.size(); i++) {

System.out.println(arr.get(i));

}

}

/** Generate the String-list. */

private static ArrayList codeGen(String str) {

ArrayList output = new ArrayList();

if(lastIsNumerical(str)) { str += ".a"; }

else { str += ".1"; }

output.add(str);

while(hasPoints(str)) {

str = removeLast(str);

str = increaseLast(str);

output.add(str);

}

return output;

}

/** Removes the last element. */

private static String removeLast(String str) {

// ...

}

/** Increases the last element. */

private static String increaseLast(String str) {

// ...

// use the method lastIsNumerical(str) here.

// ...

}

/** Checks if the last element is a 'int'. */

private static boolean lastIsNumerical(String str) {

// ...

}

/** Checks if there's at least 1 point. */

private static boolean hasPoints(String str) {

// ...

}

} // class StringGen

Good luck.

prometheuzza at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 6
I got the program rt..thanks anyway..will take ur advise on breaking up the program into easier parts..thanlks
rthi99a at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...
# 7
> I got the program rt..> thanks anyway..will take ur advise on breaking up the> program into easier parts..> > thanlksYou're welcome.Good luck.
prometheuzza at 2007-7-13 3:21:35 > top of Java-index,Other Topics,Algorithms...