Pythonic Code In Several Lines
1. Fibonacci Series
#
def Fib(n):
if n == 1 or n == 2:
return 1;
else:
return Fib(n - 1) + Fib(n - 2)
#
def Fib(n):
return 1 and n <= 2 or Fib(n - 1) + Fib(n - 2)
#
Fib = lambda n: 1 if n <= 2 else Fib(n - 1) + Fib(n - 2) #
def Fib(n):
x, y = 0, 1
while(n):
x, y, n = y, x + y, n - 1
return x
#
Fib = lambda n, x = 0, y = 1 : x if not n else Fib(n - 1, y, x + y)
#
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)
#
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]
2. FizzBuzz
print(' '.join(["fizz"[x % 3 * 4:]+"buzz"[x % 5 * 4:] or str(x) for x in range(1, 101)]))
3. Primes between 1 and 100
print(' '.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))]))
print(' '.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))]))
4. Nine multiplication tables
print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))
5. Flatten List
flatten = lambda x: [y for l in x for y in flatten(l)] if isinstance(x, list) else [x]
6.
print(sum(map(int, str(2**1000))))
7. Print Fibonacci
print([x[0] for x in [(a[i][0], a.append([a[i][1], a[i][0]+a[i][1]])) for a in ([[1, 1]], ) for i in range(30)]])
8. Quick Sort
qsort = lambda arr: len(arr) > 1 and qsort(list(filter(lambda x: x <= arr[0], arr[1:]))) + arr[0:1] + qsort(list(filter(lambda x: x > arr[0], arr[1:]))) or arr
9. Eight Queens
[__import__('sys').stdout.write('\n'.join('.' * i + 'Q' + '.' * (8-i-1) for i in vec) + "\n========\n") for vec in __import__('itertools').permutations(range(8)) if 8 == len(set(vec[i]+i for i in range(8))) == len(set(vec[i]-i for i in range(8)))]
10. Generate primes in one line: (181222)
reduce((lambda r,x: r-set(range(x**2,N,x)) if (x in r) else r), range(2,N), set(range(2,N)))
Pythonic Code In Several Lines的更多相关文章
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- Elixir - Hey, two great tastes that go great together!
这是Elixir的作者 José Valim 参与的一次技术访谈,很有料,我们可以了解Elixir的一些设计初衷,目标等等. 原文在: http://rubyrogues.com/114-rr-eli ...
- F#之旅3 - F# PK C#:简单的求和
原文链接:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-sum-of-squares.html Comp ...
- windows下安装xgboost
Note that as of the most recent release the Microsoft Visual Studio instructions no longer seem to a ...
- 编写高质量代码--改善python程序的建议(六)
原文发表在我的博客主页,转载请注明出处! 建议二十八:区别对待可变对象和不可变对象 python中一切皆对象,每一个对象都有一个唯一的标识符(id()).类型(type())以及值,对象根据其值能否修 ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型
Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...
- Qt Creator调试
与调试器交互的几种方法: 1.单行运行或者单指令运行 2.中断程序运行 3.设置断点 4.检查调用栈空间的内容 5.检查并修改局部或者全局变量 6.检查并修改被调试程序的寄存器和内存内容 7.检查装载 ...
- 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 Xc ...
- Python深入学习笔记(一)
写在前面的话 从08年接触Python到现在,断断续续地使用,到如今Python已经成为日常事物处理.科研实验,甚至工程项目的主力语言,主要因为其敏捷性和快速实现的能力.虽然看了一些Python的教程 ...
随机推荐
- ubuntu下终端代理方法
起因 正常使用shadowsocks后只能在浏览器中访问google,而终端中却无法使用. 解决方法 ProxyChains是一个终端代理方案,使用比较简单. 在源里有这个软件,直接安装 sudo ...
- SQL常见面试题(学生表_课程表_成绩表_教师表)
表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师 ...
- flask 框架快速入门
flask 框架快速入门 搭建一个简易flask项目 首先使用 Pycharm创建flask项目 运行flask项目 1.使用Pycharm搭建flask项目 (如果Pycharm新建项目中未出现该图 ...
- DNS_主从搭建
一.DNS简介 1.DNS DNS是域名系统(Domain Name System)的简称,它是一个将域名和IP相互映射的分布式数据库.有了DNS服务器,我们只需要记录一个网站的域名即可访问,而再也不 ...
- 关于Pytorch中accuracy和loss的计算
这几天关于accuracy和loss的计算有一些疑惑,原来是自己还没有弄清楚. 给出实例 def train(train_loader, model, criteon, optimizer, epoc ...
- WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用)
WDS部署基础知识:使用WDS捕获与应用映像(使用WDS定制系统和应用) Win7部署基础知识(8):使用WDS捕获与应用映像 一.添加映像组 使用WDS捕获映像时,会将映像加载到WDS服务器的映像 ...
- springcloud zookeeper+gateway
搭建springcloud项目的时候以 zookeeper为注册中心 gateway为路由 启动时出现以下报错: ****************************************** ...
- SQL语句规范
SQLStructure Query Language,结构化查询语言 1.规范(1)mysql对于SQL语句不区分大小写,SQL语句关键字尽量大写 show databases;show DataB ...
- Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- tourist's modular arithmetic class
#include <bits/stdc++.h> using namespace std; template <typename T> T inverse(T a, T m) ...