five_leaves 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +31 -0
- data/lib/five_leaves/items_following.rb +7 -0
- data/lib/five_leaves/lazy_enum.rb +7 -5
- data/lib/five_leaves/maximize.rb +4 -2
- data/lib/five_leaves/palindromic.rb +5 -3
- data/lib/five_leaves/prime_util.rb +16 -14
- data/lib/five_leaves/range_util.rb +3 -3
- data/test/items_following_test.rb +17 -0
- data/test/lazy_enum_test.rb +3 -3
- data/test/maximize_test.rb +4 -4
- data/test/palindromic_test.rb +6 -6
- data/test/prime_util_test.rb +16 -16
- metadata +8 -2
data/README.md
CHANGED
@@ -2,6 +2,37 @@
|
|
2
2
|
|
3
3
|
a collection of useful ruby methods
|
4
4
|
|
5
|
+
## Examples
|
6
|
+
|
7
|
+
For more examples, see the [unit tests](https://github.com/mattraibert/five_leaves/blob/master/test/)
|
8
|
+
|
9
|
+
|
10
|
+
```
|
11
|
+
require 'five_leaves/lazy_enum'
|
12
|
+
|
13
|
+
def test_lazy_select_can_select_from_infinite_series
|
14
|
+
assert_equal [0, 2, 4, 6, 8], (0..(1.0/0.0)).lazy_select { |x| x % 2 == 0 }.take(5)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_lazy_map_can_map_inifinite_series
|
18
|
+
assert_equal [0, 2, 4, 6, 8], (0..(1.0/0.0)).lazy_map { |x| x * 2 }.take(5)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_series
|
22
|
+
assert_equal [0, 2, 4, 6, 8], series { |x| x * 2 }.take(5)
|
23
|
+
assert_equal [2, 4, 6, 8, 10], series(1) { |x| x * 2 }.take(5)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_lazy_zip_does
|
27
|
+
zip = series { |x| x }.lazy_zip(series { |x| x+1 })
|
28
|
+
assert_equal [0, 1], zip.next
|
29
|
+
assert_equal [1, 2], zip.next
|
30
|
+
end
|
31
|
+
```
|
32
|
+
## Naming
|
33
|
+
|
34
|
+
Five Leaves is named for a [restaurant](http://www.fiveleavesny.com/) in Brooklyn, NY.
|
35
|
+
|
5
36
|
## Copyright
|
6
37
|
|
7
38
|
Copyright (c) 2012 Matt Raibert.
|
@@ -26,10 +26,12 @@ class Enumerator
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
(init
|
31
|
-
|
29
|
+
module FiveLeaves
|
30
|
+
def self.series(init=0)
|
31
|
+
(init..(1.0/0.0)).lazy_map { |x| yield x }
|
32
|
+
end
|
32
33
|
|
33
|
-
def find(init=0)
|
34
|
-
|
34
|
+
def self.find(init=0)
|
35
|
+
(init..(1.0/0.0)).find { |x| yield x }
|
36
|
+
end
|
35
37
|
end
|
data/lib/five_leaves/maximize.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'five_leaves/lazy_enum'
|
2
2
|
|
3
|
+
module FiveLeaves
|
3
4
|
#find the input in the range which maximizes the result of the block
|
4
|
-
def maximize(range = 1..1_000_000)
|
5
|
-
|
5
|
+
def self.maximize(range = 1..1_000_000)
|
6
|
+
(range).lazy_map { |x| [yield(x), x] }.max[1]
|
7
|
+
end
|
6
8
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'five_leaves/enum_util'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
module FiveLeaves
|
4
|
+
def self.palindromic?(n)
|
5
|
+
string = n.to_s
|
6
|
+
(0..string.size/2).all_match? { |i| string[i] == string[-1-i] }
|
7
|
+
end
|
6
8
|
end
|
@@ -4,24 +4,26 @@ class Integer
|
|
4
4
|
end
|
5
5
|
|
6
6
|
def prime?
|
7
|
-
self > 1 && small_factors(self).size == 1
|
7
|
+
self > 1 && FiveLeaves.small_factors(self).size == 1
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
module FiveLeaves
|
12
|
+
def self.small_factors(n)
|
13
|
+
(Math.sqrt(n).truncate.downto 1).select { |x| x.divides? n }
|
14
|
+
end
|
14
15
|
|
15
|
-
def proper_factors(n)
|
16
|
-
|
17
|
-
|
18
|
-
end
|
16
|
+
def self.proper_factors(n)
|
17
|
+
sf = small_factors(n)
|
18
|
+
(sf.map { |x| n / x } + sf).uniq - [n]
|
19
|
+
end
|
19
20
|
|
20
|
-
def factors(n)
|
21
|
-
|
22
|
-
end
|
21
|
+
def self.factors(n)
|
22
|
+
[n] + proper_factors(n)
|
23
|
+
end
|
23
24
|
|
24
|
-
def prime_factors(n)
|
25
|
-
|
26
|
-
|
25
|
+
def self.prime_factors(n)
|
26
|
+
small = small_factors(n)
|
27
|
+
(small.map { |f| n / f } + small).uniq.select { |x| x.prime? }
|
28
|
+
end
|
27
29
|
end
|
@@ -18,15 +18,15 @@ class Range
|
|
18
18
|
stutter.lazy_zip squared
|
19
19
|
end
|
20
20
|
|
21
|
-
def *
|
21
|
+
def *(n)
|
22
22
|
(size * n).times.lazy_map { |x| first + (x % size) }
|
23
23
|
end
|
24
24
|
|
25
|
-
def **
|
25
|
+
def **(n)
|
26
26
|
(size * n).times.lazy_map { |x| first + (x / n) }
|
27
27
|
end
|
28
28
|
|
29
|
-
def pair_with
|
29
|
+
def pair_with(range)
|
30
30
|
(self * range.size).lazy_zip(range ** self.size)
|
31
31
|
end
|
32
32
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'five_leaves/items_following'
|
3
|
+
|
4
|
+
class ItemsFollowingTest < MiniTest::Unit::TestCase
|
5
|
+
def test_items_following
|
6
|
+
data = %w(I want text that follows this THESE TWO NOT THIS)
|
7
|
+
result = []
|
8
|
+
data.items_following(/this/) do |items|
|
9
|
+
result << items[0]
|
10
|
+
result << items[1]
|
11
|
+
end
|
12
|
+
assert result.include? "THESE"
|
13
|
+
assert result.include? "TWO"
|
14
|
+
assert !result.include?("NOT")
|
15
|
+
assert !result.include?("this")
|
16
|
+
end
|
17
|
+
end
|
data/test/lazy_enum_test.rb
CHANGED
@@ -11,12 +11,12 @@ class LazyEnumTest < MiniTest::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_series
|
14
|
-
assert_equal [0, 2, 4, 6, 8], series { |x| x * 2 }.take(5)
|
15
|
-
assert_equal [2, 4, 6, 8, 10], series(1) { |x| x * 2 }.take(5)
|
14
|
+
assert_equal [0, 2, 4, 6, 8], FiveLeaves.series { |x| x * 2 }.take(5)
|
15
|
+
assert_equal [2, 4, 6, 8, 10], FiveLeaves.series(1) { |x| x * 2 }.take(5)
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_lazy_zip_does
|
19
|
-
zip = series { |x| x }.lazy_zip(series { |x| x+1 })
|
19
|
+
zip = FiveLeaves.series { |x| x }.lazy_zip(FiveLeaves.series { |x| x+1 })
|
20
20
|
assert_equal [0, 1], zip.next
|
21
21
|
assert_equal [1, 2], zip.next
|
22
22
|
end
|
data/test/maximize_test.rb
CHANGED
@@ -3,9 +3,9 @@ require 'five_leaves/maximize'
|
|
3
3
|
|
4
4
|
class MaximizeTest < MiniTest::Unit::TestCase
|
5
5
|
def test_maximize
|
6
|
-
assert_equal 5, maximize(-10..10) { |x| (x - 5) * (-x + 5) }
|
7
|
-
assert_equal 5, maximize(-10..10) { |x| -x*x + 10*x }
|
8
|
-
assert_equal 10, maximize(-10..10) { |x| -x*x + 25*x }
|
9
|
-
assert_equal -10, maximize(-10..10) { |x| -x*x - 25*x }
|
6
|
+
assert_equal 5, FiveLeaves.maximize(-10..10) { |x| (x - 5) * (-x + 5) }
|
7
|
+
assert_equal 5, FiveLeaves.maximize(-10..10) { |x| -x*x + 10*x }
|
8
|
+
assert_equal 10, FiveLeaves.maximize(-10..10) { |x| -x*x + 25*x }
|
9
|
+
assert_equal -10, FiveLeaves.maximize(-10..10) { |x| -x*x - 25*x }
|
10
10
|
end
|
11
11
|
end
|
data/test/palindromic_test.rb
CHANGED
@@ -3,11 +3,11 @@ require 'five_leaves/palindromic'
|
|
3
3
|
|
4
4
|
class PalindromicTest < MiniTest::Unit::TestCase
|
5
5
|
def test_palindrome
|
6
|
-
assert(palindromic?(nil))
|
7
|
-
assert(palindromic?(9009))
|
8
|
-
assert(!palindromic?(1234))
|
9
|
-
assert(!palindromic?(1224))
|
10
|
-
assert(palindromic?(11211))
|
11
|
-
assert(!palindromic?(11238))
|
6
|
+
assert(FiveLeaves.palindromic?(nil))
|
7
|
+
assert(FiveLeaves.palindromic?(9009))
|
8
|
+
assert(!FiveLeaves.palindromic?(1234))
|
9
|
+
assert(!FiveLeaves.palindromic?(1224))
|
10
|
+
assert(FiveLeaves.palindromic?(11211))
|
11
|
+
assert(!FiveLeaves.palindromic?(11238))
|
12
12
|
end
|
13
13
|
end
|
data/test/prime_util_test.rb
CHANGED
@@ -3,31 +3,31 @@ require 'five_leaves/prime_util'
|
|
3
3
|
|
4
4
|
class PrimeUtilTest < MiniTest::Unit::TestCase
|
5
5
|
def test_small_factors_skips_factors_above_sqrt
|
6
|
-
assert_equal [2, 1], small_factors(6)
|
7
|
-
assert_equal [2, 1], small_factors(14)
|
6
|
+
assert_equal [2, 1], FiveLeaves.small_factors(6)
|
7
|
+
assert_equal [2, 1], FiveLeaves.small_factors(14)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_factors_finds_all_factors
|
11
|
-
assert_equal [6, 3, 2, 1], factors(6)
|
12
|
-
assert_equal [9, 3, 1], factors(9)
|
13
|
-
assert_equal [14, 7, 2, 1], factors(14)
|
14
|
-
assert_equal [25, 5, 1], factors(25)
|
11
|
+
assert_equal [6, 3, 2, 1], FiveLeaves.factors(6)
|
12
|
+
assert_equal [9, 3, 1], FiveLeaves.factors(9)
|
13
|
+
assert_equal [14, 7, 2, 1], FiveLeaves.factors(14)
|
14
|
+
assert_equal [25, 5, 1], FiveLeaves.factors(25)
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_proper_factors
|
18
|
-
assert_equal [], proper_factors(1)
|
19
|
-
assert_equal [1], proper_factors(2)
|
20
|
-
assert_equal [3, 2, 1], proper_factors(6)
|
21
|
-
assert_equal [3, 1], proper_factors(9)
|
22
|
-
assert_equal [7, 2, 1], proper_factors(14)
|
23
|
-
assert_equal [5, 1], proper_factors(25)
|
18
|
+
assert_equal [], FiveLeaves.proper_factors(1)
|
19
|
+
assert_equal [1], FiveLeaves.proper_factors(2)
|
20
|
+
assert_equal [3, 2, 1], FiveLeaves.proper_factors(6)
|
21
|
+
assert_equal [3, 1], FiveLeaves.proper_factors(9)
|
22
|
+
assert_equal [7, 2, 1], FiveLeaves.proper_factors(14)
|
23
|
+
assert_equal [5, 1], FiveLeaves.proper_factors(25)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_prime_factors_finds_all_prime_factors
|
27
|
-
assert_equal [3, 2], prime_factors(6)
|
28
|
-
assert_equal [7, 2], prime_factors(14)
|
29
|
-
assert_equal [5], prime_factors(25)
|
30
|
-
assert_equal [7, 2], prime_factors(28)
|
27
|
+
assert_equal [3, 2], FiveLeaves.prime_factors(6)
|
28
|
+
assert_equal [7, 2], FiveLeaves.prime_factors(14)
|
29
|
+
assert_equal [5], FiveLeaves.prime_factors(25)
|
30
|
+
assert_equal [7, 2], FiveLeaves.prime_factors(28)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: five_leaves
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A collection of methods for creating series and iterators
|
15
15
|
email: mattraibert@gmail.com
|
@@ -25,11 +25,13 @@ files:
|
|
25
25
|
- lib/five_leaves/sum.rb
|
26
26
|
- lib/five_leaves/range_util.rb
|
27
27
|
- lib/five_leaves/maximize.rb
|
28
|
+
- lib/five_leaves/items_following.rb
|
28
29
|
- lib/five_leaves/lazy_enum.rb
|
29
30
|
- lib/five_leaves/digits.rb
|
30
31
|
- lib/five_leaves/enum_util.rb
|
31
32
|
- lib/five_leaves/palindromic.rb
|
32
33
|
- test/maximize_test.rb
|
34
|
+
- test/items_following_test.rb
|
33
35
|
- test/test_helper.rb
|
34
36
|
- test/range_util_test.rb
|
35
37
|
- test/lazy_enum_test.rb
|
@@ -51,6 +53,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
53
|
- - ! '>='
|
52
54
|
- !ruby/object:Gem::Version
|
53
55
|
version: '0'
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
hash: 3540236943800449797
|
54
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
60
|
none: false
|
56
61
|
requirements:
|
@@ -65,6 +70,7 @@ specification_version: 3
|
|
65
70
|
summary: Some helpful methods
|
66
71
|
test_files:
|
67
72
|
- test/maximize_test.rb
|
73
|
+
- test/items_following_test.rb
|
68
74
|
- test/test_helper.rb
|
69
75
|
- test/range_util_test.rb
|
70
76
|
- test/lazy_enum_test.rb
|