epitools 0.5.58 → 0.5.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f476bb597f8724e520b4d9c8dda3a051c4e3bb77
4
- data.tar.gz: 3974e9c35019dc899060337650af381aac362c83
3
+ metadata.gz: e9003ca555f667ebc3547afdcbe332b0718cb3e0
4
+ data.tar.gz: 83d00e82dc95355773c1f34744c6a1f24290f808
5
5
  SHA512:
6
- metadata.gz: ef2db918b34d5ecfc0ad79f3709947fd26c194488aa2371502cf69fffcf28c83c9a5949f8cba8230816abf38a31522bdd4e54f805e9f9e91f987304a6232af71
7
- data.tar.gz: f02ee7dacfdb34acfefd48bde5112dd097d18470963fbbfa5a7aba8a6761e373ace0ed98e9896923ace059d1145bd82491ec12990c88b1ab6c877700bad67627
6
+ metadata.gz: 776eca2bf207c715ef1ec9a0fa213e216bc640b152b356841b6873a8ecf66c2e0d0d5ca7a786f38c4b16edca14676f29a73644b7b82d38df31145938623e8071
7
+ data.tar.gz: 5400b2ad213c76e4e297838143af5c43010bd8c485aee789479b530b89781d73303b21ed784ec4cae2e1c67c7937c818f11057dda6ab44129465e9aaf794f2d7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.58
1
+ 0.5.59
@@ -289,6 +289,21 @@ module Enumerable
289
289
  result
290
290
  end
291
291
 
292
+
293
+ #
294
+ # See: Array#permutation
295
+ #
296
+ def permutation(*args, &block)
297
+ to_a.permutation(*args, &block)
298
+ end
299
+
300
+ #
301
+ # See: See Array#combination
302
+ #
303
+ def combination(*args, &block)
304
+ to_a.combination(*args, &block)
305
+ end
306
+
292
307
  #
293
308
  # Returns the powerset of the Enumerable
294
309
  #
@@ -123,6 +123,46 @@ class Numeric
123
123
  end
124
124
 
125
125
 
126
+ #
127
+ # Combinations: compute "n choose r" (self.choose(r))
128
+ #
129
+ # This represents number of ways to pick "r" items from a collection of "self"
130
+ # items (where the order of the items doesn't matter, and items can't be repeated.)
131
+ #
132
+ # eg: 49.choose(6) is how many ways can we pick 6 lottery numbers from a set of 49.
133
+ #
134
+ # Formula: n! / (r! * (n-r)!) == n * n-1 * ... * n-r / r * r-1 * ... * 2
135
+ #
136
+ def choose(r)
137
+ (self-r+1..self).reduce(:*) / (2..r).reduce(:*)
138
+ end
139
+ alias_method :combinations, :choose
140
+
141
+ #
142
+ # Permutations: compute "n P r"
143
+ #
144
+ # This represents number of ways to pick "r" items from a collection of "self"
145
+ # items (where the order of the items DOES matter, and items can't be repeated.)
146
+ #
147
+ # eg: 23.perm(3) is how many ways 23 people can win 1st, 2nd and 3rd place in a race.
148
+ #
149
+ # Formula: n! / (n - r)!
150
+ #
151
+ def perms(r)
152
+ (self-r+1..self).reduce(:*)
153
+ end
154
+ alias_method :permutations, :perms
155
+
156
+ #
157
+ # Multiply self by n, returning the integer product and the floating point remainder.
158
+ #
159
+ def mulmod(n)
160
+ prod = self * n
161
+ intprod = prod.to_i
162
+
163
+ [intprod, prod % intprod]
164
+ end
165
+
126
166
  # Math.log is different in 1.8
127
167
  if RUBY_VERSION["1.8"]
128
168
 
@@ -281,9 +321,7 @@ class Integer
281
321
  # Factorial (iterated style)
282
322
  #
283
323
  def fact
284
- total = 1
285
- self.downto(2) { |x| total *= x }
286
- total
324
+ (2..self).reduce(:*)
287
325
  end
288
326
  alias_method :factorial, :fact
289
327
 
@@ -147,15 +147,16 @@ class String
147
147
  end
148
148
 
149
149
  #
150
- # Indent all the lines in the string by "amount" of spaces.
150
+ # Indent all the lines, if "prefix" is a string, prepend that string
151
+ # to each lien. If it's an integer, prepend that many spaces.
151
152
  #
152
- def indent(prefix=" ", strip_ansi=true)
153
+ def indent(prefix=" ")
153
154
  prefix = (" " * prefix) if prefix.is_an? Integer
154
155
 
155
156
  if block_given?
156
157
  lines.each { |line| yield prefix + line }
157
158
  else
158
- lines.each { |line| prefix + line }.join ''
159
+ lines.map { |line| prefix + line }.join('')
159
160
  end
160
161
  end
161
162
 
@@ -67,8 +67,8 @@ describe Object do
67
67
  end
68
68
 
69
69
  it "nots" do
70
- 10.should be_even
71
- 10.not.should_not be_even
70
+ 10.even?.should == true
71
+ 10.not.even?.should == false
72
72
  end
73
73
 
74
74
  it "alias_class_methods" do
@@ -234,8 +234,10 @@ describe String do
234
234
  end
235
235
 
236
236
  it "indents" do
237
- s = "Some things\nNeed indenting\nSo, you indent them!\n"
238
- s.indent(2).should == " Some things\n Need indenting\n So, you indent them!\n"
237
+ s = "Some things\nNeed indenting\nSo, you indent them!\n"
238
+ indented_2 = " Some things\n Need indenting\n So, you indent them!\n"
239
+
240
+ s.indent(2).should == indented_2
239
241
  end
240
242
 
241
243
  it "titlecases" do
@@ -302,6 +304,25 @@ describe String do
302
304
  e.to_a.should == %w[he ll ot he re !]
303
305
  end
304
306
 
307
+ it "n choose r" do
308
+ n = 49
309
+ r = 6
310
+
311
+ n.choose(r).should == n.fact / (r.fact * (n-r).fact)
312
+ end
313
+
314
+ it "n perms r" do
315
+ n = 23
316
+ r = 3
317
+
318
+ n.perms(r).should == (n.fact / (n-r).fact)
319
+ end
320
+
321
+ it "mulmods" do
322
+ 1.25.mulmod(2).should == [2, 0.5]
323
+ 5.mulmod(2).should == [10, 0]
324
+ end
325
+
305
326
  end
306
327
 
307
328
 
@@ -510,6 +531,15 @@ describe Enumerable do
510
531
  [:a, :b, :c].to_enum.includes?(5).should == false
511
532
  end
512
533
 
534
+ it "permutatitons and combinations" do
535
+ 10.times.permutation(2).to_a.should == 10.times.to_a.permutation(2).to_a
536
+ 10.times.combination(2).to_a.should == 10.times.to_a.combination(2).to_a
537
+
538
+ result = []
539
+ 10.times.permutation(2) { |perm| result << perm }
540
+ result.should == 10.times.permutation(2).to_a
541
+ end
542
+
513
543
  end
514
544
 
515
545
  describe Enumerator do
@@ -26,20 +26,20 @@ describe Term do
26
26
  table.by_columns.should_not be_nil
27
27
  table.by_rows.should_not be_nil
28
28
 
29
- Term::Table do |table|
30
- 100.times do |n|
31
- table.row do
32
- col "#{n}."
33
- col "A" * rand(10)
34
- col "B" * rand(10)
35
- end
36
- end
37
- end
38
-
39
- Term::Table[
40
- [1,2,3],
41
- [4,5,6]
42
- ]
29
+ # Term::Table do |table|
30
+ # 100.times do |n|
31
+ # table.row do
32
+ # col "#{n}."
33
+ # col "A" * rand(10)
34
+ # col "B" * rand(10)
35
+ # end
36
+ # end
37
+ # end
38
+
39
+ # Term::Table[
40
+ # [1,2,3],
41
+ # [4,5,6]
42
+ # ]
43
43
 
44
44
  table = Term::Table.new
45
45
  table.rows = [ [1,2,3], [4,5,6] ]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.58
4
+ version: 0.5.59
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-07 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec