1. 求第n项的斐波那契数列值
def F(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return F(n-1) + F(n-2)
print(F(17))
2. 青蛙跳楼梯,每一次只能跳一个台阶或者两个台阶,注意,不允许倒退, 如果第N个台阶,请问有多少种跳法
def step(n):
if n == 0:
return 0
elif n == 1:
return 1
elif n == 2:
return 2
elif n == 3:
return 3
else:
return step(n-1) + step(n-2)
print(step(4))
print(step(5))
print(step(6))
3. 小明高考结束,成绩非常理想,父母为了奖励他,为他买了一对刚刚出生的兔子, 刚刚出生的兔子经过4个月成长为成年的兔子,就可以生小兔子, 假设成年兔子每月出生一对小兔子,问,第N月,共有多少对兔子, 假设所有不会死亡。
"""
假设成年兔子每月出生一对小兔子,
问,第N月,共有多少对兔子, 假设所有不会死亡。
1 2 3 4 5 6 7 8 9 10 11 12
1 1 1 1 2 3 4 5 7 10 14 19
"""
def rabbit_num(n):
if n <= 4:
return 1
else:
return rabbit_num(n-1)+rabbit_num(n-4)
print(rabbit_num(9))
4. 找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)
def find_num(n):
if n % 5 == 0 or n % 6 == 0 :
return n
elif n % 5 == 0 and n % 6 == 0 :
return False
a=[]
for i in range (1, 10000):
if find_num(i):
a.append(i)
print(a)
5. 写一个方法,计算列表所有偶数下标元素的和(注意返回值)根据完整的路径从路径中分离文件路径、文件名及扩展名(切片)
def way_two(a):
return a[::2]
a=[2,4,6,8,10]
print(sum(way_two(a)))
6. 根据标点符号对字符串进行分行
def divid(s):
return (s.split("."))
a = "this is text. I am happy. How are you?"
print(divid(a))
7. 去掉字符串数组中每个字符串的空格
def y (x):
return [s.strip() for s in x if s.strip()]
x = [" hello world ", " hello python "]
print(y (x))
8. 随意输入你心中想到的一个书名,然后输出它的字符串长度。 (len()属性:可以得字符串的长度)
def book_name_len(name):
return len(name)
x = "the moon and sixpence"
print(book_name_len(x))
9. 两个学员输入各自最喜欢的游戏名称,判断是否一致,如果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输出你们俩喜欢不相同的游戏。
def like_game_same(x,y):
if x == y:
return "like is same"
else:
return "like isn't same"
x=input("请输入你最喜欢的游戏:")
y=input("请让另一个人输入最喜欢的游戏:")
print(like_game_same(x,y))
10. 上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?
def like_game_same(x,y):
if x == y:
return "like is same"
else:
if x.lower()==y.lower():
return "like is same"
else:
return "like isn't same"
x=input("请输入你最喜欢的游戏:")
y=input("请让另一个人输入最喜欢的游戏:")
print(like_game_same(x,y))
11. 让用户输入一个日期格式如“2008/08/08”,将 输入的日 期格式转换为“2008年-8月-8日”。
def he(y,z):
return " ".join(a+b for a, b in zip(y,z)) # zip 交叉合并
year_month_day= input("输入xxxx/x/x/:")
y = year_month_day.split("/")
z ="年","月","日"
print(he(y,z))
12. 接收用户输入的字符串,将其中的字符进行排序(升 序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”。
x = input("输入字符串:")
y = sorted(x)
print(y)
z = y[::-1]
print(z)
13. 接收用户输入的一句英文,将其中的单词以反序输 出,“hello c sharp”→“sharp c hello”。
x = input("输入字符串:")
y = x.split()
print(y)
z = y[::-1]
print(z)
14. 从请求地址中提取出用户名和域名 http://www.163.com?userName=admin&pwd=123456
15. 有个字符串数组,存储了10个书名,书名有长有短,现 在将他们统一处理,若书名长度大于10,则截取长度8的 子串并且最后添加“...”,加一个竖线后输出作者的名字。
books_author="the moon and sixpence","a","夜行实录","徐浪"
new_books_author=[]
for book in books_author:
if len(book) > 10:
book_name = book[:8] + "..."
new_books_author.append(book_name)
else:
book_name = book
new_books_author.append(book_name)
print("|".join(new_books_author))
16. 让用户输入一句话,找出所有"呵"的位置。
x = input("请输入带呵的一句话:")
position = [i for i, j in enumerate(x) if j == '呵']
print("'呵'的位置:", position)
17. 让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变 成”老牛很**”;
x = input("请输入带‘邪恶’的一句话:")
masked_input =x.replace("邪恶", "**")
print(masked_input)
18. 如何判断一个字符串是否为另一个字符串的子串 find() index() 双层循环完成
def judge_son(a,b):
for i in range (len(a)-len(b)+1):
if a.find(b,i,len(a)) != -1:
return True
return False
a ="this is text"
b ="this is"
print(judge_son(a,b))
19. 如何验证一个字符串中的每一个字符均在另一个字符串中出现过
def judge_same(a,b):
if a.lower() == b.lower():
return True
else:
return False
a ="this is text"
b ="This Is Text"
c ="this isn't text"
print(judge_same(a,b))
print(judge_same(a,c))
20. 如何随机生成无数字的全字母的字符串
import random
len = 10
letters = "abcdefghijklmnopqrstuvwxyz"
random_letters = " ".join(random.choice(letters)for _ in range (len))
print(random_letters)
21. 如何随机生成带数字和字母的字符串
import random
len = 20
letters_numbber = "abcdefghijklmnopqrstuvwxyz1234567890"
random_letters_num = " ".join(random.choice(letters_numbber)for _ in range (len))
print(random_letters_num)
22. 判断一个字符是否是回文字符串(面试题) "1234567654321" "上海自来水来自海上"
def huiwen(x):
if x == x[::-1]:
return True
return False
x = "this is"
y = "this is si siht"
z = "上海自来水来自海上"
print(huiwen(x))
print(huiwen(y))
print(huiwen(z))
作业(选做题): 某个人进入如下一个棋盘中,要求从左上角开始走, 最后从右下角出来(要求只能前进,不能后退), 问题:共有多少种走法?
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0