Pythonic Code In Several Lines

1. Fibonacci Series

#1
def Fib(n):
    if n == 1 or n == 2:
        return 1;
    else:
        return Fib(n - 1) + Fib(n - 2)
#2
def Fib(n):
    return 1 and n <= 2 or Fib(n - 1) + Fib(n - 2)
#3
Fib = lambda n: 1 if n <= 2 else Fib(n - 1) + Fib(n - 2)

#4
def Fib(n):
    x, y = 0, 1
    while(n):
        x, y, n = y, x + y, n - 1
    return x
#5
Fib = lambda n, x = 0, y = 1 : x if not n else Fib(n - 1, y, x + y)
#6
def Fib(n):
    def Fib_iter(n, x, y):
        if n == 0:
            return x
        else:
            return Fib_iter(n - 1, y, x + y)
    return Fib_iter(n, 0, 1)
#7
def fib(n):
    def m1(a,b):
        m=[[],[]]
        m[0].append(a[0][0]*b[0][0]+a[0][1]*b[1][0])
        m[0].append(a[0][0]*b[0][1]+a[0][1]*b[1][1])
        m[1].append(a[1][0]*b[0][0]+a[1][1]*b[1][0])
        m[1].append(a[1][0]*b[1][0]+a[1][1]*b[1][1])
        return m
    def m2(a,b):
        m=[]
        m.append(a[0][0]*b[0][0]+a[0][1]*b[1][0])
        m.append(a[1][0]*b[0][0]+a[1][1]*b[1][0])
        return m
    return m2(reduce(m1,[[[0,1],[1,1]] for i in range(n)]),[[0],[1]])[0]
时间: 2024-07-30 16:58:43

Pythonic Code In Several Lines的相关文章

IntelliJ IDEA “duplicated code fragment(6 lines long)”提示如何关闭

Settings —> Editor —> Inspections —> General —> Duplicated Code fragment  把对应的勾去掉 原文地址:https://www.cnblogs.com/zhaobao1830/p/12217368.html

python linecache source code

The source code of linecache.py from python 2.0 is here. from stat import * Get file information >>> filestat = os.stat('README.TXT') >>> filestat nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size

20 Best Code Review Tools for Developers

20 Best Code Review Tools for Developers Apr 02, 2015by vikas in TOOLS A programmer always faces the pressure of deadlines and with many delays the software which is designed is quite unstable and the whole product is unstable. The cause of this inst

Oracle Applications Multiple Organizations Access Control for Custom Code

文档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code Checked for relevance on 12-JAN-2011 See Change Record This document discusses how to update the customization code that is affected by the access co

Python深入学习笔记(一)

写在前面的话 从08年接触Python到现在,断断续续得使用,到如今Python已经成为日常事物处理.科研实验,甚至工程项目的主力语言,主要因为其敏捷性和快速实现的能力.虽然看了一些Python的教程,除了当初那本<Python核心编程>被反复翻看之外,其余还没看到很能让自己Python水平提高的书,虽然也掌握了一些比较Pythonic的方法,但总觉得离真正的深入理解和运用还很远,就像一直属于业余选手,算不上专业队伍那般.直到最近在看<编写高质量代码——改善Python程序的91个建议&

String decryption with de4dot

Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealing with known and supported protections is easy – drag&drop executable on de4dot and it will create deobfuscated assembly. Removing unknown protections

Differences Between Xcode Project Templates for iOS Apps

Differences Between Xcode Project Templates for iOS Apps When you create a new iOS app project in Xcode, you get to choose between several project templates, from the aptly named “Empty Application” to specialized things like an “OpenGL Game”. I noti

Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference

(Sorry for that click-bait heading. Couldn't resist ;-) ) We're on a mission. To teach you SQL. But mostly, we want to teach you how to appreciate SQL. You'll love it! Getting SQL right or wrong shouldn't be about that You're-Doing-It-Wrong? attitude

F#之旅3 - F# PK C#:简单的求和

原文链接:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-sum-of-squares.html Comparing F# with C#: A simple sumF# PK C#:简单的求和 To see what some real F# code looks like, let's start with a simple problem: "sum the squares from 1 to N