unique_permutation 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/unique_permutation.rb +32 -0
- metadata +45 -0
@@ -0,0 +1,32 @@
|
|
1
|
+
# Add the unique_permutation method to the Array class.
|
2
|
+
# This is incredibly more efficient that the built in permutation method as duplicate elements will yield
|
3
|
+
# identical permutations.
|
4
|
+
|
5
|
+
class Array
|
6
|
+
def unique_permutation(&block)
|
7
|
+
return enum_for(:unique_permutation) unless block_given?
|
8
|
+
|
9
|
+
array_copy = self.sort
|
10
|
+
yield array_copy.dup
|
11
|
+
return if size < 2
|
12
|
+
|
13
|
+
while true
|
14
|
+
# Based off of Algorithm L (Donald Knuth)
|
15
|
+
j = size - 2;
|
16
|
+
j -= 1 while j > 0 && array_copy[j] >= array_copy[j+1]
|
17
|
+
|
18
|
+
if array_copy[j] < array_copy[j+1]
|
19
|
+
l = size - 1
|
20
|
+
l -= 1 while array_copy[j] >= array_copy[l]
|
21
|
+
|
22
|
+
array_copy[j] , array_copy[l] = array_copy[l] , array_copy[j]
|
23
|
+
array_copy[j+1..-1] = array_copy[j+1..-1].reverse
|
24
|
+
|
25
|
+
yield array_copy.dup
|
26
|
+
|
27
|
+
else
|
28
|
+
break
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: unique_permutation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron Rosenberg
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Adds the unique_permutation method to the array class.
|
15
|
+
email: aarongrosenberg@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/unique_permutation.rb
|
21
|
+
homepage: https://github.com/LtCmdDudefellah/unique_permutation
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.17
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Create unique permutations from an array
|
45
|
+
test_files: []
|