fraction-tree 2.0.1 → 2.1.0

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 +4 -4
  2. data/lib/fraction_tree/node.rb +11 -8
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afa73aeb4956a309b6e03e8d58426e407164e4697280b1d27f672a4e73963eb5
4
- data.tar.gz: a643a87bd78aee13fe93d5d0e28322b5cfbc5c46b67e7617e0e217160461f635
3
+ metadata.gz: 6a5099eb066c0e5fae9c4553a44b75ca90b52db8c4711b7d7a07247d116e93ea
4
+ data.tar.gz: 20624486f4b7987331ce3abc1f724a183212d3d403ab207d71a7bdbcf081c3a7
5
5
  SHA512:
6
- metadata.gz: 3dca9f6465ebbc4ce801c48f7dec3fa8e1e2ed98742afd997b6cce5e7c2dcb46588ce49e9f786c9ac5166b20e765f6d0be94844ec1fb85a7661f9c19ce3ef29c
7
- data.tar.gz: bb93ac7938caa3d50950295449e386e4eed308fb0d1d4228ed9e62139e210017174c03c3ad1840bb983f7d46c45c93bd33a4c7b3ae9d2bbed667c5523d9693b0
6
+ metadata.gz: a9abac107904f305c8a23fd34a4fb838b35ddca05046c7713a1c823c82848b85b914c055b2c1a24b3e2b440cfc04d23ad95fd312df4351d7244cfad0c5850a61
7
+ data.tar.gz: bfac6032e8f9b958e3f766bb16e7160a5ddb56fe2234e6fc79df6fe4317eb823cdcbb374508d1fbd3172b27334a7921a1c0f0c33b82e07ec767c8fc98c116952
@@ -42,8 +42,9 @@ class FractionTree
42
42
  # @return [String] the Stern-Brocot encoding of number
43
43
  # @example
44
44
  # FractionTree::Node.encode(4/3r) => "RLL"
45
+ # @param limit of codes to generate
45
46
  #
46
- def encode(number)
47
+ def encode(number, limit: Float::INFINITY)
47
48
  return nil if (number.infinite? || number.zero?)
48
49
 
49
50
  m = number.numerator
@@ -52,7 +53,7 @@ class FractionTree
52
53
  return "I" if m == n
53
54
 
54
55
  "".tap do |string|
55
- while m != n
56
+ while m != n && string.length < limit
56
57
  if m < n
57
58
  string << "L"
58
59
  n = n - m
@@ -90,8 +91,9 @@ class FractionTree
90
91
  # @example
91
92
  # FractionTree.node(7/4r).path
92
93
  # => [(0/1), (1/0), (1/1), (2/1), (3/2), (5/3), (7/4)]
94
+ # @param limit of nodes to generate
93
95
  #
94
- def path
96
+ def path(limit: Float::INFINITY)
95
97
  return nil if infinite? || zero?
96
98
 
97
99
  ln = tree.node(FractionTree.left_node)
@@ -104,7 +106,7 @@ class FractionTree
104
106
  n = denominator
105
107
  [].tap do |p|
106
108
  p << ln << rn << mn
107
- while m != n
109
+ while m != n && p.length < limit
108
110
  if m < n
109
111
  result = result * LEFT_MATRIX
110
112
  n = n - m
@@ -181,10 +183,11 @@ class FractionTree
181
183
 
182
184
  # @return [String] encoding of self
183
185
  # @example
184
- # FractionTree.node(5/4r).encoding => "RLLL"
186
+ # FractionTree.node(Math.log2(5/4r)).encoding(limit: 30) => "LLLRRRRRRRRRLLRRLLLLRRRRRRLLRL"
187
+ # @param limit of codes to generate
185
188
  #
186
- def encoding
187
- @encoding ||= self.class.encode(number)
189
+ def encoding(limit: Float::INFINITY)
190
+ self.class.encode(number, limit:)
188
191
  end
189
192
 
190
193
  # @return [FractionTree::Node] sum of self and another node
@@ -235,7 +238,7 @@ class FractionTree
235
238
 
236
239
  private
237
240
  def tree
238
- FractionTree #self.class.tree
241
+ FractionTree
239
242
  end
240
243
 
241
244
  def fraction_pair(number)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fraction-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Hales-Garcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-31 00:00:00.000000000 Z
11
+ date: 2024-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: matrix
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  - !ruby/object:Gem::Version
139
139
  version: '3.1'
140
140
  requirements: []
141
- rubygems_version: 3.5.18
141
+ rubygems_version: 3.5.20
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: Fraction tree