Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created February 27, 2011 04:46
Show Gist options
  • Save basicxman/845910 to your computer and use it in GitHub Desktop.
Save basicxman/845910 to your computer and use it in GitHub Desktop.
<?php
/*
@author: Andrew Horsman
@description: Incremental permutations.
*/
class PermutationGenerator {
public $characters = array('!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~');
private $digitMax;
private $digits;
public function printSeries($separator) {
for ($x = 0; $x < count($this->digits); ++$x) {
echo $this->characters[$this->digits[$x]];
if ($x < count($this->digits) - 1) echo $separator;
}
echo "\n";
}
public function returnSeries() {
for ($x = 0; $x < count($this->digits); ++$x) {
$ret .= $this->digits[$x];
}
return $ret;
}
public function increment() {
$this->printSeries(" ");
if ($this->digits[count($this->digits) - 1] != $this->digitMax) {
++$this->digits[count($this->digits) - 1];
return 0;
}
$edited = 0;
for ($x = count($this->digits) - 1; $x >= 0; --$x) {
if ($this->digits[$x] == $this->digitMax) {
$this->digits[$x] = 0; continue;
} else {
++$this->digits[$x];
$edited = 1;
break;
}
}
if ($edited == 0) {
array_unshift($this->digits, 0);
}
}
public function __construct($digits = array(0), $characters = 0) {
$this->digits = $digits;
if ($characters != 0) {
$this->characters = $characters;
}
$this->digitMax = count($this->characters) - 1;
}
}
$instance = new PermutationGenerator();
for ($x = 0; $x < 100; ++$x) {
$instance->increment();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment