Created
March 20, 2012 05:40
-
-
Save mikeflynn/2131717 to your computer and use it in GitHub Desktop.
given a list of words, i need every unique combination where the list is in alphabetical order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function order_list($words, &$return = null) | |
{ | |
if(!$return) $return = array(); | |
sort($words); | |
$string = implode(', ', $words); | |
if(!empty($string) && !in_array($string, $return)) $return[] = $string; | |
foreach($words as $key=>$word) | |
{ | |
$temp = $words; | |
unset($temp[$key]); | |
sort($temp); | |
$string = implode(', ', $temp); | |
if(!empty($string) && !in_array($string, $return)) $return[] = $string; | |
if(sizeof($temp) > 0) order_list($temp, $return); | |
} | |
} | |
order_list(array('apple', 'zebra', 'cat', 'bear'), $result); | |
if(!empty($result) && is_array($result)) | |
{ | |
echo "--------\n"; | |
echo implode("\n", $result); | |
echo "\n--------\n"; | |
} | |
echo "\nDone.\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved this from a private gist to a public.
Another solution: https://gist.github.com/764121