10 Replies - 860 Views - Last Post: 13 November 2009 - 07:34 PM Rate Topic: -----

#1 Perogy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 09

Character generation algorithm

Posted 13 November 2009 - 03:54 PM

I'm usually pretty good with these types of problems but this one has really got me stumped. I basically have to generate a list of strings of letters (up to 5 characters long) in sequential order where the letters going down the string are in ascending order and NOT equal to the letter before it. Don't know what I'm talking about do you? Here's an example which should make more sense :-)

I need to generate the letters in this way:

1) a
2) b
3) c
4) d
5) e
.
.
.
26) z
27) ab
28) ac
29) ad
.
.
.
51) az
52) bc
53) bd
54) be
.
(and this goes all the way down to...)
.
.
83681) vwxyz

So basically how I've done it so far is put all the letters from a-z into a set (the alphabet so to speak) and then I'm iterating over that alphabet, but I'm really stumped on the algorithm to generate the letters in this pattern. Another problem I had is if I do something like this for example:

Iterator it = alphabet.iterator();
Iterator it2 = it;

it2.next();



If I do this it seems that my it2.next() will increment not only "it2", but "it" as well. I assume this is because with iterators if you use the assignment operator= it assigns the actual address to that iterator. Is there any way around this so I could assign iterator it2 with the position of iterator it, but be able to increment them seperately? I think if I could do that it would allow me to figure out this algorithm much easier.

Maybe I am taking a completely wrong approach to this, anyways I'm sure the geniuses here will have some good ideas :-). Any help greatly appreciated.

This post has been edited by Perogy: 13 November 2009 - 03:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Character generation algorithm

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Character generation algorithm

Posted 13 November 2009 - 04:21 PM

does the list end at zzzzz
Was This Post Helpful? 0
  • +
  • -

#3 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 08-April 09

Re: Character generation algorithm

Posted 13 November 2009 - 04:26 PM

Alright, see how this does for you:

• All chars have a getNumericValue method that returns the Unicode number of the char.

• Try entering

char test = new char(65);
System.out.println(test);


and see what happens.

Basically, you just want a for loop with five char values, and have the last chars print null on every line until you hit the part of the list where they become letters.

This post has been edited by SayMoi: 13 November 2009 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 Perogy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 09

Re: Character generation algorithm

Posted 13 November 2009 - 04:36 PM

View PostSayMoi, on 13 Nov, 2009 - 03:26 PM, said:

Alright, see how this does for you:

• All chars have a getNumericValue method that returns the Unicode number of the char.

• Try entering

char test = new char(65);
System.out.println(test);


and see what happens.

Basically, you just want a for loop with five char values, and have the last chars print null on every line until you hit the part of the list where they become letters.



Compiler error, with that code you posted... Seems that char doesnt take a constructor. As for what you said there, I don't think I am quite understanding.. Wouldn't I have to have 5 for loops???

View Postsakshamkum, on 13 Nov, 2009 - 03:21 PM, said:

does the list end at zzzzz


nope, vwxyz

This post has been edited by Perogy: 13 November 2009 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 Ostralis  Icon User is offline

  • #include <greeter>
  • member icon

Reputation: 9
  • View blog
  • Posts: 557
  • Joined: 13-December 08

Re: Character generation algorithm

Posted 13 November 2009 - 04:38 PM

Interesting algorithm and approach. What you'll want to do is assign the Iterators separately, as such:
Iterator it = alphabet.iterator();
Iterator it2 = alphabet.iterator();


Now, within the algorithm itself, you're going to need to continually check to see if the next position is != null, or, not null (you'll be checking to see if there's a character next in the alphabet), as such:
while (it.hasNext() && it2.hasNext())


You'll then go from there and work on your algorithm, wherein, you can set it2 as it:
it2 = it;


Good luck on the code, I can definitely help you with more algorithm work regarding this problem if that's what you're looking for, but it's hard for me to explain without giving you the code.

Actually, I'll write some pseudocode.

You're going to need to have a few loops and while statements. I understand your alphabet is a LinkedList, though, with the external spot where you're placing the Strings, is that an ArrayList? If so, you're going to need to firstly check that the ArrayList index is not taken by 5 elements. Secondly, you're going to begin by saying while it.hasNext(), store the elements in the ArrayList, or LinkedList, depending on what you're using for the external output. Thirdly, when it.hasNext() == null, or !it.hasNext(), you're going to increment it2 similarly to it, and you'll reset "it" and then store it.next() + it2.next() in the ArrayList or LinkedList while it2 traverses through the list. If that made any sense at all, you'll just have to recursively keep doing it.

I'll work on some code later to help you out, as I'm sure my pseudocode made no sense.

Hope I've helped a little!
Was This Post Helpful? 1
  • +
  • -

#6 Perogy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 09

Re: Character generation algorithm

Posted 13 November 2009 - 04:47 PM

View PostOstralis, on 13 Nov, 2009 - 03:38 PM, said:

Interesting algorithm and approach. What you'll want to do is assign the Iterators separately, as such:
Iterator it = alphabet.iterator();
Iterator it2 = alphabet.iterator();


Now, within the algorithm itself, you're going to need to continually check to see if the next position is != null, or, not null (you'll be checking to see if there's a character next in the alphabet), as such:
while (it.hasNext() && it2.hasNext())


You'll then go from there and work on your algorithm, wherein, you can set it2 as it:
it2 = it;


Good luck on the code, I can definitely help you with more algorithm work regarding this problem if that's what you're looking for, but it's hard for me to explain without giving you the code.

Actually, I'll write some pseudocode.

You're going to need to have a few loops and while statements. I understand your alphabet is a LinkedList, though, with the external spot where you're placing the Strings, is that an ArrayList? If so, you're going to need to firstly check that the ArrayList index is not taken by 5 elements. Secondly, you're going to begin by saying while it.hasNext(), store the elements in the ArrayList, or LinkedList, depending on what you're using for the external output. Thirdly, when it.hasNext() == null, or !it.hasNext(), you're going to increment it2 similarly to it, and you'll reset "it" and then store it.next() + it2.next() in the ArrayList or LinkedList while it2 traverses through the list. If that made any sense at all, you'll just have to recursively keep doing it.

I'll work on some code later to help you out, as I'm sure my pseudocode made no sense.

Hope I've helped a little!



Thanks this helped alot! Enough to get me started somewhere anyways. I'll have to go work on it for a bit and come back later for an update.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,525
  • Joined: 27-December 08

Re: Character generation algorithm

Posted 13 November 2009 - 05:11 PM

If you take a look at the ASCII definition (of which the primitive type char is based on), you will notice that the capital letter 'A' is letter 65, and capital 'Z' is letter 90. I thought I'd bring this up b/c numerical values can be converted to characters, as was mentioned in a previous post. So this means that if you do something like:
int x = 'A'; //x == 65
char c = 66; //c == 'B'



And since you want these values to be sorted, that also means you can't have more than 1 of the same character. If you use the ArrayList<Character> object, you can make use of its .contains(elem) method to test for duplicates. From there, just call the Collections.sort(myArrayListName) method on your ArrayList. The rest should be pretty straight-forwar.

If you want more information on ArrayList, check out the following link:
http://java.sun.com/.../ArrayList.html
Was This Post Helpful? 0
  • +
  • -

#8 Ostralis  Icon User is offline

  • #include <greeter>
  • member icon

Reputation: 9
  • View blog
  • Posts: 557
  • Joined: 13-December 08

Re: Character generation algorithm

Posted 13 November 2009 - 05:52 PM

^Yep, you bring up some valid points there, that might be more efficient, using numerical values and then converting them into characters.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: Character generation algorithm

Posted 13 November 2009 - 06:02 PM

I'm sorry. I'm not following the implied logic here. Give what's shown, the last value MUST start with z. Applying a strict interpretation of the rules, it still seems you'd have far more elements.

I blew my heap with 5, for 4 I get a result of:
358800) zyxw



I don't understand how this can be true:
51) az
52) bc



Which makes sense. Yet the last pair of two characters isn't "zy".
Was This Post Helpful? 0
  • +
  • -

#10 Perogy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 09

Re: Character generation algorithm

Posted 13 November 2009 - 07:13 PM

View Postbaavgai, on 13 Nov, 2009 - 05:02 PM, said:

I'm sorry. I'm not following the implied logic here. Give what's shown, the last value MUST start with z. Applying a strict interpretation of the rules, it still seems you'd have far more elements.

I blew my heap with 5, for 4 I get a result of:
358800) zyxw



I don't understand how this can be true:
51) az
52) bc



Which makes sense. Yet the last pair of two characters isn't "zy".


The last pair of 2 characters would be "yz". The next "zz" would be invalid as it has to always be "ascending".

for 3 characters it would go from "abc" to "xyz" and so on
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: Character generation algorithm

Posted 13 November 2009 - 07:34 PM

View PostPerogy, on 13 Nov, 2009 - 08:13 PM, said:

The last pair of 2 characters would be "yz". The next "zz" would be invalid as it has to always be "ascending".

for 3 characters it would go from "abc" to "xyz" and so on


Ah! Got it. My numbers still aren't jiving with what you've got. But at least it makes a little sense.
// if first value is a
1) a
142506) vwxyz

// if first value if 5 char
1) abcde
65780) vwxyz


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1