Class: Fib
Class Method Summary collapse
Instance Method Summary collapse
- #fib(x) ⇒ Object
- #fib_block(&blk) ⇒ Object
- #find(x) ⇒ Object
- #merge(left, right) ⇒ Object
- #mergesort(list) ⇒ Object
- #no_args ⇒ Object
Class Method Details
.fib(x) ⇒ Object
219 220 221 222 |
# File 'lib/tell_me_about_it.rb', line 219 def self.fib x return 1 if x <= 2 fib(x - 1) + fib(x - 2) end |
.find(x) ⇒ Object
225 226 227 228 |
# File 'lib/tell_me_about_it.rb', line 225 def self.find x return 1 if x <= 2 find(x - 1) + find(x - 2) end |
Instance Method Details
#fib(x) ⇒ Object
213 214 215 216 |
# File 'lib/tell_me_about_it.rb', line 213 def fib x return 1 if x <= 2 fib(x - 1) + fib(x - 2) end |
#fib_block(&blk) ⇒ Object
235 236 237 |
# File 'lib/tell_me_about_it.rb', line 235 def fib_block &blk fib yield end |
#find(x) ⇒ Object
230 231 232 |
# File 'lib/tell_me_about_it.rb', line 230 def find x x * 2 end |
#merge(left, right) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/tell_me_about_it.rb', line 249 def merge(left, right) sorted = [] until left.empty? or right.empty? if left.first <= right.first sorted << left.shift else sorted << right.shift end end sorted.concat(left).concat(right) end |
#mergesort(list) ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/tell_me_about_it.rb', line 240 def mergesort(list) return list if list.size <= 1 mid = list.size / 2 left = list[0, mid] right = list[mid, list.size] merge(mergesort(left), mergesort(right)) end |
#no_args ⇒ Object
208 209 210 |
# File 'lib/tell_me_about_it.rb', line 208 def no_args 5 end |