Monday, May 24, 2010

How can I create a simple word unscrambler in java?

The program will start by asking the user to input a "scrambled" word. Then it will compute and output all possible re-arrangements of the letters in the user's input. Sounds easy, but I'm dying, like usual, trying to figure out how to code it.





And I'm not just asking for code, it you want to add it, please, haha, thats ok. But I know some people are anti-code, which is fine with me, i just want to know how. Thanks!

How can I create a simple word unscrambler in java?
Nice one! LOL! The answer, my friend, is Deep Recursion!


I am seriously "pro-code" - it's the best way to learn. Even when you do things that can be done better, that doesn't matter. Here's some code that will do it. You can tidy it up by removing three of the functions jsMid, jsLeft and jsRight if you want really minimal code (but heck, it was based on a VB algorithm so I just kept the idea of VB string functions and VB string types. BY THE WAY - this is NOT EFFICIENT - I wouldn't enter more than 6 characters or you may be waiting a while..... enjoy


%26lt;html%26gt;





%26lt;head%26gt;


%26lt;meta http-equiv="Content-Language" content="en-gb"%26gt;


%26lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"%26gt;





%26lt;script type="text/javascript" language="javascript"%26gt;





function factorial(n) {


if ( (n == 0) || (n == 1) ) return 1


else return (n * factorial(n-1) )


}





function scramble() {


var ipLen, ipStr, e ;





e = document.getElementById("txtIP") ;


ipStr = e.value ;


ipLen = ipStr.length ;





if ( ipLen %26lt; 2 ) {


alert ( "Be sensible! Enter two or more characters!" ) ;


return ;


}





alert ( "You have entered " + ipLen + " characters - there are " + factorial(ipLen) + " permutations!" ) ;





getCombinations ( "", ipStr ) ;





}





function getCombinations ( estr, ostr ) {


var i, j, e ;





j = ostr.length ;





if ( j %26lt; 2 ) {


e = document.getElementById("results")


s = e.innerHTML


s = s + "%26lt;br%26gt;" + estr + ostr


e.innerHTML = s


}


else {


for ( i=1; i%26lt;=j; i++ ){


getCombinations ( estr + jsMid(ostr,i, 1), jsLeft(ostr, i-1) + jsRight(ostr, j-i))


}


}


}





function jsMid ( str, s, c ) {


return str.substring(s-1, s-1+c) ;


}





function jsLeft (str, c ) {


return str.substring ( 0, c ) ;


}





function jsRight ( str, c ) {


return str.substring ( str.length - c ) ;


}





%26lt;/script%26gt;





%26lt;/head%26gt;





%26lt;body%26gt;





%26lt;p%26gt;   


Input your word here %26lt;input type="text" id="txtIP" name="T1" size="20"%26gt;  Now,


%26lt;span id="span1" onclick="scramble()" %26gt;%26lt;u%26gt;click me%26lt;/u%26gt;%26lt;/span%26gt; to scramble !!


%26lt;/p%26gt;





%26lt;/form%26gt;





%26lt;p%26gt;


%26lt;div id="results"%26gt;


%26lt;/div%26gt;


%26lt;/p%26gt;





%26lt;/body%26gt;





%26lt;/html%26gt;





And if you figure out the recursion, I'll buy you a beer !!!!!


No comments:

Post a Comment