Skip to content

Instantly share code, notes, and snippets.

@awojnowski
Created January 5, 2012 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awojnowski/1566415 to your computer and use it in GitHub Desktop.
Save awojnowski/1566415 to your computer and use it in GitHub Desktop.
Rearranges and uses the letters of a word to form all possible other words, in PHP.
<?php
/*
letter_rearranger.class.php
Created by Aaron Wojnowski, 2012
-- USAGE --
1) Create a .txt wordlist with words separated with a newline. Example:
wordone
wordtwo
wordthree
wordfour
wordfive
Need to find a wordlist? I recommend http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/share/dict/web2?rev=1.12.22.1 (as of 2012-01-05)
2) Import the 'letter_rearranger.class.php' file into your project.
3) Call the static function "rearrange_letters" and pass a string like so:
letter_rearranger::rearrange_letters('unicorns');
4) Receive an array back of the words that can be created.
*/
class letter_rearranger {
private $WORD_LIST_NAME = 'words.txt'; // path of your wordlist relative to this file.
private $USABLE_LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // all of the letters that can be used by the input string
private $MAX_INPUT_LENGTH = 18; // the max length of the input word
private static function call_exception($errstr, $error_line) {
throw new Exception('letter_rearranger encountered an error (' . $error_line . ') ' . $errstr);
}
public static function rearrange_letters($input_word) {
if (strlen($input_word) > letter_rearranger::$WORD_LIST_NAME || strlen($input_word) == 0) letter_rearranger::call_exception('Invalid input word.', __LINE__);
if (!file_exists(letter_rearranger::$WORD_LIST_NAME)) letter_rearranger::call_exception('Invalid wordlist path.', __LINE__);
$word_array = file(letter_rearranger::$WORD_LIST_NAME);
if (count($word_array) == 0) letter_rearranger::call_exception('Invalid wordlist contents.', __LINE__);
$letters_array = str_split($input_word);
$usable_letters_array = str_split(letter_rearranger::$USABLE_LETTERS);
/*
Firstly, we will remove any words that contain letters we do NOT have.
We can do this by simply creating an array of letters that are unused
by us, and then checking the wordlist to see if the words contain the
letters. If they do, we remove the word. Simple!
*/
$unused_letter_array = array();
for ($i = 0; $i < count($usable_letters_array); $i ++) {
if (!in_array($usable_letters_array[$i], $letters_array)) {
$unused_letter_array[] = $usable_letters_array[$i];
}
}
$word_array_copy = $word_array;
$word_array = array();
$word_array_copy_count = count($word_array_copy);
$unused_letter_array_count = count($unused_letter_array);
for ($i = 0; $i < $word_array_copy_count; $i ++) {
$word = substr($word_array_copy[$i], 0, -1); // remove newline at the end of the word
$word_letters = str_split($word);
if (count($word_letters) <= count($letters_array)) { // if there are more letters in the word we are checking than the original word, we obviously cannot make the word with our letters
for ($o = 0; $o < count($word_letters); $o ++) {
if (in_array($word_letters[$o], $unused_letter_array)) break;
if ($o == (count($word_letters) - 1)) $word_array[] = $word;
}
}
}
/*
Now that we have an array of words containing only our letters,
we will check and compare the amount of letters in the remaining
possible words.
*/
$word_array_count = count($word_array);
$good_word_array = array();
for ($i = 0; $i < $word_array_count; $i ++) {
$letter_array = str_split($word_array[$i]);
$letter_unique_array = array_unique($letter_array);
$letter_count_array = array_count_values($letter_array);
$our_letters_count_array = array_count_values($letters_array);
$success = true;
foreach($letter_unique_array as $letter) {
if (array_key_exists($letter, $letter_count_array) && array_key_exists($letter, $our_letters_count_array)) {
if ($letter_count_array[$letter] > $our_letters_count_array[$letter]) {
$success = false;
break;
}
}
}
if ($success) $good_word_array[] = $word_array[$i];
}
return $good_word_array;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment