Advertisement
Guest User

Untitled

a guest
May 25th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #
  2. # Recursion generating all valid 5 length permutations.
  3. #
  4. def patterns(pattern, i)
  5. if pattern.length == 5
  6. p_p pattern and return
  7. end
  8.  
  9. (1..5).each do |v|
  10. if is_valid(pattern[0...i], v)
  11. pattern[i] = v
  12. patterns Array.new(pattern), i+1
  13. end
  14. end
  15. end
  16.  
  17. #
  18. # Checking if value can be appended to the pattern
  19. #
  20. def is_valid(pattern, value)
  21. # value can not be appended if value if already in the pattern
  22. return false unless (pattern & [value]).empty?
  23.  
  24. # value can not be appended if adding the value would result
  25. # in a decreasing/increasing sequence of length 3
  26. return false if (pattern.length > 1) && ((pattern[-1] - value) * (pattern[-2] - pattern[-1]) == 1)
  27.  
  28. true
  29. end
  30.  
  31. def p_p(pattern)
  32. p "#{pattern} with #{(pattern.select.with_index { |e, i| e != i + 1 }.length / 2.0).round} transpositions"
  33. end
  34.  
  35. patterns Array.new, 0
  36.  
  37.  
  38. <<-RESULTS
  39. [1, 2, 4, 3, 5] with 1 transpositions
  40. [1, 2, 4, 5, 3] with 2 transpositions
  41. [1, 2, 5, 3, 4] with 2 transpositions
  42. [1, 3, 2, 4, 5] with 1 transpositions
  43. [1, 3, 2, 5, 4] with 2 transpositions
  44. [1, 3, 4, 2, 5] with 2 transpositions
  45. [1, 3, 5, 2, 4] with 2 transpositions
  46. [1, 3, 5, 4, 2] with 2 transpositions
  47. [1, 4, 2, 3, 5] with 2 transpositions
  48. [1, 4, 2, 5, 3] with 2 transpositions
  49. [1, 4, 3, 5, 2] with 2 transpositions
  50. [1, 4, 5, 2, 3] with 2 transpositions
  51. [1, 4, 5, 3, 2] with 2 transpositions
  52. [1, 5, 2, 4, 3] with 2 transpositions
  53. [1, 5, 3, 2, 4] with 2 transpositions
  54. [1, 5, 3, 4, 2] with 1 transpositions
  55. [1, 5, 4, 2, 3] with 2 transpositions
  56. [2, 1, 3, 5, 4] with 2 transpositions
  57. [2, 1, 4, 3, 5] with 2 transpositions
  58. [2, 1, 4, 5, 3] with 3 transpositions
  59. [2, 1, 5, 3, 4] with 3 transpositions
  60. [2, 3, 1, 4, 5] with 2 transpositions
  61. [2, 3, 1, 5, 4] with 3 transpositions
  62. [2, 3, 5, 1, 4] with 3 transpositions
  63. [2, 3, 5, 4, 1] with 2 transpositions
  64. [2, 4, 1, 3, 5] with 2 transpositions
  65. [2, 4, 1, 5, 3] with 3 transpositions
  66. [2, 4, 3, 1, 5] with 2 transpositions
  67. [2, 4, 3, 5, 1] with 2 transpositions
  68. [2, 4, 5, 1, 3] with 3 transpositions
  69. [2, 4, 5, 3, 1] with 3 transpositions
  70. [2, 5, 1, 3, 4] with 3 transpositions
  71. [2, 5, 1, 4, 3] with 2 transpositions
  72. [2, 5, 3, 1, 4] with 2 transpositions
  73. [2, 5, 3, 4, 1] with 2 transpositions
  74. [2, 5, 4, 1, 3] with 3 transpositions
  75. [3, 1, 2, 4, 5] with 2 transpositions
  76. [3, 1, 2, 5, 4] with 3 transpositions
  77. [3, 1, 4, 2, 5] with 2 transpositions
  78. [3, 1, 4, 5, 2] with 3 transpositions
  79. [3, 1, 5, 2, 4] with 3 transpositions
  80. [3, 1, 5, 4, 2] with 2 transpositions
  81. [3, 2, 4, 1, 5] with 2 transpositions
  82. [3, 2, 4, 5, 1] with 2 transpositions
  83. [3, 2, 5, 1, 4] with 2 transpositions
  84. [3, 2, 5, 4, 1] with 2 transpositions
  85. [3, 4, 1, 2, 5] with 2 transpositions
  86. [3, 4, 1, 5, 2] with 3 transpositions
  87. [3, 4, 2, 1, 5] with 2 transpositions
  88. [3, 4, 2, 5, 1] with 3 transpositions
  89. [3, 5, 1, 2, 4] with 3 transpositions
  90. [3, 5, 1, 4, 2] with 2 transpositions
  91. [3, 5, 2, 1, 4] with 3 transpositions
  92. [3, 5, 2, 4, 1] with 2 transpositions
  93. [3, 5, 4, 1, 2] with 3 transpositions
  94. [3, 5, 4, 2, 1] with 3 transpositions
  95. [4, 1, 2, 5, 3] with 3 transpositions
  96. [4, 1, 3, 2, 5] with 2 transpositions
  97. [4, 1, 3, 5, 2] with 2 transpositions
  98. [4, 1, 5, 2, 3] with 3 transpositions
  99. [4, 1, 5, 3, 2] with 3 transpositions
  100. [4, 2, 1, 3, 5] with 2 transpositions
  101. [4, 2, 1, 5, 3] with 2 transpositions
  102. [4, 2, 3, 1, 5] with 1 transpositions
  103. [4, 2, 3, 5, 1] with 2 transpositions
  104. [4, 2, 5, 1, 3] with 2 transpositions
  105. [4, 2, 5, 3, 1] with 2 transpositions
  106. [4, 3, 1, 2, 5] with 2 transpositions
  107. [4, 3, 1, 5, 2] with 3 transpositions
  108. [4, 3, 5, 1, 2] with 3 transpositions
  109. [4, 3, 5, 2, 1] with 3 transpositions
  110. [4, 5, 1, 3, 2] with 3 transpositions
  111. [4, 5, 2, 1, 3] with 3 transpositions
  112. [4, 5, 2, 3, 1] with 3 transpositions
  113. [4, 5, 3, 1, 2] with 2 transpositions
  114. [5, 1, 2, 4, 3] with 2 transpositions
  115. [5, 1, 3, 2, 4] with 2 transpositions
  116. [5, 1, 3, 4, 2] with 2 transpositions
  117. [5, 1, 4, 2, 3] with 3 transpositions
  118. [5, 2, 1, 3, 4] with 2 transpositions
  119. [5, 2, 1, 4, 3] with 2 transpositions
  120. [5, 2, 3, 1, 4] with 2 transpositions
  121. [5, 2, 4, 1, 3] with 2 transpositions
  122. [5, 2, 4, 3, 1] with 2 transpositions
  123. [5, 3, 1, 2, 4] with 3 transpositions
  124. [5, 3, 1, 4, 2] with 2 transpositions
  125. [5, 3, 2, 4, 1] with 2 transpositions
  126. [5, 3, 4, 1, 2] with 3 transpositions
  127. [5, 3, 4, 2, 1] with 3 transpositions
  128. [5, 4, 1, 3, 2] with 3 transpositions
  129. [5, 4, 2, 1, 3] with 3 transpositions
  130. [5, 4, 2, 3, 1] with 3 transpositions
  131. RESULTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement