Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 9th, 2006, 07:13 PM
highlander2k5 highlander2k5 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 14 highlander2k5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 31 sec
Reputation Power: 0
Mixing up a string

I was wondering if anyone knew what method to use in order to mix up a user-entered string? For example if the word computer were entered for the string I want it to read something like pmuctreo. Basically it's like a word jumble. Please help... I've been trying to find one on java.sun for hours.

Reply With Quote
  #2  
Old February 10th, 2006, 12:43 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Post

Pseudocode:
Code:
Create a new empty string (the jumbled result)
Make a list of characters of the entered string
while( not list.isempty)
{
  indexofvictim = Math.Random(list.length)
  Jumbledstring = jumbledstring + list[indexofvictim]
  list.remove(indexofvictim)
}



You can easily create a function like this in any language. There may be easier ways and it's too early for me to remember all the nice java functions out there..

p.s. the pseudo code is very pseudo, i.e., figure out the specifics and off-by-one bugs yourself

Last edited by Icon : February 10th, 2006 at 07:13 AM.

Reply With Quote
  #3  
Old February 10th, 2006, 05:54 AM
destin destin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 18 destin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 6 m 19 sec
Reputation Power: 0
Send a message via AIM to destin
Quote:
Originally Posted by Icon
Pseudocode:
Create a new empty string (the jumbled result)
Make a list of characters of the entered string
while( not list.isempty)
{
indexofvictim = Math.Random(list.length)
Jumbledstring = jumbledstring + list[indexofvictim]
list.remove(indexofvictim)
}

You should use a StringBuffer or StringBuilder here. The way you're doing it you allocate memory for a new String every iteration of the loop.

Reply With Quote
  #4  
Old February 10th, 2006, 07:11 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
highlander2k5 should use something like StringBuffer yes.. As is said: it's just pseudocode, figure the java-specific stuff yourself. There are lots of other ways to jumble a string this was just an example.

Another one comes to mind:
Code:
len = str.length()
for c=0 to len-1
  //swap two chars
  temp = str[c]
  other = random(len)
  str[c] = str[other]
  str[other] = temp


Also jumbles in O(n) ( O(strlen) ) with no extra memory requirements..

The moral is: I don't think something already exists in the Java API, so you have to go nuts yourself

But I might be mistaken, you know of a 'jumble' method destin?
Hmm, I wonder if there are any code obfuscate tools written in Java from which we can get the source...
Comments on this post
MadCowDzz agrees!

Reply With Quote
  #5  
Old February 10th, 2006, 07:19 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Hmm, you probably need stringbuffer or something for this as well.. Strings are immutable.. I'm doing to many languages at the same time... Of course a good old fashioned char[] would also work..

Reply With Quote
  #6  
Old February 10th, 2006, 07:41 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I like your second example... I'm interested in testing it to see if it works.
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #7  
Old February 10th, 2006, 10:24 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Something like this I guess:

java Code:
Original - java Code
  1. class Jumble
  2. {
  3.   public static String Jumble(String original)
  4.   {
  5.     StringBuilder sb = new StringBuilder(original);
  6.  
  7.     char temp;
  8.     int  swapWith;
  9.     for(int i=0; i<sb.length(); i++)
  10.     {
  11.       temp = sb.charAt(i);
  12.       swapWith = (int)Math.floor(Math.random()*sb.length());
  13.       sb.setCharAt(i, sb.charAt(swapWith));
  14.       sb.setCharAt(swapWith, temp);
  15.     }
  16.  
  17.     return sb.toString();
  18.   }
  19.  
  20.   public static void main(String[] args)
  21.   {
  22.     String someInput = "A nice string";
  23.  
  24.     System.out.println(Jumble(someInput));
  25.     System.out.println(Jumble(someInput));
  26.     System.out.println(Jumble(someInput));
  27.   }
  28. }


This one is actually:
T(n) = 3n ~ O(n)
M(n) = 3n too. Where T is time complexity and M is memory usage (n is, as usual, the length of the problem instance).

Depending on the program one can probably shave of some more cycles but I'm more into understandable code lately
Comments on this post
MadCowDzz agrees: Good code... you beat me to it!

Reply With Quote
  #8  
Old February 10th, 2006, 04:39 PM
highlander2k5 highlander2k5 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 14 highlander2k5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 31 sec
Reputation Power: 0
Thanks for all the ideas they helped a lot.

Reply With Quote
  #9  
Old February 13th, 2006, 10:18 PM
mrhoo mrhoo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 4 mrhoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 17 m 4 sec
Reputation Power: 0
Quote:
Originally Posted by highlander2k5
I was wondering if anyone knew what method to use in order to mix up a user-entered string? For example if the word computer were entered for the string I want it to read something like pmuctreo. Basically it's like a word jumble. Please help... I've been trying to find one on java.sun for hours.



1. Split the string into an array of characters:
var A=str.split('');

2. pass it to an unsorting function:
A=unsort(A);

3. join the scrambled array with the empty string:
return A.join('');

I scramble a lot of stuff, so I made a scrambler a method of all arrays:

Code:
Array.prototype.disorder=function(){
            var tem,temp;
	var L= this.length;	
	while(--L){
		tem= Math.round(Math.random()*L);
		if(tem!= L){
			temp= this[tem];
			this[tem]= this[L];
			this[L]= temp;
		}
	}
	return this;		
}
Comments on this post
MadCowDzz agrees: Smells like Javascript; not Java... but nice style!

Reply With Quote
  #10  
Old February 14th, 2006, 12:21 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Nice, but your code looks like Javascript not Java...

Reply With Quote
  #11  
Old February 14th, 2006, 07:30 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Yep, that was Javascript... I like the technique though... good show!

Reply With Quote
  #12  
Old February 14th, 2006, 03:02 PM
destin destin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 18 destin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 6 m 19 sec
Reputation Power: 0
Send a message via AIM to destin
Here's a similar approach to Icon's.
Java Code:
Original - Java Code
  1. public String scramble(String word) {
  2.     StringBuilder builder = new StringBuilder(word.length());
  3.     boolean[] used = new boolean[word.length()];
  4.    
  5.     for (int i = 0; i < word.length(); i++) {
  6.         int rndIndex;
  7.         do {
  8.             rndIndex = new Random().nextInt(word.length());
  9.         } while (used[rndIndex]);
  10.         used[rndIndex] = true;
  11.            
  12.         builder.append(word.charAt(rndIndex));
  13.     }
  14.     return builder.toString();
  15. }


@Icon
swapWith = (int)Math.floor(Math.random()*sb.length());
The Math.floor isn't neccessary:
swapWith = (int) (Math.random() * sb.length());
When casting to an int, it will truncate the end of the double.

Also, you declare [font="Courier New"]temp[/url] and [font="Courier New"]swapWith[/code] outside the loop, but only use them in the loop. It would be better to declare them within the loop; it is best to have variables only declared in the scope they are used in.

Reply With Quote
  #13  
Old February 15th, 2006, 02:29 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 8 h 12 m 36 sec
Reputation Power: 14
Yeah, I programmed in too many languages to remember what each language does exactly. In my opinion adding the Math.floor makes things clear and explicit, which I always prefer. (Noobs understand explicit things better). The arithmetic complexity is in the random anyway (I profiled..).

Also, I declared temp outside of the loop _on purpose_. Although offcourse your argument about scope holds, it is not always best to do so, declaring temp in the loop would mean pushing an extra variable on the stack every time. Especially when creating objects within loops it is sometimes better to just reuse an object from outside the loop!

Don't want to be snippy, but your algorithm is not very efficient. you are repeatedly creating an object (Random) inside a while (and a for) loop. Having to create and seed just one Random object outside of the loop seems more logical to me. Next to that: your algorithm could never end! This is exactly the trap most programmers fall into, who will assure you that Random.nextInt() will _ever_ return a specific value? What I mean is what if rndIndex never becomes, say, 4? You are dealing with (pseudo) random numbers, so this could happen! In practice the algorithm would probably stop eventually but it has a very unpredictable time complexity and does not scale well at all.
Comments on this post
Itsacon agrees: Lesson: Profile, not pout...

Last edited by Icon : February 15th, 2006 at 02:50 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Mixing up a string


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap