第四周作业-python123

1.跳台阶

def fun(a):
    total=0
    firstElem=1
    secondElem=2

    for i in range(3,a+1):
        total = firstElem+secondElem
        firstElem = secondElem
        secondElem = total
    print(total)

n = eval(input())
fun(n)

 2.汉诺塔

n=int(input())
def move(n,A,B,C):
    if(n%2!=0):
        if(n==1):
            print(A,"->",C)
            return
        move(n-1,A,C,B)
        move(1,A,B,C)
        move(n-1,B,A,C)
    else:
        if(n==1):
            print(A,"->",B)
            return
        move(n-1,A,B,C)
        move(1,A,C,B)
        move(n-1,C,A,B)
move(3,"A","B","C")

 3.计算三维空间某点距离原点的欧式距离

import math
x,y,z=input().split(",")
def distance (x,y,z):
    dist=pow(x,2)+pow(y,2)+pow(z,2)
    return(math.sqrt(dist))
d=distance(float(x),float(y),float(z))
print("{:.2f}".format(d))

 4.验证码校验

s = "qS2x"
N = input()
if s.lower() == N.lower():
    print("验证码正确")
else:
    print("验证码错误,请重新输入")

 5.大小写转换

import string
n=input()
for i in n:
    if(i in string.ascii_lowercase):
        i=i.upper()
        print(i,end='')
    elif(i in string.ascii_uppercase):
        i=i.lower()
        print(i,end='')
    else:
        print(i,end='')

 6.查找指定字符

a=input()
s=input()
s1=list(s)
if a in s1:
    print("index =",s.rindex(a))
else:
    print("Not Found")

 7.凯撒加密

code = input()
n=int(input())
for p in code: 
     if ord("a") <= ord(p) <= ord("z"):
         print(chr(ord("a")+(ord(p)-ord("a")+n)%26), end='')
     elif ord("A") <= ord(p) <= ord("Z"):
        print(chr(ord("A")+(ord(p)-ord("A")+n)%26), end='')
     else:
         print(p, end='')

 8.敏感词过滤

n=input()
print(n.replace('垃圾','*').replace('陷阱','*').replace('不要脸','*').replace('内幕','*').replace('辣鸡','*'))

 9.字符串替换

n1=input()
n2=input()
n3=input()
print(n3.replace(n1,n2))

 10.身份证号处理

import datetime
n=input()
a=datetime.datetime.now().year
year=n[6:10]
month=n[10:12]
day=n[12:14]
x=int(n[16])
old=a-int(year)-1
print("你出生于"+year+"年"+month+"月"+day+"日")
print("你今年"+str(old)+"周岁")
if x%2==0:
    print("你的性别为女")
else:
    print("你的性别为男")

 11.斐波那契数列II

def fbi(n):
    if n==1:
        x=1
    else:
        a=0
        b=1
        for i in range(1,n):
            x=a+b
            a=b
            b=x
    return(x)
n = eval(input())
print(fbi(n))

 12.英文单词个数统计

s = '''
"Collusion is very real with Russia," Trump quoted conservative commentator Dan Bongino as saying on Trump's favorite Fox News morning show, "but only with Hillary and the Democrats, and we should demand a full investigation."
'''
word = s.split()
num = len(word)
print(num)

 13.任意积累

def cmul(a, *b):
    m = a
    for i in b:
        m *= i
    return m

print(eval("cmul({})".format(input())))

 14.随机密码生成

import random

def genpwd(length):
    a = 10**(length-1)
    b = 10**length - 1
    return "{}".format(random.randint(a, b))

length = eval(input())
random.seed(17)
for i in range(3):
    print(genpwd(length))

 15.时间输出格式化

import time
timestr = "2020-10-10 10:10:10"
t = time.strptime(timestr, "%Y-%m-%d %H:%M:%S")
print(time.strftime("%Y年%m月%d日%H时%M分%S秒", t))

 16.时间差之天数计算

import time
import datetime
day1,day2 = input().split(',')
t1 = time.strptime(day1, "%Y年%m月%d日%H点%M分%S秒")
t2 = time.strptime(day2, "%Y年%m月%d日%H点%M分%S秒")
d1 = datetime.datetime(t1[0],t1[1],t1[2],t1[3],t1[4],t1[5])
d2 = datetime.datetime(t2[0],t2[1],t2[2],t2[3],t2[4],t2[5])
early = min(d1,d2)
later = max(d1,d2)
days = list(str(later - early).split(' '))
print(days[0])

 

posted @ 2021-12-15 00:19  林木森3  阅读(410)  评论(0)    收藏  举报