Python 小程序练习

1. 实现 f(x) = 2 * f(x -1) + x * x:

def f(x):
    if x == 0:
        return x
    else:
        return f(x-1) * 2 + x * x

for x in xrange(1,10):
    print f(x)

2. 一个简单类的实现

class tClass():
    def __init__(self,orig=0):
        self.storedValue = orig
    def read(self):
        return self.storedValue
    def write(self,x):
        self.storedValue = x

def Test():
    a = tClass()
    print "a: " + str(a.read())
    a.write(10)
    print "a: " + str(a.read())
    
    b = tClass(100)
    print "b: " + str(b.read ())

if __name__ == '__main__':
    Test()

运行结果:

3. 地精排序:

import random
def gnome_sort(seq):
    i = 0
    while i < len(seq):
        if i == 0 or seq[i-1] <= seq[i]:
            i += 1
        else:
            seq[i], seq[i-1] = seq[i-1],seq[i]
            i -= 1
#seq = range(1,11)
#print seq
#print 
#i = 0
#while(i < 10):
#    seq[i] = random.randint(1,10)
#    i += 1
#print seq
#gnome_sort(seq)
#print 
#print seq        
#-----------------------------
#-----------------------------
i = 0
seq = []
while i < 10:
    seq.append(random.randint(1,100))
    i += 1
print "Origin: ",seq
print
gnome_sort(seq)
#seq.sort()
print "After sort:",seq

运行结果:

4. 冒泡排序:

import random
def bubblesort(a):
    for i in xrange(0,len(a)):
        for j in xrange(i,len(a)):
            if a[j] < a[i]:
                a[j],a[i] = a[i],a[j]

def Test():
    a = []
    for i in xrange(1,10):
        a.append(random.randint(1,100))
    print "orginal: ",a
    #print "after sort: "
    bubblesort(a)
    print "after sort:",a

if __name__ == '__main__':
    Test()

运行结果:

5. 直接插入排序

# -*- coding: utf-8 -*-
import random,time
r = random.Random()

li = [r.randint(1,300) for i in xrange(20)]  #生成随机数

print li
print "-"*30

starttime = time.time()

for i in range(len(li)):
    temp = li[i]
    j = i
    while j > 0 and temp < li[j-1]:
        li[j] = li[j-1]
        j -= 1
    li[j] = temp 

print li

endtime = time.time()

print endtime - starttime

运行结果:

 

6.快速排序

import random
def sort(L):
    if not L: return []
    else:
        return sort([x for x in L[1:] if x < L[0]]) + L[0:1] + sort([x for x in L[1:] if x >= L[0]])

L = [ random.randint(1,100) for i in xrange(10)]
print L
print 
LL = sort(L)
print "After sort: "
print LL

运行结果:

7. 最大子序列和

def maxSum(a):
    currSum = 0
    mSum = 0

    for i in a:
        currSum += i
        if currSum > mSum :
            mSum = currSum
        elif currSum < 0:
            currSum = 0
    return mSum
def maxSum_1(a):   #第二种实现方式
    currSum = 0
    mSum = 0

    for i in xrange(0,len(a)):
        for j in xrange(i,len(a)):
            currSum = 0
            for k in a[i:j]:
                currSum += k
            if currSum > mSum:
                mSum = currSum
    return mSum

def Test():
    t = [-2, 11, -4, 13, -5, -2]
    print "maxSum: ", maxSum(t)
    #print
    print "maxSum_1: ", maxSum_1(t)


if __name__ == '__main__':
    Test()

运行结果:

posted @ 2012-07-07 22:01  KingsLanding  阅读(27184)  评论(0编辑  收藏  举报