LoginSignup
5
3

More than 5 years have passed since last update.

PythonでオレオレLinqライブラリ書いてみた

Last updated at Posted at 2017-09-30

PythonでオレオレLinqライブラリ書いてます.
ある程度できたので共有してみます.
なにかあったら, よろしくお願いします.
Githubはこちらです.

インストール方法

$ pip install ilinq

使用例

ProjectEulerの問題2に, フィボナッチ数列の値が400万以下かつ偶数の部分の和を求めよという問題があります.
その問題を解いてみましょう.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ilinq.ilinq import Linq
from math import sqrt


def fib(n):
    return round(1/sqrt(5) * ((1 + sqrt(5))/2)**n)


fibs = Linq(range(1, 100 + 1))  \
    .select(fib)    \
    .where(lambda n: n % 2 == 0)

print(fibs.last() >= 4 * 10**6)
# => True

print(
    fibs
    .take_while(lambda n: n < 4 * 10**6)
    .sum()
)
# => 4613732
5
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3