python: practice recurse function

starting with a  factorial :

def      function_factorial(n):

number=1

for i   in   range(1,n+1):

number *=i

return number

print(function_factorial( n)

use this application can acheved one number‘s factorial.

similar  recurse function also can realized one number factorial:

def  function_recurse(n):

if  n==1:

return 1

return n*function_recurse(n-1)

print(function_recurse(n))

recurse function have follow  characters:

1:in itself‘s function call itself  realize it function  performance

2:must setup a broke point ,otherwise it will enter a endless loop.

3: all recurse function can realize function loop also can do .

4:recurse productive  low efficiency,workpiece ratio inefficiency

原文地址:https://www.cnblogs.com/alansuny/p/12495824.html

时间: 2024-11-07 20:44:36

python: practice recurse function的相关文章

Python中def function(*args)用法

Python中可用def function(): 创建一个自定义函数. 下面我将用代码解释def function(*args): 的用法 --- *args输入参数如何工作的: *args ```可以接受序列的输入参数.当函数的参数不确定时,可以使用 *args. #!/usr/bin/python # -*- coding: UTF-8 -*- def biggest_number(*args): print max(args) return max(args) def smallest_n

python closure and function decorators 1

原文:http://thecodeship.com/patterns/guide-to-python-function-decorators/ 仅此做一个中文翻译: 我们知道,python在方法def的使用上,有一些非常强大的功能. 譬如,将方法传给一个变量: def sayHI(name): return "hi " + name ExexSayHI = sayHI print ExexSayHI("Allen") 或者在方法中定义方法 def SayHi(nam

Python 函數 Function

函數最初被設計出來,是用來減輕重複 coding 一段相同的代碼,這之間只有代碼 (方法,Method) 的重用,但還沒有物件導向OO整個Object 的屬性與方法被封裝重用的概念. 函數的定義很簡單,使用 def 這保留字,而其宣告方式如下: def 函數名(參數名) : 例如: >>> def print_count(): # 函數宣告...    print('Good')...    print('Morning')...    print('Mr.')...>>&g

python closure and function decorators 2

好吧,基础打好,聊聊decorator,先看这段代码: def SayHi(name): return "How are you {0}, good morning".format(name) def decoator(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrapper hi = decoator(SayHi) pr

Python 偏函数 partial function

1 import functools 2 int2 = functools.partial(int, base=2) 3 print(int2('100')) 4 5 6 max2 = functools.partial(max, 10) 7 print(max2(2,6,0,8)) functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单. 创建偏函数时,实际上可以接收函数对象.*args和**kw这3个参数. 前

python:practice decorator calaulate_time

from functools import reduce import time def  factorial_array(n) number=reduce(lambda x,y:x*y,range(1,n+1)) return number print( factorial_array(101) def calaulate_time(func): start_time=time.time() func() end_time=time.time() return end_time-start_t

General Purpose Hash Function Algorithms

General Purpose Hash Function Algorithms [email protected]: http://www.partow.net/programming/hashfunctions/index.html     Description Hashing Methodologies Hash Functions and Prime Numbers Bit Biases Various Forms Of Hashing String Hashing Cryptogra

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//

Exploring Python Code Objects

Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Inspired by David Beazley's Keynote at PyCon, I've been digging around in code objects in Python lately. I don't have a particular axe to grind, nor some