permutations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Rakefile +59 -0
- data/Readme.rdoc +13 -0
- data/init.rb +1 -0
- data/lib/permutations.rb +41 -0
- data/permutations.gemspec +30 -0
- metadata +63 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Wes Oldenbeuving
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require "rake"
|
2
|
+
require "rake/clean"
|
3
|
+
require "rake/gempackagetask"
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
################################################################################
|
7
|
+
### Gem
|
8
|
+
################################################################################
|
9
|
+
|
10
|
+
begin
|
11
|
+
# Parse gemspec using the github safety level.
|
12
|
+
file = Dir['*.gemspec'].first
|
13
|
+
data = File.read(file)
|
14
|
+
spec = nil
|
15
|
+
# FIXME: Lowered SAFE from 3 to 2 to work with Ruby 1.9 due to rubygems
|
16
|
+
# performing a require internally
|
17
|
+
Thread.new { spec = eval("$SAFE = 2\n%s" % data)}.join
|
18
|
+
|
19
|
+
# Create the gem tasks
|
20
|
+
Rake::GemPackageTask.new(spec) do |package|
|
21
|
+
package.gem_spec = spec
|
22
|
+
end
|
23
|
+
rescue Exception => e
|
24
|
+
printf "WARNING: Error caught (%s): %s\n%s", e.class.name, e.message, e.backtrace[0...5].map {|l| ' %s' % l}.join("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Package and install the gem for the current version'
|
28
|
+
task :install => :gem do
|
29
|
+
system "sudo gem install -l pkg/%s-%s.gem" % [spec.name, spec.version]
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Show files missing from gemspec'
|
33
|
+
task :diff do
|
34
|
+
files = %w[
|
35
|
+
Rakefile
|
36
|
+
*README* *readme*
|
37
|
+
*LICENSE*
|
38
|
+
*.gemspec deps.rip
|
39
|
+
bin/*
|
40
|
+
lib/**/*
|
41
|
+
spec/**/*
|
42
|
+
].map {|pattern| Dir.glob(pattern)}.flatten.select{|f| File.file?(f)}
|
43
|
+
missing_files = files - spec.files
|
44
|
+
extra_files = spec.files - files
|
45
|
+
puts "Missing files:"
|
46
|
+
puts missing_files.join(" ")
|
47
|
+
puts "Extra files:"
|
48
|
+
puts extra_files.join(" ")
|
49
|
+
end
|
50
|
+
|
51
|
+
desc 'Local install the latest gem version'
|
52
|
+
task :reinstall do
|
53
|
+
system("rm -f pkg/*.gem && rake gem && gem install pkg/*.gem")
|
54
|
+
end
|
55
|
+
|
56
|
+
desc 'Uninstall all Fresnel versions and install the latest gem version'
|
57
|
+
task :upgrade do
|
58
|
+
system("gem uninstall -a -x fresnel && rm -f pkg/*.gem && rake gem && gem install pkg/*.gem")
|
59
|
+
end
|
data/Readme.rdoc
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
= Permutations
|
2
|
+
|
3
|
+
Generate permutations for nested arrays or strings that offer one or more choices.
|
4
|
+
You can use it as a Rails plugin.
|
5
|
+
|
6
|
+
== Example
|
7
|
+
|
8
|
+
[[1,2],[3,4]].permutations # =>[[1,3],[1,4],[2,3],[2,4]]
|
9
|
+
"{1,2}{3,4}".permutations # => ["13", "14", "23", "24"]
|
10
|
+
|
11
|
+
== Credits
|
12
|
+
|
13
|
+
Created by Wes Oldenbeuving. Licensed under the MIT License.
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'permutations'
|
data/lib/permutations.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Permutations
|
2
|
+
module Array
|
3
|
+
# Recursively permutate nested arrays.
|
4
|
+
def permutations(leftovers=self)
|
5
|
+
tail = leftovers.last
|
6
|
+
return tail.map{|e| [e]} if leftovers.size == 1
|
7
|
+
resulting_permutations = []
|
8
|
+
permutations(leftovers[0...-1]).each do |permutation|
|
9
|
+
tail.each do |tail_element|
|
10
|
+
resulting_permutations << (permutation + [tail_element])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
resulting_permutations
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module String
|
18
|
+
# Create multi-level string permutations
|
19
|
+
# "{a,b,c}{1,2,3}".permutations # => ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
|
20
|
+
def permutations
|
21
|
+
matches = self.scan(/(\{(.*?)\})/)
|
22
|
+
substitutions = matches.map{|match| match[0]}
|
23
|
+
permutations = matches.map{|match| match[1].split(/\s*,\s*/)}.permutations
|
24
|
+
permutations.map do |permutation|
|
25
|
+
permutated_string = self.dup
|
26
|
+
permutation.each_with_index do |choice, index|
|
27
|
+
choice_string = substitutions[index]
|
28
|
+
permutated_string.sub!(choice_string,choice)
|
29
|
+
end
|
30
|
+
permutated_string
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
class Array
|
36
|
+
include Permutations::Array
|
37
|
+
end
|
38
|
+
|
39
|
+
class String
|
40
|
+
include Permutations::String
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
# Project
|
3
|
+
s.name = 'permutations'
|
4
|
+
s.summary = "Permutations is a library that allows you to generate permutations from Arrays and Strings."
|
5
|
+
s.description = s.summary
|
6
|
+
s.version = '0.1.0'
|
7
|
+
s.date = '2010-01-21'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Wes Oldenbeuving"]
|
10
|
+
s.email = "narnach@gmail.com"
|
11
|
+
s.homepage = "http://www.github.com/narnach/permutations"
|
12
|
+
|
13
|
+
# Files
|
14
|
+
root_files = %w[Readme.rdoc Rakefile permutations.gemspec init.rb MIT-LICENSE]
|
15
|
+
bin_files = []
|
16
|
+
lib_files = %w[permutations]
|
17
|
+
s.bindir = "bin"
|
18
|
+
s.require_path = "lib"
|
19
|
+
s.executables = bin_files
|
20
|
+
s.test_files = []
|
21
|
+
s.files = root_files + s.test_files + bin_files.map {|f| 'bin/%s' % f} + lib_files.map {|f| 'lib/%s.rb' % f}
|
22
|
+
|
23
|
+
# rdoc
|
24
|
+
s.has_rdoc = true
|
25
|
+
s.extra_rdoc_files = %w[ Readme.rdoc]
|
26
|
+
s.rdoc_options << '--inline-source' << '--line-numbers' << '--main' << 'Readme.rdoc'
|
27
|
+
|
28
|
+
# Requirements
|
29
|
+
s.required_ruby_version = ">= 1.8.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: permutations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Oldenbeuving
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-21 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Permutations is a library that allows you to generate permutations from Arrays and Strings.
|
17
|
+
email: narnach@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- Readme.rdoc
|
24
|
+
files:
|
25
|
+
- Readme.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- permutations.gemspec
|
28
|
+
- init.rb
|
29
|
+
- MIT-LICENSE
|
30
|
+
- lib/permutations.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://www.github.com/narnach/permutations
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --inline-source
|
38
|
+
- --line-numbers
|
39
|
+
- --main
|
40
|
+
- Readme.rdoc
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.0
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Permutations is a library that allows you to generate permutations from Arrays and Strings.
|
62
|
+
test_files: []
|
63
|
+
|