Python之路【第十六篇】:冒泡排序、斐波那契数列、用户登陆程序

1、冒泡排序

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 冒泡排序
li = [12,34,2,4,1,6,5,78,46,34,65,18]
for j in range(len(li)-2):
    for i in range(len(li)-1):
        if li[i] > li[i+1]:
            temp = li[i]
            li[i] = li[i+1]
            li[i+1] = temp
    print(li)

2、寻找斐波那契数列中特定的数

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#递归求解斐波那契数列中的某一个数
def f(depth,a1,a2):
    #print(depth)
    if depth == 10:
        return a1
    a3 = a1 + a2
    ret = f(depth + 1,a2,a3)
    return ret

ret = f(1,0,1)
print(ret)

 

posted @ 2018-03-20 09:44  RongHe  阅读(145)  评论(0编辑  收藏  举报