Python homework for the fourth week

Jump the stairs

A frog can jump up to 1 step at a time, or jump up to 2 steps. May I ask how many kinds of jumps the frog jumps on an n-level step?

Enter the number of steps and output how many jump methods

def jumpFloor(number):
    a = 1
    b = 1
    for i in range(number):
        a,b = b,a+b 
    return a 

number = eval(input()) 
print(jumpFloor(number))

  Hanota

E = eval(input())
count = 0
def hanoi (n, src, dst, mid): # (number of discs, source column, destination column, transition column)
    global count
    if n == 1: #If there is only one disc
        print("{} --> {}".format(src,dst))
        count += 1
    else:
        hanoi (n-1, src, mid, dst) #Move to the last disk of n
        print("{} --> {}".format(src,dst))
        count += 1
        hanoi(n-1,mid,dst,src)
hanoi(E,"A","C","B")

  #0033003200380032003200301587537703035

Calculate the Euclidean distance from a point in 3D space to the origin 

Euclidean metric (also known as Euclidean distance) is a commonly used definition of distance. If the coordinates of points a and b in three-dimensional space are a (x1, y1, z1) and b (x2, y2, z2), then the computer formula for the distance of ab is dist (a, b) = √ ((x1- x2) ^ 2 + (y1-y2) ^ 2 + (z1-z2) ^ 2)

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

  003200301587537765653

Verification code verification

Users often need to enter a verification code to log in to the website. The verification code contains uppercase and lowercase letters and numbers, and appears randomly. The user does not distinguish between upper and lower case when entering the verification code, as long as the characters appear in the correct order, they can pass the verification. 
Please write a program to complete the verification of the verification code, assuming that the currently displayed verification code is 'Qs2X'.
If the user enters the verification code is correct, the output is "Verification code is correct", when the input is incorrect, the "Verification code is wrong, please re-enter"
s = input("")
e = {'Q','q'}
r = {'s','S'}
t = {'2'}
y = {'x', 'X'}
f = list(s)
if s[0] in e:
    if s[1] in r:
        if s[2] in t:
            if s[3] in y:
                print ("Verification code is correct")
else:
    print ("Verification code error, please re-enter")

  Case conversion

s = input("")

print(s.swapcase())

  Find the specified character

If it is found, output the largest subscript corresponding to the character in the string (the subscript starts from 0) according to the format "index = subscript" in a line;
otherwise, it outputs "Not Found".

a=input()
s=input()
s1=list(s)
if len(a)==1 and 0<len(s)<=80:
    if a in s1:
        print ("index =" = "s.rindex (a)) #rindex () returns the last position of the substring in the string
    else:
        print("Not Found")

  Caesar encryption

s = input()
num = eval(input())
t = ""
for c in s:
    if 'a' <= c <= 'z': 
        t + = chr (word ('a') + ((word (c) -word ('a')) + num)% 26)
    elif 'A'<=c<='Z':
        t + = chr (word ('A') + ((word (c) -word ('A')) + num)% 26)
    else:
        t += c
print(t)

  Sensitive word filtering

import re
s = input("")
s = s.replace ('garbage', '*'). replace ('spicy chicken', '*'). replace ('shameless', '*'). replace ('trap', '*'). replace ('Inside Story', '*')
print(s)

  String replacement

When editing a document, you can often replace a string with a special short string for a string that appears frequently and is difficult to enter, and then replace it when the document is complete. 
For example: when inputting "Wuhan University of Technology", you can use "whut" instead, and program to complete this replacement.

Input There are three lines:

The first line is a short character string for replacement ‬‮‬‪‬‪‬

The second line is the need to replace the longer string ‪‬‮‬‪‬‪‬

The third line is the input document, which ends with a carriage return.

import re
s = input("")
l = input("")
w = input("")
w = w.replace(s,l)
print(w)

  ID card processing

18-digit ID number: the 7th, 8th, 9th, and 10th digits are the year of birth (four digits), the 11th and 12th digits are the month of birth, the 13th and 14th digits represent the date of birth, 
the 17th digit represents gender, and an odd number For males, even for females.
The user enters a legal ID number, please output the user's birth date, age and gender. (The legality of the input is not required to be checked)
Please first check the special explanation below.
import datetime
ID = input('')
ID_birth=ID[6:14]
ID_sex=ID[14:17]
year=ID_birth[0:4]
month=ID_birth[4:6]
day=ID_birth[6:8]
d = 2020-int(year)
print ("You were born in {: .0f} year {: .0f} month {: .0f} day" .format (int (year), int (month), int (day)))
print ("Your {: .0f} year old" .format (d))
if int(ID_sex)%2 == 0:
    print ("Your gender is female")
else:
    print ("Your gender is male")

  

Guess you like

Origin www.cnblogs.com/wangyingjie123/p/12752043.html