permutation 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +4 -0
- data/README +123 -14
- data/Rakefile +17 -28
- data/VERSION +1 -1
- data/lib/permutation.rb +1 -1
- data/lib/permutation/version.rb +1 -1
- data/make_doc.rb +1 -1
- metadata +15 -10
- data/doc-main.txt +0 -120
- data/permutation.gemspec +0 -21
data/CHANGES
CHANGED
data/README
CHANGED
@@ -1,35 +1,144 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
=== Description
|
2
|
+
|
3
|
+
The Permutation class has a dual purpose: It can be used to create
|
4
|
+
permutations of a given size and to do some simple computations with/on
|
5
|
+
permutations. The instances of this class don't require much memory
|
6
|
+
because they don't include the permutation as a data structure. They
|
7
|
+
only save the information necessary to create the permutation if asked
|
8
|
+
to do so.
|
9
|
+
|
10
|
+
To generate permutations the ranking/unranking method described in [SS97]
|
11
|
+
is used. Because of Ruby's Bignum arithmetic it is useful also
|
12
|
+
for permutations of very large size.
|
13
|
+
|
14
|
+
=== Download
|
15
|
+
|
16
|
+
The latest version of <b>permutation</b> can be found at
|
17
|
+
|
18
|
+
* http://www.ping.de/~flori/
|
19
|
+
|
20
|
+
The homepage of this library is located at
|
21
|
+
|
22
|
+
* http://flori.github.com/permutation
|
23
|
+
|
24
|
+
== Installation
|
3
25
|
|
4
26
|
Just type into the command line as root:
|
5
27
|
|
6
28
|
# ruby install.rb
|
7
29
|
|
8
|
-
Or if you
|
9
|
-
|
30
|
+
Or if you use rubygems just type and rubygems fetches the gem and installs it
|
31
|
+
for you:
|
10
32
|
|
11
33
|
# gem install permutation
|
12
34
|
|
13
|
-
Documentation
|
14
|
-
=============
|
35
|
+
== Creating Documentation
|
15
36
|
|
16
|
-
To create the documentation of this module, type
|
37
|
+
To create the documentation of this module, type either
|
17
38
|
|
18
39
|
$ ruby make_doc.rb
|
19
40
|
|
41
|
+
or with rake installed
|
42
|
+
|
43
|
+
$ rake doc
|
44
|
+
|
20
45
|
and the API documentation is generated by your rdoc command in
|
21
46
|
the doc/ sub-directory.
|
22
47
|
|
23
|
-
|
48
|
+
The examples direcotry also contains a small demonstration that solves the
|
24
49
|
Traveling Salesman Problem (TSP) with this library.
|
25
50
|
|
26
|
-
|
27
|
-
|
51
|
+
=== Examples
|
52
|
+
|
53
|
+
In this section some examples show what can be done with this class.
|
54
|
+
|
55
|
+
Creating all permutations and project them on data:
|
56
|
+
|
57
|
+
perm = Permutation.new(3)
|
58
|
+
# => #<Permutation:0x57dc94 @last=5, @rank=0, @size=3>
|
59
|
+
perm.map { |p| p.value }
|
60
|
+
# => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
|
61
|
+
colors = [:r, :g, :b]
|
62
|
+
# => [:r, :g, :b]
|
63
|
+
perm.map { |p| p.project(colors) }
|
64
|
+
# => [[:r, :g, :b], [:r, :b, :g], [:g, :r, :b], [:g, :b, :r], [:b, :r, :g],
|
65
|
+
# [:b, :g, :r]]
|
66
|
+
string = "abc"# => "abc"
|
67
|
+
perm.map { |p| p.project(string) }
|
68
|
+
# => ["abc", "acb", "bac", "bca", "cab", "cba"]
|
69
|
+
|
70
|
+
Or perhaps more convenient to use:
|
71
|
+
|
72
|
+
perm = Permutation.for("abc")
|
73
|
+
perm.map { |p| p.project }
|
74
|
+
# => ["abc", "acb", "bac", "bca", "cab", "cba"]
|
75
|
+
|
76
|
+
Finding the successor and predecessor of Permutations or a
|
77
|
+
certain Permutation for a given rank:
|
78
|
+
|
79
|
+
perm = Permutation.new(7)
|
80
|
+
# => #<Permutation:0x8453c @rank=0, @size=7, @last=5039>
|
81
|
+
perm.succ!
|
82
|
+
# => #<Permutation:0x8453c @rank=1, @size=7, @last=5039>
|
83
|
+
perm.succ!
|
84
|
+
# => #<Permutation:0x8453c @rank=2, @size=7, @last=5039>
|
85
|
+
perm.succ!
|
86
|
+
# => #<Permutation:0x8453c @rank=3, @size=7, @last=5039>
|
87
|
+
perm.pred!
|
88
|
+
# => #<Permutation:0x8453c @rank=2, @size=7, @last=5039>
|
89
|
+
perm.rank = 3200
|
90
|
+
# => 3200
|
91
|
+
perm
|
92
|
+
# => #<Permutation:0x8453c @rank=3200, @size=7, @last=5039>
|
93
|
+
perm.value
|
94
|
+
# => [4, 2, 5, 1, 3, 0, 6]
|
95
|
+
|
96
|
+
Generating random Permutations
|
97
|
+
|
98
|
+
perm = Permutation.new(10)
|
99
|
+
# => #<Permutation:0x59f4c0 @rank=0, @size=10, @last=3628799>
|
100
|
+
perm.random!.value
|
101
|
+
# => [6, 4, 9, 7, 3, 5, 8, 1, 2, 0]
|
102
|
+
perm.random!.value
|
103
|
+
# => [3, 7, 6, 1, 4, 8, 9, 2, 5, 0]
|
104
|
+
perm.random!.value
|
105
|
+
# => [2, 8, 4, 9, 3, 5, 6, 7, 0, 1]
|
106
|
+
perm.random!.project("ABCDEFGHIJ")
|
107
|
+
# => "DFJGAEBCIH"
|
108
|
+
perm.random!.project("ABCDEFGHIJ")
|
109
|
+
# => "BFADEGHJCI"
|
110
|
+
|
111
|
+
Performing some mathematical operations on/with Permutations
|
112
|
+
|
113
|
+
p1 = Permutation.from_cycles([[1, 3, 2], [5, 7]], 10)
|
114
|
+
# => #<Permutation:0x593594 @rank=80694, @size=10, @last=3628799>
|
115
|
+
p2 = Permutation.from_value [3, 2, 0, 5, 6, 8, 9, 1, 4, 7]
|
116
|
+
# => #<Permutation:0x5897b0 @rank=1171050, @size=10, @last=3628799>
|
117
|
+
p3 = p1 * p2
|
118
|
+
# => #<Permutation:0x586a88 @rank=769410, @size=10, @last=3628799>
|
119
|
+
p3.value
|
120
|
+
# => [2, 1, 0, 7, 6, 8, 9, 3, 4, 5]
|
121
|
+
p3.cycles
|
122
|
+
# => [[0, 2], [3, 7], [4, 6, 9, 5, 8]]
|
123
|
+
p4 = p1 * -p2
|
124
|
+
# => #<Permutation:0x581a10 @rank=534725, @size=10, @last=3628799>
|
125
|
+
p4.value
|
126
|
+
# => [1, 5, 3, 0, 8, 2, 4, 9, 7, 6]
|
127
|
+
p4.cycles
|
128
|
+
# => [[0, 1, 5, 2, 3], [4, 8, 7, 9, 6]]
|
129
|
+
id = p1 * -p1
|
130
|
+
# => #<Permutation:0x583a7c @rank=0, @size=10, @last=3628799>
|
131
|
+
|
132
|
+
=== References
|
133
|
+
|
134
|
+
[SS97] The Algorithm Design Manual, Steven S. Skiena, Telos/Springer, 1997.
|
28
135
|
|
29
|
-
|
136
|
+
=== Author
|
30
137
|
|
31
|
-
|
32
|
-
=======
|
138
|
+
Florian Frank mailto:flori@ping.de
|
33
139
|
|
34
|
-
|
140
|
+
=== License
|
35
141
|
|
142
|
+
This is free software; you can redistribute it and/or modify it under the
|
143
|
+
terms of the GNU General Public License Version 2 as published by the Free
|
144
|
+
Software Foundation: www.gnu.org/copyleft/gpl.html
|
data/Rakefile
CHANGED
@@ -32,38 +32,27 @@ task :doc do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
if defined? Gem
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
s.summary = 'Permutation library in pure Ruby'
|
41
|
-
s.description = "Library to perform different operations with permutations of sequences (strings, arrays, etc.)"
|
35
|
+
spec = Gem::Specification.new do |s|
|
36
|
+
s.name = PKG_NAME
|
37
|
+
s.version = PKG_VERSION
|
38
|
+
s.summary = 'Permutation library in pure Ruby'
|
39
|
+
s.description = "Library to perform different operations with permutations of sequences (strings, arrays, etc.)"
|
42
40
|
|
43
|
-
|
41
|
+
s.files = PKG_FILES.to_a.sort
|
44
42
|
|
45
|
-
|
43
|
+
s.require_path = 'lib'
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
s.has_rdoc = true
|
46
|
+
s.rdoc_options << '--main' << 'README' << '--line-numbers'
|
47
|
+
s.extra_rdoc_files << 'README'
|
48
|
+
s.test_files << 'test/test.rb'
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
GEM
|
58
|
-
|
59
|
-
desc 'Create a gemspec file'
|
60
|
-
task :gemspec do
|
61
|
-
File.open("#{PKG_NAME}.gemspec", 'w') do |f|
|
62
|
-
f.puts spec_src
|
63
|
-
end
|
50
|
+
s.author = "Florian Frank"
|
51
|
+
s.email = "flori@ping.de"
|
52
|
+
s.homepage = "http://flori.github.com/#{PKG_NAME}"
|
53
|
+
s.rubyforge_project = "#{PKG_NAME}"
|
64
54
|
end
|
65
55
|
|
66
|
-
spec = eval(spec_src)
|
67
56
|
Rake::GemPackageTask.new(spec) do |pkg|
|
68
57
|
pkg.need_tar = true
|
69
58
|
pkg.package_files += PKG_FILES
|
@@ -87,6 +76,6 @@ EOT
|
|
87
76
|
end
|
88
77
|
end
|
89
78
|
|
90
|
-
task :default => [ :version, :
|
79
|
+
task :default => [ :version, :test ]
|
91
80
|
|
92
|
-
task :release => [ :clean, :version, :
|
81
|
+
task :release => [ :clean, :version, :package ]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.8
|
data/lib/permutation.rb
CHANGED
data/lib/permutation/version.rb
CHANGED
data/make_doc.rb
CHANGED
@@ -2,4 +2,4 @@
|
|
2
2
|
|
3
3
|
$outdir = 'doc/'
|
4
4
|
puts "Creating documentation in '#$outdir'."
|
5
|
-
system "rdoc -d -m
|
5
|
+
system "rdoc -d -m README -t 'Permutation library for Ruby' -o #$outdir #{(%w[README] + Dir['lib/**/*.rb']) * ' '}"
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permutation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 8
|
9
|
+
version: 0.1.8
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Florian Frank
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-16 00:00:00 +01:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -20,7 +25,7 @@ executables: []
|
|
20
25
|
extensions: []
|
21
26
|
|
22
27
|
extra_rdoc_files:
|
23
|
-
-
|
28
|
+
- README
|
24
29
|
files:
|
25
30
|
- CHANGES
|
26
31
|
- COPYING
|
@@ -32,17 +37,15 @@ files:
|
|
32
37
|
- lib/permutation.rb
|
33
38
|
- lib/permutation/version.rb
|
34
39
|
- make_doc.rb
|
35
|
-
- permutation.gemspec
|
36
40
|
- test/test.rb
|
37
|
-
- doc-main.txt
|
38
41
|
has_rdoc: true
|
39
|
-
homepage: http://
|
42
|
+
homepage: http://flori.github.com/permutation
|
40
43
|
licenses: []
|
41
44
|
|
42
45
|
post_install_message:
|
43
46
|
rdoc_options:
|
44
47
|
- --main
|
45
|
-
-
|
48
|
+
- README
|
46
49
|
- --line-numbers
|
47
50
|
require_paths:
|
48
51
|
- lib
|
@@ -50,18 +53,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
53
|
requirements:
|
51
54
|
- - ">="
|
52
55
|
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
53
58
|
version: "0"
|
54
|
-
version:
|
55
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
60
|
requirements:
|
57
61
|
- - ">="
|
58
62
|
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
59
65
|
version: "0"
|
60
|
-
version:
|
61
66
|
requirements: []
|
62
67
|
|
63
68
|
rubyforge_project: permutation
|
64
|
-
rubygems_version: 1.3.
|
69
|
+
rubygems_version: 1.3.6
|
65
70
|
signing_key:
|
66
71
|
specification_version: 3
|
67
72
|
summary: Permutation library in pure Ruby
|
data/doc-main.txt
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
== Permutation library for Ruby
|
2
|
-
|
3
|
-
=== Author
|
4
|
-
|
5
|
-
Florian Frank mailto:flori@ping.de
|
6
|
-
|
7
|
-
=== License
|
8
|
-
|
9
|
-
This is free software; you can redistribute it and/or modify it under the
|
10
|
-
terms of the GNU General Public License Version 2 as published by the Free
|
11
|
-
Software Foundation: www.gnu.org/copyleft/gpl.html
|
12
|
-
|
13
|
-
=== Download
|
14
|
-
|
15
|
-
The latest version of <b>permutation</b> can be found at
|
16
|
-
|
17
|
-
* http://rubyforge.org/frs/?group_id=291
|
18
|
-
|
19
|
-
The homepage of this library is located at
|
20
|
-
|
21
|
-
* http://permutation.rubyforge.org
|
22
|
-
|
23
|
-
=== Description
|
24
|
-
|
25
|
-
The Permutation class has a dual purpose: It can be used to create
|
26
|
-
permutations of a given size and to do some simple computations with/on
|
27
|
-
permutations. The instances of this class don't require much memory
|
28
|
-
because they don't include the permutation as a data structure. They
|
29
|
-
only save the information necessary to create the permutation if asked
|
30
|
-
to do so.
|
31
|
-
|
32
|
-
To generate permutations the ranking/unranking method described in [SS97]
|
33
|
-
is used. Because of Ruby's Bignum arithmetic it is useful also
|
34
|
-
for permutations of very large size.
|
35
|
-
|
36
|
-
=== Examples
|
37
|
-
|
38
|
-
In this section some examples show what can be done with this class.
|
39
|
-
|
40
|
-
Creating all permutations and project them on data:
|
41
|
-
|
42
|
-
perm = Permutation.new(3)
|
43
|
-
# => #<Permutation:0x57dc94 @last=5, @rank=0, @size=3>
|
44
|
-
perm.map { |p| p.value }
|
45
|
-
# => [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
|
46
|
-
colors = [:r, :g, :b]
|
47
|
-
# => [:r, :g, :b]
|
48
|
-
perm.map { |p| p.project(colors) }
|
49
|
-
# => [[:r, :g, :b], [:r, :b, :g], [:g, :r, :b], [:g, :b, :r], [:b, :r, :g],
|
50
|
-
# [:b, :g, :r]]
|
51
|
-
string = "abc"# => "abc"
|
52
|
-
perm.map { |p| p.project(string) }
|
53
|
-
# => ["abc", "acb", "bac", "bca", "cab", "cba"]
|
54
|
-
|
55
|
-
Or perhaps more convenient to use:
|
56
|
-
|
57
|
-
perm = Permutation.for("abc")
|
58
|
-
perm.map { |p| p.project }
|
59
|
-
# => ["abc", "acb", "bac", "bca", "cab", "cba"]
|
60
|
-
|
61
|
-
Finding the successor and predecessor of Permutations or a
|
62
|
-
certain Permutation for a given rank:
|
63
|
-
|
64
|
-
perm = Permutation.new(7)
|
65
|
-
# => #<Permutation:0x8453c @rank=0, @size=7, @last=5039>
|
66
|
-
perm.succ!
|
67
|
-
# => #<Permutation:0x8453c @rank=1, @size=7, @last=5039>
|
68
|
-
perm.succ!
|
69
|
-
# => #<Permutation:0x8453c @rank=2, @size=7, @last=5039>
|
70
|
-
perm.succ!
|
71
|
-
# => #<Permutation:0x8453c @rank=3, @size=7, @last=5039>
|
72
|
-
perm.pred!
|
73
|
-
# => #<Permutation:0x8453c @rank=2, @size=7, @last=5039>
|
74
|
-
perm.rank = 3200
|
75
|
-
# => 3200
|
76
|
-
perm
|
77
|
-
# => #<Permutation:0x8453c @rank=3200, @size=7, @last=5039>
|
78
|
-
perm.value
|
79
|
-
# => [4, 2, 5, 1, 3, 0, 6]
|
80
|
-
|
81
|
-
Generating random Permutations
|
82
|
-
|
83
|
-
perm = Permutation.new(10)
|
84
|
-
# => #<Permutation:0x59f4c0 @rank=0, @size=10, @last=3628799>
|
85
|
-
perm.random!.value
|
86
|
-
# => [6, 4, 9, 7, 3, 5, 8, 1, 2, 0]
|
87
|
-
perm.random!.value
|
88
|
-
# => [3, 7, 6, 1, 4, 8, 9, 2, 5, 0]
|
89
|
-
perm.random!.value
|
90
|
-
# => [2, 8, 4, 9, 3, 5, 6, 7, 0, 1]
|
91
|
-
perm.random!.project("ABCDEFGHIJ")
|
92
|
-
# => "DFJGAEBCIH"
|
93
|
-
perm.random!.project("ABCDEFGHIJ")
|
94
|
-
# => "BFADEGHJCI"
|
95
|
-
|
96
|
-
Performing some mathematical operations on/with Permutations
|
97
|
-
|
98
|
-
p1 = Permutation.from_cycles([[1, 3, 2], [5, 7]], 10)
|
99
|
-
# => #<Permutation:0x593594 @rank=80694, @size=10, @last=3628799>
|
100
|
-
p2 = Permutation.from_value [3, 2, 0, 5, 6, 8, 9, 1, 4, 7]
|
101
|
-
# => #<Permutation:0x5897b0 @rank=1171050, @size=10, @last=3628799>
|
102
|
-
p3 = p1 * p2
|
103
|
-
# => #<Permutation:0x586a88 @rank=769410, @size=10, @last=3628799>
|
104
|
-
p3.value
|
105
|
-
# => [2, 1, 0, 7, 6, 8, 9, 3, 4, 5]
|
106
|
-
p3.cycles
|
107
|
-
# => [[0, 2], [3, 7], [4, 6, 9, 5, 8]]
|
108
|
-
p4 = p1 * -p2
|
109
|
-
# => #<Permutation:0x581a10 @rank=534725, @size=10, @last=3628799>
|
110
|
-
p4.value
|
111
|
-
# => [1, 5, 3, 0, 8, 2, 4, 9, 7, 6]
|
112
|
-
p4.cycles
|
113
|
-
# => [[0, 1, 5, 2, 3], [4, 8, 7, 9, 6]]
|
114
|
-
id = p1 * -p1
|
115
|
-
# => #<Permutation:0x583a7c @rank=0, @size=10, @last=3628799>
|
116
|
-
|
117
|
-
=== References
|
118
|
-
|
119
|
-
[SS97] The Algorithm Design Manual, Steven S. Skiena, Telos/Springer, 1997.
|
120
|
-
|
data/permutation.gemspec
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
Gem::Specification.new do |s|
|
3
|
-
s.name = 'permutation'
|
4
|
-
s.version = '0.1.7'
|
5
|
-
s.summary = 'Permutation library in pure Ruby'
|
6
|
-
s.description = "Library to perform different operations with permutations of sequences (strings, arrays, etc.)"
|
7
|
-
|
8
|
-
s.files = ["CHANGES", "COPYING", "README", "Rakefile", "VERSION", "examples", "examples/tsp.rb", "install.rb", "lib", "lib/permutation", "lib/permutation.rb", "lib/permutation/version.rb", "make_doc.rb", "permutation.gemspec", "test", "test/test.rb"]
|
9
|
-
|
10
|
-
s.require_path = 'lib'
|
11
|
-
|
12
|
-
s.has_rdoc = true
|
13
|
-
s.rdoc_options << '--main' << 'doc-main.txt' << '--line-numbers'
|
14
|
-
s.extra_rdoc_files << 'doc-main.txt'
|
15
|
-
s.test_files << 'test/test.rb'
|
16
|
-
|
17
|
-
s.author = "Florian Frank"
|
18
|
-
s.email = "flori@ping.de"
|
19
|
-
s.homepage = "http://permutation.rubyforge.org"
|
20
|
-
s.rubyforge_project = "permutation"
|
21
|
-
end
|