array_pair 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/array_pair.rb +26 -12
  3. data/test/test_array_pair.rb +26 -8
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/lib/array_pair.rb CHANGED
@@ -25,13 +25,13 @@ class Array
25
25
  pairs = []
26
26
 
27
27
  if another_array #between this array and the next
28
- (0..length-1).each do |index1|
29
- (0..another_array.length-1).each do |index2|
28
+ (0..length-1).each do |index1|
29
+ (0..another_array.length-1).each do |index2|
30
30
  pairs.push [self[index1], another_array[index2]]
31
31
  end
32
32
  end
33
33
  else # within this array only
34
- (0..length-1).each do |index1|
34
+ (0..length-1).each do |index1|
35
35
  index2 = index1+1
36
36
  while index2 < length
37
37
  pairs.push [self[index1], self[index2]]
@@ -39,7 +39,7 @@ class Array
39
39
  end
40
40
  end
41
41
  end
42
-
42
+
43
43
  return pairs
44
44
  end
45
45
 
@@ -134,7 +134,7 @@ class Array
134
134
  if columns_to_normalise.nil? or columns_to_normalise.include?(index)
135
135
  minima = column_minima[index]
136
136
  maxima = column_maxima[index]
137
-
137
+
138
138
  if col.nil?
139
139
  new_row.push nil
140
140
  elsif minima == maxima
@@ -170,15 +170,15 @@ class Array
170
170
  return '()' if empty?
171
171
  return "('#{join("','")}')"
172
172
  end
173
-
173
+
174
174
  # For SQL conditions. In [] brackets, single quotes don't work.
175
175
  # ['a','bc'] => "(a,bc)"
176
176
  def to_sql_in_string_no_quotes
177
177
  return '()' if empty?
178
178
  return "(#{join(",")})"
179
179
  end
180
-
181
-
180
+
181
+
182
182
  def median
183
183
  return nil unless length>0
184
184
  a = sort
@@ -188,12 +188,12 @@ class Array
188
188
  return a[length/2]
189
189
  end
190
190
  end
191
-
191
+
192
192
  def standard_deviation
193
193
  return nil if empty?
194
194
  RSRuby.instance.sd(self)
195
195
  end
196
-
196
+
197
197
  # Similar to pairs(another_array) iterator, in that you iterate over 2
198
198
  # pairs of elements. However, here only the one array (the 'this' Enumerable)
199
199
  # and the names of these are from the names
@@ -206,7 +206,21 @@ class Array
206
206
  end
207
207
  end
208
208
  end
209
-
209
+
210
+ # Like each_lower_triangular_matrix, except iterate over 3 items at once, not 2.
211
+ # So for [1,2,3,4] you should get (in succession), 123, 124, 134, 234
212
+ def each_lower_triangular_3d_matrix
213
+ each_with_index do |e1, i1|
214
+ each_with_index do |e2, i2|
215
+ next unless i2 > i1
216
+ each_with_index do |e3, i3|
217
+ next unless i3 > i2
218
+ yield e1, e2, e3
219
+ end
220
+ end
221
+ end
222
+ end
223
+
210
224
  # like uniq -c for unix
211
225
  def uniq_count
212
226
  hash = {}
@@ -216,7 +230,7 @@ class Array
216
230
  end
217
231
  hash
218
232
  end
219
-
233
+
220
234
  def no_nils
221
235
  reject do |element|
222
236
  element.nil?
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # To change this template, choose Tools | Templates
3
3
  # and open the template in the editor.
4
-
4
+
5
5
 
6
6
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')
7
7
 
@@ -63,28 +63,28 @@ class ArrayPairTest < Test::Unit::TestCase
63
63
  assert_equal '()', [].to_sql_in_string
64
64
  assert_equal "('a','bc')", ['a','bc'].to_sql_in_string
65
65
  end
66
-
66
+
67
67
  def test_median
68
68
  assert_equal nil, [].median
69
69
  assert_equal 1, [1].median
70
70
  assert_equal 1.5, [0,3,2,1].median
71
71
  assert_equal 2, [1,2,3].median
72
72
  end
73
-
73
+
74
74
  def test_each_lower_triangle_iterator
75
75
  gained = []
76
76
  [].each_lower_triangular_matrix() { |i,j| gained.push([i,j]) }
77
77
  assert_equal [], gained
78
-
78
+
79
79
  gained = []
80
80
  [1].each_lower_triangular_matrix() { |i,j| gained.push([i,j]) }
81
81
  assert_equal [], gained
82
-
82
+
83
83
  gained = []
84
84
  [1,2].each_lower_triangular_matrix() { |i,j| gained.push([i,j]) }
85
85
  assert_equal [1,2], gained[0]
86
86
  assert_equal 1, gained.length
87
-
87
+
88
88
  gained = []
89
89
  [1,2,3].each_lower_triangular_matrix() { |i,j| gained.push([i,j]) }
90
90
  assert_equal [1,2], gained[0]
@@ -92,13 +92,31 @@ class ArrayPairTest < Test::Unit::TestCase
92
92
  assert_equal [2,3], gained[2]
93
93
  assert_equal 3, gained.length
94
94
  end
95
-
95
+
96
+ def test_each_lower_triangular_3d_matrix
97
+ gained = []
98
+ [].each_lower_triangular_3d_matrix { |i,j,k| gained.push([i,j,k]) }
99
+ assert_equal [], gained
100
+
101
+ gained = []
102
+ [1,2,3].each_lower_triangular_3d_matrix { |i,j,k| gained.push([i,j,k]) }
103
+ assert_equal [[1,2,3]], gained
104
+
105
+ gained = []
106
+ [1,2,3,4].each_lower_triangular_3d_matrix { |i,j,k| gained.push([i,j,k]) }
107
+ assert_equal [1,2,3], gained[0]
108
+ assert_equal [1,2,4], gained[1]
109
+ assert_equal [1,3,4], gained[2]
110
+ assert_equal [2,3,4], gained[3]
111
+ assert_equal 4, gained.length
112
+ end
113
+
96
114
  def test_standard_deviation
97
115
  assert_nil [].standard_deviation
98
116
  assert_equal 1.0, [1,2,3].standard_deviation
99
117
  assert_equal 129, ([1,2,3,4].standard_deviation*100).round
100
118
  end
101
-
119
+
102
120
  def test_uniq_count
103
121
  assert_equal({}, [].uniq_count)
104
122
  assert_equal({'a' => 1}, ['a'].uniq_count)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_pair
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben J Woodcroft
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-11 00:00:00 +11:00
12
+ date: 2010-02-16 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency