第四周作业
1.青蛙跳台阶
def f(n): t1=1 t2=2 s=0 for i in range(3,n+1): s=t1+t2 t1=t2 t2=s return s def jump(n): if n<=2: return n else: return f(n) n = eval(input()) s = jump(n) print(s)
2、汉诺塔 程序设计题
def hanoi(n,x,y,z): if n==1: print(x,'-->',z) else: hanoi(n-1,x,z,y) hanoi(1,x,y,z) hanoi(n-1,y,x,z) n=int(input()) hanoi(n,'A','B','C')
3、计算三维空间某点距离原点的欧式距离
import math def distance(): x,y,z=input().split(",") d = math.sqrt(float(x)**2+float(y)**2+float(z)**2) return d d=distance() print("{:.2f}".format(d))
4、验证码校验
a = input() b = ['qs2x','qS2x','qs2X','qS2X','Qs2x','QS2x','QS2X','Qs2X'] #创建所有可能值的列表 if a in b: #if方法使用in判断a是否在b中 print("验证码正确") else: print("验证码错误,请重新输入")
5、大小写转换
s = input()
print(s.swapcase())
6、查找指定字符
a = input() b = input() c = b.find(a) # b.find(a)方法,从b字符串s搜索a,并返回出现出偏移量,未找到则为-1 if c != -1: # 比较符号 不等于!= print("index = {}".format(c)) else: print('Not Found')
7、凯撒加密
plaincode = input() num = int(input()) ''' 原文字符P,密文字符C,偏移量num满足下列条件 C = (P + num) mod 26 P = (C - num) mod 26 ord函数:返回用来表示内存中相应字符的实际二进制值(底层整数码-ASCII字节值) chr函数:获取整数码并将其转换为对应的字符 ''' for p in plaincode: # 使用上面的方法计算 if ord("a") <= ord(p) <= ord("z"): print(chr(ord('a')+(ord(p)-ord('a')+num)%26),end='') elif ord("A") <= ord(p) <= ord("Z"): print(chr(ord('A')+(ord(p)-ord('A')+num)%26),end='') else: print(p,end="")
8、敏感词过滤
list1 = ['垃圾','陷阱','不要脸','内幕','辣鸡'] #创建关键词列表 a = input() for i in list1: #使用in判断字符串是否有词在列表中 遍历循环 if i in a: #if判断结果 a = a.replace(i,'*') #str.replace(x,y)方法,x为被替换的值,y为替换值 print(a)
9、字符串替换
str1 = input() str2 = input() str3 = input() print(str3.replace(str1,str2)) #str.replace(x,y)方法,x为被替换的值,y为替换值
10、身份证号处理
import datetime #导入datetime库,或使用 from datatime import * yearnow = datetime.datetime.now().year #可用于获得当前年份 id1 = input() #输入身份证号 year = id1[6:10] #切片获得年份 mouth = id1[10:12] #切片获得月份 day = id1[12:14] #切片获得天数 gender = id1[16] #切片获得性别数值 if eval(gender)%2 == 0: #整除2求余数,若为0则是偶数-女,反之则为奇数-男 gender="女" else: gender="男" print("你出生于{}年{}月{}日".format(year,mouth,day)) print("你今年{}周岁".format(int(yearnow)-int(year))) #当前年份与出生年份相减的年龄 print("你的性别为{}".format(gender))
树懒爱睡,日常敲代码
努力营业

浙公网安备 33010602011771号