python笔记之编程风格大比拼
python笔记之编程风格大比拼
虽然我的python age并不高,但我仍然愿意将我遇到的或者我写的有趣的python程序和大家一块分享,下面是我找到的一篇关于各类python程序员的编程风格的比较文章,以阶乘为例,很有意思。
新手程序员
def factorial(x):if x == 0:return 1else:return x * factorial(x - 1)
print factorial(6)
第一年的刚学完Pascal的新手
def factorial(x):result = 1i = 2while i <= x:result = result * ii = i + 1return result
print factorial(6)
第一年的刚学完C语言的新手
def fact(x): #{result = i = 1;while (i <= x): #{result *= i;i += 1;#}return result;
#}
print(fact(6))
第一年刚学完SICP的新手
@tailcall
def fact(x, acc=1):if (x > 1): return (fact((x - 1), (acc * x)))else: return acc
print(fact(
第一年刚学完Python的新手
def Factorial(x):res = 1for i in xrange(2, x + 1):res *= ireturn res
print Factorial(6)
爱偷懒的程序员
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
@tailcall
def fact(x, acc=1):if x: return fact(x.__sub__(1), acc.__mul__(x))return acc
sys.stdout.write(str(fact(6)) + '\n')
专家级程序员
import c_math
fact = c_math.fact
print fact(6)
英语系的专家级程序员
import c_maths
fact = c_maths.fact
print fact(6)
Web 设计者
def factorial(x):#-------------------------------------------------#--- Code snippet from The Math Vault ---#--- Calculate factorial (C) Arthur Smith 1999 ---#-------------------------------------------------result = str(1)i = 1 #Thanks Adamwhile i <= x:#result = result * i #It's faster to use *=#result = str(result * result + i)#result = int(result *= i) #??????result str(int(result) * i)#result = int(str(result) * i)i = i + 1return result
print factorial(6)
Unix 程序员
import os
def fact(x):os.system('factorial ' + str(x))
fact(6)
Windows 程序员
NULL = None
def CalculateAndPrintFactorialEx(dwNumber,hOutputDevice,lpLparam,lpWparam,lpsscSecurity,*dwReserved):if lpsscSecurity != NULL:return NULL #Not implementeddwResult = dwCounter = 1while dwCounter <= dwNumber:dwResult *= dwCounterdwCounter += 1hOutputDevice.write(str(dwResult))hOutputDevice.write('\n')return 1
import sys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
公司里的程序员
def new(cls, *args, **kwargs):return cls(*args, **kwargs)class Number(object):passclass IntegralNumber(int, Number):def toInt(self):return new (int, self)class InternalBase(object):def __init__(self, base):self.base = base.toInt()def getBase(self):return new (IntegralNumber, self.base)class MathematicsSystem(object):def __init__(self, ibase):Abstract@classmethoddef getInstance(cls, ibase):try:cls.__instanceexcept AttributeError:cls.__instance = new (cls, ibase)return cls.__instanceclass StandardMathematicsSystem(MathematicsSystem):def __init__(self, ibase):if ibase.getBase() != new (IntegralNumber, 2):raise NotImplementedErrorself.base = ibase.getBase()def calculateFactorial(self, target):result = new (IntegralNumber, 1)i = new (IntegralNumber, 2)while i <= target:result = result * ii = i + new (IntegralNumber, 1)return resultprint StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))
转载于:https://my.oschina.net/kooksee/blog/542430
python笔记之编程风格大比拼相关推荐
- RT-Thread笔记 之 编程风格
前言 本文整理并介绍了RT-Thread系统使用的编码规范,原文链接及更新请移步参考资料部分. 目录 RT-Thread 编程风格 1.目录名称 2.文件名称 3.头文件定义 4.文件头注释 5.结构 ...
- Python笔记002-Python编程基础概念
第二章(1):Python编程基础概念 1. Python 程序的构成 Python 程序有模块组成.一个模块对应 Python 源文件,一般后缀名是:.py. 模块有语句组成.运行 Python程序 ...
- python的编程方式模块化_我的Python笔记·模块化编程(一)
如何自定义和调用函数 定义一个函数 def my_function(arg1, arg2, ...): '''documentation''' return value1, value2, ... m ...
- 《EMCAScript6入门》读书笔记——24.编程风格
转载于:https://www.cnblogs.com/zhengxj1991/p/9068808.html
- python笔记03_IO编程
with open('/path/to/file', 'r') as f: print(f.read()) f.write('Hello, world!') f.close() StringIO() ...
- python笔记2(函数 面向对象 文件编程 上下文管理器)
记录python听课笔记 文章目录 记录python听课笔记 一,函数 1.介绍python里的函数 2.用户自定义函数 3.变量的作用域 4.参数的传递 5.参数的默认值 6.向函数内部批量传递数据 ...
- Python学习笔记:网络编程
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- Python基础概念_12_编程风格
编程风格 13 编程风格 13.1 简介 为了编程的规范,我们一般约定一些编程规则.约定,这些都称为编程风格. 13.2 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 13.3 行长度 ...
- javascript编程风格(粗略笔记)
1.空格 紧凑型: project.MyClass = function(arg1, arg2){ 松散型: for( i = 0; i < length; i++ ){ 2.代码行长度 最多8 ...
最新文章
- Mysql事务结合spring管理
- android 窗口监听按键,Android编程实现Dialog窗体监听的方法
- Java的Map接口,以及Collection和Collections的区别
- “==”和equals()那些事
- Vue—核心概念—异步组件和路由懒加载
- spring-AOP前言
- 查看当前提供了哪些引擎
- 在虚拟机中安装和配配置 MOSS2007 全过程
- 【SP26073】DIVCNT1 - Counting Divisors 题解
- PostgreSQL复制特性历史漫谈-士别三日,当刮目相看
- 网络七大趋势 媒介将很快彻底消失
- oracle下载配置文件,oracle 11G、12C BBED 配置和库文件下载!
- hihoCoder 1014trie树(字典树)
- 趣谈网络协议学习笔记——TCP
- 基于51单片机控制步进电机正反转
- Zigbee 协议栈
- id2021中文直装版 v16.0附安装教程及软件特点
- 日本CKD电磁阀、CKD、电磁阀、CKD电磁阀
- Java程序员必读精选书籍分享,强烈推荐
- 算法笔记习题 7-1小节