permutations 0.1.3 → 0.1.4
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/{Readme.rdoc → Readme.md} +23 -14
- data/lib/permutations.rb +9 -4
- data/permutations.gemspec +5 -5
- metadata +22 -10
data/{Readme.rdoc → Readme.md}
RENAMED
@@ -1,39 +1,48 @@
|
|
1
|
-
|
1
|
+
Permutations
|
2
|
+
============
|
2
3
|
|
3
4
|
Generate permutations for nested arrays or strings that offer one or more choices.
|
4
5
|
|
5
|
-
|
6
|
+
Installation
|
7
|
+
------------
|
6
8
|
|
7
9
|
You can use it as a Rails plugin or as a normal ruby.
|
8
10
|
|
9
|
-
|
11
|
+
script/plugin install git://github.com/Narnach/permutations.git
|
10
12
|
|
11
13
|
or
|
12
14
|
|
13
|
-
|
15
|
+
sudo gem install permutations
|
14
16
|
|
15
|
-
|
17
|
+
Example
|
18
|
+
-------
|
16
19
|
|
17
20
|
You can permutate array elements:
|
18
|
-
|
19
|
-
|
21
|
+
|
22
|
+
require 'permutations'
|
23
|
+
[[1,2],[3,4]].permutations # =>[[1,3],[1,4],[2,3],[2,4]]
|
20
24
|
|
21
25
|
You can also permutate choice sets extraced from strings. These choices are specified between curly braces:
|
22
|
-
|
26
|
+
|
27
|
+
"{1,2}{3,4}".permutations # => ["13", "14", "23", "24"]
|
23
28
|
|
24
29
|
It is possible to use a custom separator, and whitespace surrounding options is automatically removed to allow for more reader-friendly formatting of the choicees:
|
25
|
-
|
30
|
+
|
31
|
+
"{ 1 | 2 }{ 3 | 4 }".permutations("|") # => ["13", "14", "23", "24"]
|
26
32
|
|
27
33
|
By default (custom) separators are regexp-escaped, but this can be turned off to use a regexp to determine the separation; for example when there are multiple separators:
|
28
|
-
"{1|2}{3,4}".permutations("[|,]", false) # => ["13", "14", "23", "24"]
|
29
34
|
|
30
|
-
|
35
|
+
"{1|2}{3,4}".permutations("[|,]", false) # => ["13", "14", "23", "24"]
|
36
|
+
|
37
|
+
Contributing
|
38
|
+
------------
|
31
39
|
|
32
|
-
Bugs and feature requests can be added in Lighthouse. Pull requests on
|
33
|
-
When you submit code, it is very much appreciated when have specs and perhaps a bit of documentation for new features.
|
40
|
+
Bugs and feature requests can be added in Lighthouse. Pull requests on Github are welcome, too.
|
41
|
+
When you submit code, it is very much appreciated when you have specs and perhaps a bit of documentation for new features.
|
34
42
|
|
35
43
|
https://narnach.lighthouseapp.com/projects/45010-permutations
|
36
44
|
|
37
|
-
|
45
|
+
Credits
|
46
|
+
-------
|
38
47
|
|
39
48
|
Created by Wes Oldenbeuving. Licensed under the MIT License.
|
data/lib/permutations.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module Permutations
|
2
2
|
module Array
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# @param [Array] leftovers remaining elements to recursively permutate through.
|
4
|
+
# @return [Array] Nested array with all permutations
|
5
|
+
# @example
|
6
|
+
# [[1,2],[3,4]].permutations #=> [[1,3],[1,4],[2,3],[2,4]]
|
5
7
|
def permutations(leftovers=self)
|
6
8
|
tail = leftovers.last
|
7
9
|
return tail.map{|e| [e]} if leftovers.size == 1
|
@@ -16,8 +18,11 @@ module Permutations
|
|
16
18
|
end
|
17
19
|
|
18
20
|
module String
|
19
|
-
#
|
20
|
-
#
|
21
|
+
# @param [String] separator Pattern to split the string on
|
22
|
+
# @param [TrueClass, FalseClass] escape_separator Wether to Regexp-escape the separator and treat it as a literal separator (true) or treat it as a regexp (false)
|
23
|
+
# @return [Array] Nested array with all permutations
|
24
|
+
# @example
|
25
|
+
# "{a,b,c}{1,2,3}".permutations #=> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
|
21
26
|
def permutations(separator=',', escape_separator=true)
|
22
27
|
separator=Regexp.escape(separator) if escape_separator
|
23
28
|
matches = self.scan(/(\{\s*(.*?)\s*\})/)
|
data/permutations.gemspec
CHANGED
@@ -3,15 +3,15 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.name = 'permutations'
|
4
4
|
s.summary = "Permutations is a library that allows you to generate permutations from Arrays and Strings."
|
5
5
|
s.description = s.summary
|
6
|
-
s.version = '0.1.
|
7
|
-
s.date = '
|
6
|
+
s.version = '0.1.4'
|
7
|
+
s.date = '2011-11-23'
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Wes Oldenbeuving"]
|
10
10
|
s.email = "narnach@gmail.com"
|
11
11
|
s.homepage = "http://www.github.com/narnach/permutations"
|
12
12
|
|
13
13
|
# Files
|
14
|
-
root_files = %w[Readme.
|
14
|
+
root_files = %w[Readme.md Rakefile permutations.gemspec init.rb MIT-LICENSE]
|
15
15
|
bin_files = []
|
16
16
|
lib_files = %w[permutations]
|
17
17
|
s.bindir = "bin"
|
@@ -22,8 +22,8 @@ Gem::Specification.new do |s|
|
|
22
22
|
|
23
23
|
# rdoc
|
24
24
|
s.has_rdoc = true
|
25
|
-
s.extra_rdoc_files = %w[ Readme.
|
26
|
-
s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'Readme.
|
25
|
+
s.extra_rdoc_files = %w[ Readme.md]
|
26
|
+
s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'Readme.md'
|
27
27
|
|
28
28
|
# Requirements
|
29
29
|
s.required_ruby_version = ">= 1.8.0"
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permutations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Wes Oldenbeuving
|
@@ -9,8 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
18
|
+
date: 2011-11-23 00:00:00 Z
|
14
19
|
dependencies: []
|
15
20
|
|
16
21
|
description: Permutations is a library that allows you to generate permutations from Arrays and Strings.
|
@@ -20,15 +25,14 @@ executables: []
|
|
20
25
|
extensions: []
|
21
26
|
|
22
27
|
extra_rdoc_files:
|
23
|
-
- Readme.
|
28
|
+
- Readme.md
|
24
29
|
files:
|
25
|
-
- Readme.
|
30
|
+
- Readme.md
|
26
31
|
- Rakefile
|
27
32
|
- permutations.gemspec
|
28
33
|
- init.rb
|
29
34
|
- MIT-LICENSE
|
30
35
|
- lib/permutations.rb
|
31
|
-
has_rdoc: true
|
32
36
|
homepage: http://www.github.com/narnach/permutations
|
33
37
|
licenses: []
|
34
38
|
|
@@ -37,25 +41,33 @@ rdoc_options:
|
|
37
41
|
- --inline-source
|
38
42
|
- --line-numbers
|
39
43
|
- --main
|
40
|
-
- Readme.
|
44
|
+
- Readme.md
|
41
45
|
require_paths:
|
42
46
|
- lib
|
43
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
44
49
|
requirements:
|
45
50
|
- - ">="
|
46
51
|
- !ruby/object:Gem::Version
|
52
|
+
hash: 55
|
53
|
+
segments:
|
54
|
+
- 1
|
55
|
+
- 8
|
56
|
+
- 0
|
47
57
|
version: 1.8.0
|
48
|
-
version:
|
49
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
50
60
|
requirements:
|
51
61
|
- - ">="
|
52
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
53
66
|
version: "0"
|
54
|
-
version:
|
55
67
|
requirements: []
|
56
68
|
|
57
69
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.8.10
|
59
71
|
signing_key:
|
60
72
|
specification_version: 3
|
61
73
|
summary: Permutations is a library that allows you to generate permutations from Arrays and Strings.
|