Guest User

Untitled

a guest
Jul 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. @author: Andrew Horsman
  5. @description: Incremental permutations.
  6. */
  7.  
  8. class PermutationGenerator {
  9.  
  10. 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', '{', '|', '}', '~');
  11. private $digitMax;
  12. private $digits;
  13.  
  14. public function printSeries($separator) {
  15. for ($x = 0; $x < count($this->digits); ++$x) {
  16. echo $this->characters[$this->digits[$x]];
  17. if ($x < count($this->digits) - 1) echo $separator;
  18. }
  19. echo "\n";
  20. }
  21.  
  22. public function returnSeries() {
  23. for ($x = 0; $x < count($this->digits); ++$x) {
  24. $ret .= $this->digits[$x];
  25. }
  26. return $ret;
  27. }
  28.  
  29. public function increment() {
  30.  
  31. $this->printSeries(" ");
  32.  
  33. if ($this->digits[count($this->digits) - 1] != $this->digitMax) {
  34. ++$this->digits[count($this->digits) - 1];
  35. return 0;
  36. }
  37.  
  38. $edited = 0;
  39. for ($x = count($this->digits) - 1; $x >= 0; --$x) {
  40. if ($this->digits[$x] == $this->digitMax) {
  41. $this->digits[$x] = 0; continue;
  42. } else {
  43. ++$this->digits[$x];
  44. $edited = 1;
  45. break;
  46. }
  47. }
  48. if ($edited == 0) {
  49. array_unshift($this->digits, 0);
  50. }
  51.  
  52. }
  53.  
  54. public function __construct($digits = array(0), $characters = 0) {
  55. $this->digits = $digits;
  56. if ($characters != 0) {
  57. $this->characters = $characters;
  58. }
  59. $this->digitMax = count($this->characters) - 1;
  60. }
  61.  
  62. }
  63.  
  64. $instance = new PermutationGenerator();
  65. for ($x = 0; $x < 100; ++$x) {
  66. $instance->increment();
  67. }
  68.  
  69. ?>
Add Comment
Please, Sign In to add comment