zoukankan      html  css  js  c++  java
  • Python程序员的进化史

    各种程序员所写的阶乘算法代码

    # -*- coding: utf-8 -*-
    
    #新手程序员(递归)
    
    def factorial(x):
    
      if  x == 0:
    
         return 1
    
      else:
    
         return x * factorial(x - 1)
    
    print factorial(6)
    
    #有一年 Python 经验的程序员
    
    def Factorial(x):
    
      res = 1
    
      for i in xrange(2, x + 1):
    
        res *= i      
    
      return res
    
    print Factorial(6)
    
    #懒惰的 Python 程序员
    
    def fact(x):
    
      return x > 1 and x * fact(x - 1) or 1
    
    print fact(6)
    
    #更懒惰的 Python 程序员
    
    f = lambda x: x and x * f(x - 1) or 1
    
    print f(6)
    
    #专家级 Python 程序员
    
    import operator as op
    
    import functional as f
    
    fact = lambda x:  f.foldl(op.mul,  1,  xrange(2, x + 1))
    
    print fact(6)
    
    #Python 黑客
    
    import sys
    
    def fact(x,  acc = 1):
    
      if x:  return  fact(x.__sub__(1), acc.mul__(x))
    
      return acc
    
    sys.stdout.write(str(fact(6))  +  '
    ')
    
    #专家级程序员
    
    imort c_math
    
    fact = c_math.fact
    
    print fact(6)
    
    #有一年 C 经验的程序员
    
    def fact(x):
    
      result = i = 1;
    
      while (i <= x):
    
        result *= i;
    
        i += 1;
    
      return result;
    
    print(fact(6))

     

    本人是一名 Python 新手 ,在网上看到这篇文章觉得很有趣, 所以想在博客园重写一遍。

    一方面提升下自己的 Python水平 , 另一方面也可以与更多的 Python 爱好者 学习 分享。

    人生苦短  我用Python  QAQ

    这里是更为完整的原文地址:  http://python.jobbole.com/15005/

     

  • 相关阅读:
    nginx搭建https服务器
    linux重新安装grub2
    docker使用笔记
    开户vim编程之--cscope支持
    mac代码自动补全git上
    python常用函数大总结
    服务器管理模块信息收集
    Virtual Data Optimizer虚拟存储优化器实战
    虚拟化-SDDC软件定义数据中心
    富士通 DX90 S2存储分配映射盘
  • 原文地址:https://www.cnblogs.com/xautxuqiang/p/4362074.html
Copyright © 2011-2022 走看看