python中遞歸調用

遞歸一個通俗的解釋就是,在函數中調用函數自己;僞代碼以下:python

In [31]: def fun():
   ....:     fun()


# 這個遞歸沒有任何做用,只是爲了說明什麼是遞歸

遞歸(Recursion),在數學與計算機科學中,是指在函數的定義中使用函數自身的方法。app

在使用遞歸時,須要注意如下幾點:dom

  • 遞歸就是在過程或函數裏調用自身
  • 必須有一個明確的遞歸結束條件,稱爲遞歸出口。

注意: 切勿忘記遞歸出口,避免函數無限調用。python2.7

使用遞歸計算的一些方法

第一個階乘:

階乘的定義:函數

  • 1的階乘是1
  • 大於1的樹n的階乘是n乘以(n-1)
def fun(n):   #使用遞歸定義
    if n == 1:
        return n
    else:
        return n * fun(n-1)
    

fun(5)
Out[72]: 120

def fun1(n):   # 使用非遞歸
    result = n 
    for i in range(1,n):
        result *= i
    return result


fun1(5)
Out[74]: 120

第二個冪函數的實現

In [38]: def power1(x,y):  # 不使用遞歸
   ....:     result = 1
   ....:     for i in range(y):
   ....:         result *= x
   ....:     return result
   ....: 

In [39]: power1(2,5)
Out[39]: 32

In [43]: def power2(x,y): #遞歸實現
   ....:     if y == 0:
   ....:         return 1
   ....:     else:
   ....:         return x * power2(x, y-1)
   ....:     

In [44]: power2(2,5)
Out[44]: 32

二分查找法

二分查找又叫折半查找:ui

#二分查找的隊列必須是有序的
有以下一個列表
[2,5,6,7,21,54,67,76,87,98,100]
列表中有序的,用二分法查找列表中數字87的位置。
列表共有11個元素,首先折半11/2的值取6,第六個元素值爲54.
87>54
因此87在列表的後半部分中。
而後再重複以上過程,直到找到數值爲止。 

二分查找的函數代碼:(代碼爲python2.7的版本)spa

#!/usr/bin/env python
#*-* coding:utf -8 *-*
#二分法查找數值

import sys
import random

def UnsortList():         ###若是沒有指定參數,隨機生成一個序列
    list = []
    long = random.randint(0,100)
    for i in range(long):
        list.append(random.randint(0,10000))
    return list

def BinarySearch(list, mark, low=0,uplow=None):   #二分法查找
    if not uplow:
        uplow = len(list) -1
    if low == uplow:
        assert mark == list[uplow]
        return uplow
    else:
        mid = (low + uplow) // 2
        if mark > list[mid]:
            return BinarySearch(list, mark,mid+1,uplow)
        else:
            return  BinarySearch(list,mark,low,uplow=mid)    

def SuijiMark(list):            ###在列表中隨機挑選一個要查找的數據
    l = len(list)
    mark = list[random.randint(0,l) - 1]
    return mark

def main():           ####主函數
    Ulist = []
    print "1:隨機產生列表,驗證二分法"
    print "2:用戶本身輸入數值生成列表,驗證二分法"
    answer = input("請輸入對應的數字: ")
    if answer == 1:
        Ulist = UnsortList()
        mark = SuijiMark(Ulist)
        print "The list is %s" % Ulist
        print "The mark is %s" % mark
        print "The len of the list is %s " % len(Ulist)
    elif answer == 2:
        lang = input("請輸入列表長度: ")      ##根據輸入的數值,組成列表
        for i in range(lang):          
            Ulist.append(input("請輸入列表第%d個值:" % (i + 1)))
        mark = SuijiMark(Ulist)
        print "the list is %s" % Ulist
        print "the mark is %s" % mark
    else:
        print "請輸入合法的數字"
    Ulist.sort()
    index = BinarySearch(Ulist, mark)
    print "The index %s is %s" % (index, mark)


if __name__ == "__main__":
    main()

 

執行結果以下:code

[root@mgto7 ~]# python erfen.py 
1:隨機產生列表,驗證二分法
2:用戶本身輸入數值生成列表,驗證二分法
請輸入對應的數字: 1
The list is [4204, 3199, 8884, 4555, 4941, 5695, 5730, 7363, 5357, 7193, 532, 8270, 1173, 1526, 3278, 7526, 6461, 6470, 3962, 533, 5816]
The mark is 7526
The len of the list is 21 
The index 18 is 7526
[root@mgto7 ~]# python erfen.py 
1:隨機產生列表,驗證二分法
2:用戶本身輸入數值生成列表,驗證二分法
請輸入對應的數字: 2
請輸入列表長度: 5
請輸入列表第1個值:23
請輸入列表第2個值:54
請輸入列表第3個值:65
請輸入列表第4個值:87
請輸入列表第5個值:23
the list is [23, 54, 65, 87, 23]
the mark is 65
The index 3 is 65
相關文章
相關標籤/搜索