matrixtrails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +10 -0
- data/bin/matrixtrails +8 -0
- data/lib/matrixtrails.rb +5 -0
- data/lib/matrixtrails/core.rb +78 -0
- data/lib/matrixtrails/generator.rb +53 -0
- data/lib/matrixtrails/runner.rb +32 -0
- data/lib/matrixtrails/version.rb +3 -0
- data/matrixtrails.gemspec +24 -0
- data/test/test_spiral.rb +83 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab4ae4e0df4580b8376052e3bec4f3f2b484e5b8
|
4
|
+
data.tar.gz: 75966b1e80944cccd651921ed2daca2b0168c8bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26fb3375d50a0b138e191f339a89487c7aa101e5c0b674ee0891f241e1bc22a4b1299df06e18724f33de29b3d65f9eafdb00b03a76b89844cdfd245265cdf71e
|
7
|
+
data.tar.gz: 4dfcd19ad9997baff3fe6af259321dee96dc1bb9682f871b141919b95bb4d3889f7b0ff8af0fb91137fd7b3419336f73ef56202cdcf7013e8c98e483b46c8161
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 athom
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# MatrixTrails
|
2
|
+
|
3
|
+
A toy of matrix spiral trversing
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'matrixtrails'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install matrixtrails
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
- run with random data
|
22
|
+
|
23
|
+
matrixtrails 5 5
|
24
|
+
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/matrixtrails
ADDED
data/lib/matrixtrails.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
module MatrixTrails
|
2
|
+
class Core
|
3
|
+
OP_PLUS = 0
|
4
|
+
OP_MULTIPLY = 1
|
5
|
+
|
6
|
+
attr_reader :array
|
7
|
+
|
8
|
+
def initialize array_2d
|
9
|
+
@array = array_2d
|
10
|
+
@height = @array.length
|
11
|
+
@width = @height > 0 ? @array[0].length : 0
|
12
|
+
@operation = OP_PLUS
|
13
|
+
end
|
14
|
+
|
15
|
+
def spiral_indexes
|
16
|
+
positions = (0...@height).to_a.product((0...@width).to_a).to_a
|
17
|
+
|
18
|
+
def positions.trim_line(need_revert, index)
|
19
|
+
line = []
|
20
|
+
self.reject! do |pair|
|
21
|
+
if yield pair
|
22
|
+
line.push pair
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if need_revert
|
27
|
+
line.sort! do |pair1, pair2|
|
28
|
+
pair2[index] <=> pair1[index]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
line
|
32
|
+
end
|
33
|
+
|
34
|
+
trails = []
|
35
|
+
i = 0
|
36
|
+
until positions.empty?
|
37
|
+
trails += positions.trim_line(false, 0) {|pair| pair[0] == i}
|
38
|
+
trails += positions.trim_line(false, 1) {|pair| pair[1] == @width -1 -i}
|
39
|
+
trails += positions.trim_line(true, 1) {|pair| pair[0] == @height -1 -i}
|
40
|
+
trails += positions.trim_line(true, 0) {|pair| pair[1] == i}
|
41
|
+
i += 1
|
42
|
+
end
|
43
|
+
trails
|
44
|
+
end
|
45
|
+
|
46
|
+
def spiral_trails
|
47
|
+
trails = []
|
48
|
+
indexes = spiral_indexes
|
49
|
+
indexes.each do |step|
|
50
|
+
x = step[0]
|
51
|
+
y = step[1]
|
52
|
+
element = @array[x][y]
|
53
|
+
trails.push element
|
54
|
+
end
|
55
|
+
trails
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_operation op
|
59
|
+
@operation = op
|
60
|
+
end
|
61
|
+
|
62
|
+
def final_value
|
63
|
+
trails = spiral_trails
|
64
|
+
if trails.empty?
|
65
|
+
return 0
|
66
|
+
end
|
67
|
+
|
68
|
+
case @operation
|
69
|
+
when OP_PLUS
|
70
|
+
value = trails.inject(:+)
|
71
|
+
when OP_MULTIPLY
|
72
|
+
value = trails.inject(:*)
|
73
|
+
end
|
74
|
+
value
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'colored'
|
2
|
+
module MatrixTrails
|
3
|
+
class Generator
|
4
|
+
def initialize row, column
|
5
|
+
array = []
|
6
|
+
row.times do
|
7
|
+
col = []
|
8
|
+
column.times do
|
9
|
+
col.push(rand 100)
|
10
|
+
end
|
11
|
+
array.push col
|
12
|
+
end
|
13
|
+
@core = Core.new(array)
|
14
|
+
@history_pos = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def display
|
18
|
+
@core.spiral_indexes.each do |pos|
|
19
|
+
show_element(@core.array, pos)
|
20
|
+
sleep(1)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def is_printed? x, y
|
27
|
+
@history_pos.include? [x, y]
|
28
|
+
end
|
29
|
+
|
30
|
+
def is_current? pos, x, y
|
31
|
+
pos[0] == x and pos[1] == y
|
32
|
+
end
|
33
|
+
|
34
|
+
def need_separated_display? pos, x, y
|
35
|
+
is_current? pos, x, y or is_printed? x, y
|
36
|
+
end
|
37
|
+
|
38
|
+
def show_element(array, pos)
|
39
|
+
array.each_with_index do |row, x|
|
40
|
+
row.each_with_index do |e, y|
|
41
|
+
if need_separated_display? pos, x, y
|
42
|
+
print e.to_s.green, " "
|
43
|
+
@history_pos.push pos
|
44
|
+
else
|
45
|
+
print e, " "
|
46
|
+
end
|
47
|
+
end
|
48
|
+
puts ""
|
49
|
+
end
|
50
|
+
puts ""
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module MatrixTrails
|
4
|
+
class Runner
|
5
|
+
def initialize(arguments, stdin, stdout)
|
6
|
+
@arguments = arguments
|
7
|
+
parse_options
|
8
|
+
row = @arguments[0].to_i
|
9
|
+
column = @arguments[1].to_i
|
10
|
+
if row <=0 or column <= 0
|
11
|
+
puts @options
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
|
15
|
+
@game = Generator.new row, column
|
16
|
+
end
|
17
|
+
|
18
|
+
def run
|
19
|
+
#p @arguments
|
20
|
+
@game.display
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def parse_options
|
26
|
+
@options = OptionParser.new
|
27
|
+
@options.banner = "Usage: spiral <height> <width> [options]"
|
28
|
+
@options.on('-h', '--help', "Show this message") { puts(@options); exit }
|
29
|
+
@options.parse!(@arguments)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'matrixtrails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "matrixtrails"
|
8
|
+
spec.version = MatrixTrails::VERSION
|
9
|
+
spec.authors = ["athom"]
|
10
|
+
spec.email = ["athom@126.com"]
|
11
|
+
spec.description = %q{A demo program for matrix spiral traversing}
|
12
|
+
spec.summary = %q{A demo program for matrix spiral traversing}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
24
|
+
|
data/test/test_spiral.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
$LOAD_PATH << '.'
|
2
|
+
$LOAD_PATH.unshift(File.join( File.dirname(__FILE__), '..', 'lib' ))
|
3
|
+
require 'test/unit'
|
4
|
+
require 'matrixtrails'
|
5
|
+
|
6
|
+
include MatrixTrails
|
7
|
+
|
8
|
+
class TestMatrixTrails < Test::Unit::TestCase
|
9
|
+
def setup
|
10
|
+
@m1 = Core.new([
|
11
|
+
[12, 32, 9, 11, 34],
|
12
|
+
[8, 54, 76, 23, 7],
|
13
|
+
[27, 18, 25, 9, 43],
|
14
|
+
[11, 23, 78, 63, 19],
|
15
|
+
[9, 22, 56, 31, 5]
|
16
|
+
])
|
17
|
+
@m2 = Core.new([
|
18
|
+
[1,2,3],
|
19
|
+
[4,5,6],
|
20
|
+
[7,8,9],
|
21
|
+
[10,11,12],
|
22
|
+
])
|
23
|
+
@m3 = Core.new([
|
24
|
+
[]
|
25
|
+
])
|
26
|
+
@m4 = Core.new([
|
27
|
+
[1, 2, 3, 4]
|
28
|
+
])
|
29
|
+
@m5 = Core.new([
|
30
|
+
[1],
|
31
|
+
[2],
|
32
|
+
[3],
|
33
|
+
[4]
|
34
|
+
])
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
def test_spiral_indexes
|
39
|
+
assert_equal(@m1.spiral_indexes, [[0,0], [0,1], [0,2], [0,3], [0,4],
|
40
|
+
[1,4], [2,4], [3,4], [4,4],
|
41
|
+
[4,3], [4,2], [4,1], [4,0],
|
42
|
+
[3,0], [2,0], [1,0],
|
43
|
+
[1,1], [1,2], [1,3],
|
44
|
+
[2,3], [3,3], [3,2],
|
45
|
+
[3,1], [2,1], [2,2]])
|
46
|
+
assert_equal(@m2.spiral_indexes, [[0,0], [0,1], [0,2],
|
47
|
+
[1,2], [2,2], [3,2],
|
48
|
+
[3,1], [3,0],
|
49
|
+
[2,0], [1,0],
|
50
|
+
[1,1], [2,1]])
|
51
|
+
assert_equal(@m3.spiral_indexes, [])
|
52
|
+
assert_equal(@m4.spiral_indexes, [[0,0], [0,1], [0,2], [0,3]])
|
53
|
+
assert_equal(@m5.spiral_indexes, [[0,0], [1,0], [2,0], [3,0]])
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_spiral_trails
|
57
|
+
assert_equal(@m1.spiral_trails, [12,32,9,11,34,7,43,19,5,31,56,22,9,11,27,8,54,76,23,9,63,78,23,18,25])
|
58
|
+
assert_equal(@m2.spiral_trails, [1,2,3,6,9,12,11,10,7,4,5,8])
|
59
|
+
assert_equal(@m3.spiral_trails, [])
|
60
|
+
assert_equal(@m4.spiral_trails, [1,2,3,4])
|
61
|
+
assert_equal(@m5.spiral_trails, [1,2,3,4])
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_final_value
|
65
|
+
assert_equal(@m1.final_value, 12+32+9+11+34+7+43+19+5+31+56+22+9+11+27+8+54+76+23+9+63+78+23+18+25)
|
66
|
+
assert_equal(@m2.final_value, 1+2+3+6+9+12+11+10+7+4+5+8)
|
67
|
+
assert_equal(@m3.final_value, 0)
|
68
|
+
assert_equal(@m4.final_value, 1+2+3+4)
|
69
|
+
assert_equal(@m5.final_value, 1+2+3+4)
|
70
|
+
|
71
|
+
@m1.set_operation(Core::OP_MULTIPLY)
|
72
|
+
@m2.set_operation(Core::OP_MULTIPLY)
|
73
|
+
@m3.set_operation(Core::OP_MULTIPLY)
|
74
|
+
@m4.set_operation(Core::OP_MULTIPLY)
|
75
|
+
@m5.set_operation(Core::OP_MULTIPLY)
|
76
|
+
assert_equal(@m1.final_value, 12*32*9*11*34*7*43*19*5*31*56*22*9*11*27*8*54*76*23*9*63*78*23*18*25)
|
77
|
+
assert_equal(@m2.final_value, 1*2*3*6*9*12*11*10*7*4*5*8)
|
78
|
+
assert_equal(@m3.final_value, 0)
|
79
|
+
assert_equal(@m4.final_value, 1*2*3*4)
|
80
|
+
assert_equal(@m5.final_value, 1*2*3*4)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matrixtrails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- athom
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A demo program for matrix spiral traversing
|
42
|
+
email:
|
43
|
+
- athom@126.com
|
44
|
+
executables:
|
45
|
+
- matrixtrails
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/matrixtrails
|
55
|
+
- lib/matrixtrails.rb
|
56
|
+
- lib/matrixtrails/core.rb
|
57
|
+
- lib/matrixtrails/generator.rb
|
58
|
+
- lib/matrixtrails/runner.rb
|
59
|
+
- lib/matrixtrails/version.rb
|
60
|
+
- matrixtrails.gemspec
|
61
|
+
- test/test_spiral.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: A demo program for matrix spiral traversing
|
86
|
+
test_files:
|
87
|
+
- test/test_spiral.rb
|