Nice to meet you. This is leo1109, an engineer. Since this is my first post, I will write about Python, which I have been touching recently.
All the code used in the article has been uploaded on GitHub.
Code format check. Click here for PyPI.
Today, I want to write a program in Python to get the Fibonacci number! So I will write it. The Fibonacci number is a sequence in which the terms in the sequence are the sum of the terms one before and two before.
Expressed in a mathematical formula, it looks like this:
n > 0, n is unsigned Int.
n = 1: F(1) = 1
n = 2: F(2) = 1
n > 2: F(n) = F(n-1) + F(n-2)
Let's write it immediately. We need to keep the previous value, so let's implement it using an array.
# python 3.5.2
def get_fibonacci_by_index(index):
if index in [1 ,2]:
return 1
fibs = [1, 1]
for i in range(3, index + 1):
c = fibs[i - 2] + fibs[i-3]
fibs.append(c)
return fibs[-1]
It's done!
I will check if it is working properly.
Python 3.5.2 (default, Dec 19 2016, 00:08:16)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_math
>>> x=[]
>>> for i in range(1, 11):
... x.append(my_math.get_fibonacci_by_index(i))
...
>>> x
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
It seems to be working properly.
--Reference: https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A3%E3%83%9C%E3%83%8A%E3%83%83%E3%83% 81% E6% 95% B0
However, there are some subtleties to this code. For example, the following. Well, I'm curious.
def get_fibonacci_by_index(index):
if index in [1,2]:
return 1
fibs = [1, 1] #Two spaces after equal?
...
c = fibs[i - 2] + fibs[i-3] # i-2 and i-3 is not unified
Python has a coding convention called PEP8.
Of course, it is good to write according to this, but it is difficult to write while checking whether this is followed every time. It is also very inefficient to have multiple people developing and writing differently for one person and messing up. That's where the code check tool flake8 comes in. I will leave the detailed explanation to other articles, but let's actually use it.
$ flake8 my_math_before.py
my_math_before.py:3:1: E302 expected 2 blank lines, found 1
my_math_before.py:4:19: E231 missing whitespace after ','
my_math_before.py:6:11: E222 multiple spaces after operator
Gununu. I was angry. I generally say the following.
It's easy to fix because it has the number of lines and the reason.
The diff looks like this.
3d2
<
7,8c6
<
< fibs = [1, 1]
---
> fibs = [1, 1]
11c9
< c = fibs[i-2] + fibs[i-3]
---
> c = fibs[i - 2] + fibs[i-3]
Here is after the correction.
# python 3.5.2
def get_fibonacci_by_index(index):
if index in [1, 2]:
return 1
fibs = [1, 1]
for i in range(3, index + 1):
c = fibs[i-2] + fibs[i-3]
fibs.append(c)
return fibs[-1]
Let's try flake8 again. If you specify a file name, you can check only the target file. If not specified, it seems to recursively look under the executed directory.
$ flake8 my_math.py
Nothing was displayed! this is convenient. By incorporating it in Jenkins build jobs etc., it seems that not only logic but also code uniformity can be guaranteed.
Frequent code checking improves visibility and reduces the chance of bugs being embedded. I've omitted a detailed introduction, but it will notify you of errors as well as formats.
# a.py
x = 2
x = z + 3
x = 5
if x == 5:
y = 5
$ flake8 a.py
a.py:5:1: E999 IndentationError: unexpected indent
a.py:5:2: E111 indentation is not a multiple of four
a.py:5:2: E113 unexpected indentation
a.py:6:3: E111 indentation is not a multiple of four
I want to write the test of the Fibonacci number function I wrote this time in Python!
Fixed an incorrect definition of the Fibonacci sequence. thank you for the request!
Recommended Posts