本文執行個體講述了python基於右遞迴解決八皇后問題的方法。分享給大家供大家參考。具體分析如下:凡是線性回溯都可以歸結為右遞迴的形式,也即是二叉樹,因此對於只要求一個解的問題,採用右遞迴實現的程式要比回溯法要優美的多。def Test(queen,n): '''這個就不用說了吧,就是檢驗第n(下標,0-7)行皇后的位置是否合理''' q=queen[n] for i in xrange(n): if queen[i]==q or queen[i]-q==n-i or queen[i]-q==
本文執行個體講述了python顯示生日是星期幾的方法。分享給大家供大家參考。具體實現方法如下:# find the day of the week of a given date# Python will trap impossible dates like (1900, 2, 29)# tested with Python24 vegaseat 01aug2005from datetime import date# a typical birthday year, month, day
本文執行個體講述了python統計文本字串裡單詞出現頻率的方法。分享給大家供大家參考。具體實現方法如下:# word frequency in a text# tested with Python24 vegaseat 25aug2005# Chinese wisdom ...str1 = """Man who run in front of car, get tired.Man who run behind car, get exhausted."""print "Original
本文執行個體講述了python定時執行指定函數的方法。分享給大家供大家參考。具體實現方法如下:# time a function using time.time() and the a @ function decorator# tested with Python24 vegaseat 21aug2005import timedef print_timing(func): def wrapper(*arg): t1 = time.time() res = func(*arg)
本文執行個體講述了python處理大數位方法。分享給大家供大家參考。具體實現方法如下:def getFactorial(n): """returns the factorial of n""" if n == 0: return 1 else: k = n * getFactorial(n-1) return kfor k in range(1, 70): print "factorial of", k,"=",
本文執行個體講述了python解析xml檔案的方法。分享給大家供大家參考。具體如下:python解析xml非常方便。在dive into python中也有講解。如果xml的結構如下: zoer think in java this is a good book naughty gone with the wind this is a good book 2 cc this is a good
本文執行個體講述了Python簡單刪除目錄下檔案以及檔案夾的方法。分享給大家供大家參考。具體如下:#!/usr/bin/env pythonimport osimport shutilfilelist=[]rootdir="/home/zoer/aaa"filelist=os.listdir(rootdir)for f in filelist: filepath = os.path.join( rootdir, f ) if os.path.isfile(filepath):