import sys def fib(n): if n == 0: return 0 prev, cur = 0, 1 for i in range(n - 1): prev, cur = cur, prev + cur return cur if __name__ == '__main__': for line in open(sys.argv[1]): if len(line) > 0: print fib(int(line) + 1)
Saturday, January 4, 2014
Fibonacci for CodeEval
Just to have some fun, I was solving a not too complex problem on CodeEval. The description was sort of dodgy, but in the end I understood that it boiled down to calculate the Fibonacci number of the values passed as input (increased by one, to make the waters a bit muddier).
I usually play those games in C++, that is my preferred programming language. Alas, on CodeEval we are forced to use a 32 bit C++98 compiler. Not exactly what you could call a state of the art tool. This is particularly annoying in this case, where I need to calculate huge numbers as the Fibonacci ones are.
Luckily my basic knowledge of Python is enough to get me out of it. Here is my 2.7 solution, I know it is nothing special, but it sufficed to get me a shiny 100%:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment