|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
Quote:
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. |
|
#4
|
||||
|
||||
|
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... ![]() |
|
#5
|
||||
|
||||
|
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..
|
|
#6
|
||||
|
||||
|
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 |
|
#7
|
|||||
|
|||||
|
Something like this I guess:
java Code:
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 ![]() |
|
#8
|
|||
|
|||
|
Thanks for all the ideas they helped a lot.
|
|
#9
|
|||
|
|||
|
Quote:
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;
}
|
|
#10
|
||||
|
||||
|
Nice, but your code looks like Javascript not Java...
![]() |
|
#11
|
||||
|
||||
|
Yep, that was Javascript... I like the technique though... good show!
|
|
#12
|
|||||
|
|||||
|
Here's a similar approach to Icon's.
Java Code:
@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. |