python 函式例子 Python3函式之例子

2021-10-12 11:57:22 字數 3735 閱讀 6815

1.分別使用遞迴、迴圈和生成器求菲波那切數列

遞迴:def fib_rec(n):

if n == 0:

return 1

if n == 1:

return 1

return fib_rec(n-1) + fib_rec(n-2)

fib_rec(5)

迴圈:def fib_loop(n):

lst =

for i in range(n+1):

if i == 0 or i == 1:

else:

return lst.pop()

fib_loop(5)

def fib_loop2(n):

a, b = 1, 1

for i in range(n+1):

if i == 0 or i == 1:

a, b = 1, 1

else:

a, b = b, a+b

return b

fib_loop2(5)

生成器:

def fib_gen():

i = 0

a, b = 1, 1

while true:

if i == 0 or i == 1:

yield 1

else:

a, b = b, a+b

yield b

i += 1

def fib(n):

gen = fib_gen()

for _ in range(n):

next(gen)

return next(gen)

fib(5)

2.寫乙個函式,實現對整數的排序,預設公升序排序,不能使用任何內建函式和第三方庫

fun1:

def sort_int(lst, asc=true):

sort_lst =

while lst:

i = none

min_int = none

for i in lst:

if min_int is none:

min_int = i

else:

if min_int > i:

min_int = i

lst.remove(min_int)

if asc:

else:

sort_lst.insert(0, min_int)

return sort_lst

fun2:

def sort(lst, reverse=false):

dst =

for n in lst:

for i, e in enumerate(dst):

if not reverse:

if n < e:

dst.insert(i, n)

break

else:

if n > e:

dst.insert(i, n)

break

else:

return dst

3.寫乙個函式,把整數轉化為羅馬數字,輸入為1到3999之間的任意數字

def int2rom(n):

if n < 1 or n > 3999:

print('input error.')

return 0

rom_lst = [' ',

'i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix',

'x', 'xx', '***', 'xl', 'l', 'lx', 'lxx', 'l***', 'xc',

'c', 'cc', 'ccc', 'cd', 'd', 'dc', 'dcc', 'dccc', 'cm',

'm', 'mm', 'mmm']

rom = ''

bit = n % 10

ten = (n % 100) // 10

hun = (n % 1000) // 100

tho = n // 1000

if tho != 0:

rom += rom_lst[27 + tho]

if hun != 0:

rom += rom_lst[18 + hun]

if ten > 0:

rom += rom_lst[9 + ten]

if bit > 0:

rom += rom_lst[bit]

return rom

3.2寫乙個函式,把整數轉化為羅馬數字,輸出為1到3999之間的任意數字

def roman2int(src):

convert_map = {

'i':1,

'v':5,

'x':10,

'l':50,

'c':100,

'd':500,

'm':1000

# mdcccxcix

# xicxcccdm

# +10 -1 +100 -10 +100 +100 +100 +500 +1000

# 1899

roman = src.upper()[::-1]

prev = 0

lst =

for x in roman:

i = convert_map[x]

if i < prev:

else:

prev = i

return sum(lst)

roman2int('mdcccxcix')

4.寫乙個函式,求兩個字串的最長公共子串

例如輸入:i love python 和 python is a ****** language,輸出為python

def psubstring_long(x, y):

f =

max_len = 0

idx = 0

for i, a in enumerate(x):

for j, b in enumerate(y):

if a != b:

else:

if i == 0 or j == 0:

else:

if max_len < f[i][j]:

max_len = f[i][j]

# idx = max_len - (i+1)

idx = i + 1 - max_len

return x[idx: idx + max_len]

psubstring_long('babcdf', 'xxaabcaac')

5.動態規劃

總長n,step表示一次可選步數,總共多少種走法

ret = 0

tmp =

solution =

def recursion(n, tmp, step=3):

global ret

if n == 0:

solution.extend(tmp)

ret += 1

return 0

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

if n > i-1:

recursion(n-i, tmp, step)

tmp.pop(-1)

return ret

print(recursion(3, , 3), solution)

4 ['===', 1, 1, 1, '===', 1, 2, '===', 2, 1, '===', 3]

字元函式例子

要查詢以某個字結尾等於什麼的記錄 select from vehicle 車牌號以7結尾的資料記錄 select from vehicle t where trim t.veh no like 7 用函式的方式 select from vehicle t where substr trim t.ve...

C 建構函式 例子

建構函式,去掉this class person public person string name,string int age,double weight public void eatfood double quanity class program age weight firstman.n...

C 建構函式例子

已知string類定義如下 class string public string constchar str null 通用建構函式 string conststring another 拷貝建構函式 string 析構函式 string operater const string rhs 賦值函式...