Return the first occurance of a certain string length using itertools in Python -


I am generating Fibonacci sequence numbers using generator in Python (the generator works fine)

< P> The problem is that I did not know how to get the first n -digit variable and print it.

  Print list (Iterol) .takewhile (Lambda X: lane (str (x)) <3, fib_function ()))  

will come back

  [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] In other words, in other words, it gives all the three-digit conditions, which is unnecessary because I wanted to include only 144.  By limiting it, the two will not produce the next one which I want, and I do not think it is able to move only one more walk.  
  • In addition, this code will print the entire sequence, not only the last opportunity. I have taken a look through the docs but it does not seem to offer anything. I can live with it, but if it does, then it would be good.

    (Note: This project is Euler problem 25: First 100 digit Fibonacci numbers)

    Leave everything under 3 digits, then next with next () :

      print next (itertools.dorthwhile (lambda x: x <100, fib_function ()))  

    I tested the digits 100 Instead of changing to the test; It saves you 2 function calls.

    Demo:

      & gt; & Gt; & Gt; Print Next (Iterolls. Lampda X: X & lt; 100, Phib_function ())) 144  

  • Comments

    Popular posts from this blog

    Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

    multithreading - PhantomJS-Node in a for Loop -

    c++ - MATLAB .m file to .mex file using Matlab Compiler -