[RUBY] 10th

!Ubuntu-20.04.1!ruby-2.7.1p83

This is a summary site for lectures. If you see it, please push LGTM.

What i did

--Fibonacci sequence

Fibonacci sequence

theme

Create a program to find the Fibonacci sequence.
For example, if you enter "0", I want the first term 0 of the sequence to be displayed.

policy

simply

def fib(n)
  if n==0
    return 0
  end
  if n==1
    return 1
  end
end

Or make it a little cleaner

[[0,0],[1,1]].each do |pair|
  puts assert_equal(pair[0], fib(pair[1]))
end

There is no end to writing the conditions one by one.
Therefore, I think about making it possible to calculate whatever I enter.

How to actually do it

In order to be able to calculate without permission, only the first term (0th) and the next term must be taught here.
After that, according to the definition of the Fibonacci sequence, the nth term. Since the term of is (n-1) th + (n-2) th, let's program it.

def fib(n)
  if n==0
    return 0
  else if n==1
    return 1
  else
    return fib(n-1) + fib(n-2)
  end
end
p fib(6)

It becomes a feeling.

refactoring

However, the code is long if it is left as it is, so let's put it together more tightly.

def fib(n)
  return 0 if n==0
  return 1 if n==1
  return fib(n-1) + fib(n-2)
end
p fib(6)

It became.
With this alone, when n = 6, the results of fib (5) and fib (4) are added, so fib (5) and fib (4) are not defined, so they should not work.

> 8

... that! ?? It works! ?? why? Even if I tried fib (30), the value was returned properly
, but I don't remember writing anything that recursively processes it, so I'm not sure about this. Tell someone
(12/17 postscript)
I noticed it in the comment from the teacher. If you think about it carefully, if you pass 8 to fib, you will go to the 4th line and fib (7) and fib (6) will be called, and if you pass 7,6 to fib, you will go to the 4th line of fib ... and recursively. I was called by

If I sit in front of the PC for a long time, my thoughts get stuck and I don't even notice the simple things. Let's go for a walk moderately

Increase the number of tests

Use each do if you want to test multiple times.

def fib(n)
  return 0 if n==0
  return 1 if n==1
  return fib(n-1) + fib(n-2)
end

[0,1,2,6].each do |pair|
  puts fib(pair)
end

so

> 0
> 1
> 1
> 8

It will come back at once

Use assert \ _equal to check if the calculation is really correct

Use the assert \ _equal function I made last week to check if the 9th is really calculated properly.

def fib(n)
  return 0 if n==0
  return 1 if n==1
  return fib(n-1) + fib(n-2)
end

require './assert_equal.rb'
[[0,0],[1,1],[2,1],[3,2],[4,3],
 [5,5],[6,8],[7,13],[8,21],[9,34],
].each do |index, expected|
  puts assert_equal(expected,fib(index))
end

An array made up of a combination of index and expected is passed to the assert \ _equal function for judgment.

["expected", 0]
["result", 0]
succeeded in assert_equal

["expected", 1]
["result", 1]
succeeded in assert_equal

["expected", 1]
["result", 1]
succeeded in assert_equal

["expected", 2]
["result", 2]
succeeded in assert_equal

["expected", 3]
["result", 3]
succeeded in assert_equal

["expected", 5]
["result", 5]
succeeded in assert_equal

["expected", 8]
["result", 8]
succeeded in assert_equal

["expected", 13]
["result", 13]
succeeded in assert_equal

["expected", 21]
["result", 21]
succeeded in assert_equal

["expected", 34]
["result", 34]
succeeded in assert_equal

All clear

Reference article

Chart type ruby-V (Recursive Fibonacci)


Recommended Posts

5th
6th
6th
9th
7th
11th
9th
7th
11th
10th
10th
11th class: Class
9th class: assert_equal
10th class: Recursion
8th class: rake
13th (thor, rubocop)
5th class: Output
TDD study # 2 (July 13th, 2020)
7th class: if, else
TDD study # 5 (July 18th, 2020)
TDD study # 4 (July 16th, 2020)
11th :: ruby_VI (hello class)
6th class: Variables, method
11th, Classification with Ruby
TDD study # 3 (July 15th, 2020)