two_d 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/two_d.rb +55 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68e4439e864914f0c4c411b282f6adf70695fde2
4
+ data.tar.gz: e74fc90ea26e286f4c31c652f25f332d3a9b1954
5
+ SHA512:
6
+ metadata.gz: 44c75e23002a4ad593368b4fd163173c3e9d28e6e8c3a144f40ebc7a27a21c0cb940c285f58951846385a1ef41e11106bc51081e90dab60e613ec688bfe1fd12
7
+ data.tar.gz: 6e2309cbaf26ae52d2463a6664a92a0febdedf68b7534f578cb9166f5e5009a5975efcf09e5371bded14a97b2bdab35aead43c0bdfc737842593654306d9d69a
data/lib/two_d.rb ADDED
@@ -0,0 +1,55 @@
1
+ # @author Paul Stein apexrubydev@gmail.com
2
+ #
3
+ # The method called by the user. Routes the args and array to one of the other functions.
4
+ # @param [Array] The array to be checked for the terms.
5
+ # @param [Object] There can be a single term, or an array of terms.
6
+ def two_d(array_2d, term)
7
+ if term.is_a?(Array)
8
+ two_dm(array_2d, term)
9
+ else
10
+ two_ds(array_2d, term)
11
+ end
12
+ end
13
+
14
+ # The method called by 'two_d' if there was a SINGLE term.
15
+ # @param [Array] The 2D array to be checked for the term.
16
+ # @param [Object] A single term.
17
+ # @return [Hash] A hash with the term as key and an array of indices.
18
+ def two_ds(array_2d, term)
19
+ # Find the sub arrays which include the term.
20
+ f_index_array = []
21
+ array_2d.each do |line|
22
+ result = line.include?(term)
23
+ if result == true
24
+ f_index_array << array_2d.index(line)
25
+ end
26
+ end
27
+
28
+ # Create an array of arrays with the indices in [row, col] order.
29
+ combined_index_array = []
30
+ f_index_array.each do |first|
31
+ counter = 0
32
+ array_2d[first].each do |item|
33
+ result = item == term
34
+ if result == true
35
+ combined_index_array << [first,counter]
36
+ end
37
+ counter += 1
38
+ end
39
+ end
40
+ # Make a hash from the term and the array of indices for that term in the origional 2D array.
41
+ return {term => combined_index_array}
42
+ end
43
+
44
+ # The method called by 'two_d' if there was an ARRAY of terms.
45
+ # @param [Array] The 2D array to be checked for the term.
46
+ # @param [Array] The array of terms.
47
+ # @return [Hash] A hash of all terms and their indices in {term => [[index],[index]...]} format.
48
+ # Simply passes each term in the term array to two_ds and merges the returned hash into the combined hash.
49
+ def two_dm(array_2d, terms)
50
+ results = Hash.new {}
51
+ terms.each do |term|
52
+ results = results.merge(two_d(array_2d,term))
53
+ end
54
+ return results
55
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: two_d
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Stein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A method which, when passed a term or array of terms, returns a hash
14
+ as { term => [[row,column]] }.
15
+ email: apexrubydev@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/two_d.rb
21
+ homepage: http://rubygems.org/gems/two_d
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Returns indices from 2D arrays.
44
+ test_files: []