吉吉于

free

【Python】梦断代码(更新中)

1.三行快排
**

def qsort(L):
    if len(L)<=0: return L
    return qsort([x for x in L if x<L[0]]) + [x for x in L if x==L[0]] + qsort([x for x in L if x>L[0]])

L=[5,4,10,2,-10]
L=qsort(L)
print L

** 1.结果**

[-10, 2, 4, 5, 10]

2.Python之禅

import this
s=this.s
d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

print 'python 之禅' 
print "".join([d.get(c, c) for c in s])

2.结果

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
python 之禅
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

3.魔法切片

a='12345'
print a[::-1]

3.结果

54321

4.split 方法

line="1 231"
head, sublist = line.split(" ",1)
print head,sublist

4.结果

1 231

5.恋爱

ideal=['喜欢','恋爱','在一起']
real = ideal[:-1]
print ','.join(real)

5.结果

喜欢,恋爱

6.全局变量

x=1
def f():
    global x
    x=42

print x
f()
print x

6.结果

1
42

7.一行代码石头剪子布

import sys
reload(sys)
print (lambda CX,YF,cn=lambda x:x.decode("utf-8"):cn("用户:")+[cn("石头"),cn("剪刀"),cn("布")][YF]+"\n"+cn("程序:")+[cn("石头"),cn("剪刀"),cn("布")][CX]+"\n"+[cn("平局"),cn("用户胜"),cn("程序胜")][(CX-YF+4)%3-1])(__import__('random').randint(0,2),(lambda Y,cn=lambda x:x.decode("utf-8"):[cn("石头"),cn("剪刀"),cn("布")].index(Y[1]))(__import__('sys').setdefaultencoding('utf-8') or ("用户输入:","布")))

7.结果

用户:布
程序:石头
用户胜

8.一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少

import math
for i in range(100000): 
    x=int(math.sqrt(i+100))
    y=int(math.sqrt(i+268))
    if(x*x == i + 100) and (y*y == i + 268):
        print i

8.结果

21
261
1581

9.最大公约数

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

10.最小公倍数

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

def lcm(a, b):
    if a * b == 0:
        return 0
    else:
        return a * b / gcd(a, b)
print lcm(24, 36)
print lcm(3, 7)

10.结果

72
21

11.逢七必过

print [x for x in range(100) if '7' in str(x) or x % 7 ==0 ]

11.结果

[0, 7, 14, 17, 21, 27, 28, 35, 37, 42, 47, 49, 56, 57, 63, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 84, 87, 91, 97, 98]

12.返回斐波那契数列

def f():
    a,b=0,1
    import itertools
    for _ in itertools.count():
        yield a
        a,b=b,a+b
        
f=f()
print [f.next() for _ in xrange(100)]

12.结果

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738L, 19740274219868223167L, 31940434634990099905L, 51680708854858323072L, 83621143489848422977L, 135301852344706746049L, 218922995834555169026L]

 

代码取自http://www.pythontip.com/coding/code_snippet

转载请注明:于哲的博客 » 【Python】梦断代码(更新中)