The second week of operation Python

1, Caesar cipher B

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

Caesar Cipher is the Roman Julius Caesar used to be military intelligence encryption algorithm, which uses an alternative method for each English character cycle information with a third character behind the character alphabet sequence, namely, correspondence relationship alphabet as follows:

原文: ABCDEFGHIJKLMNOPQRSTU VWXYZ

密文: ABC

For the original character P, the character ciphertext C which satisfy the following conditions: C = (P + 3) mod 26

The above is Caesar cipher encryption method, decryption method and vice versa, i.e.: P = (C-3) mod 26

My code:

P = input("")
l = len(P)
for i in range(l):
if ord('A') <=ord(P[i])<=ord('Z'):
Q=ord('A')+((ord(P[i])-ord('A'))+3)%26
elif ord('a') <=ord(P[i])<=ord('z'):
Q=ord('a')+((ord(P[i])-ord('a')) + 3 )%26
else:
Q=ord(P[i])
new=chr(Q)
print(new,end='')

 

 

 

2. A detection bracket pairs

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

The user input string line, which may include the parentheses (), check whether the parentheses are correctly paired, pairing success outputs:

The pairing is successful, unsuccessful pairing

Wherein paired parentheses to consider matching sequence, i.e., () indicates pairing) (not paired, pairing only consider parentheses.

Note that this is a topic OJ, get input using input ( "").

My code:

Str=input("")
Left_bracket=0
Left_bracket_Z=0
Right_bracket_Z=0
for i in Str:
if i== '(':
Left_bracket+=1
elif i== ')':
if Left_bracket>0:
Left_bracket-=1
else:
print("配对不成功")
break
elif i=='[':
Left_bracket_Z+=1
elif i==']':
Right_bracket_Z+=1
else:
if Left_bracket!=0:
print("配对不成功")
elif Left_bracket_Z!=Right_bracket_Z:
print("配对不成功")
else:
print("配对成功")

 

 

 

3, every day of power B

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

365 days a year, the ability to base the value on day 1, referred to as 1.0. When learn, to improve the ability value with the previous day N ‰; when there is no learning, forgotten since the previous day relative to other reasons decreased ability value N ‰. Effort or indulge every day, how much capacity a year down the difference between the value of it? Wherein, N ranges from 1 to 10, N may be a decimal.

Obtaining user input N, the computational effort and letting ratio between capacity and ability value of 365 days per day per day, which retained the ability value to one decimal place, the ratio between output capacity integer between outputs a "comma + space" format.

My code:

NN=eval(input(""))
Working=pow((1.0+0.001*NN),364)
Pig=pow((1.0-0.001*NN),364)
BB=int(Working//Pig)
print("{:.2f}, {:.2f}, {}".format(Working,Pig,BB))

 

4, the same symbol Math

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

Read an integer N, the following are calculated:

1. N absolute value;

2. N with symbols for addition, subtraction, multiplication and 10, referring to the same operation using the symbol N and the absolute value calculates another number, the absolute value of the calculation result are given the same reference numerals N, wherein symbol 0 is a positive number .

The results in the above-described four output line, a blank-separated outputs are integers.

 

My code:

n = eval(input())
N = abs(n)
a = N + 10
b = N - 10
c = N * 10
if n < 0:
a = -abs(a)
b = -abs(b)
c = -abs(c)
else:
a = abs(a)
b = abs(b)
c = abs(c)
print(N, a , b, c, end = "")

 

 

 

 

5, happy digital

description

Write an algorithm to determine whether a number is "happy." Happy figure is determined as follows: starting from a positive integer, with each square of which number and the number of substituents, and repeat the process until the final digital convergence or equal to 1 and equal to 1 has been, or will cycle endlessly and ultimately will not go convergence equal to 1. 1 can equal the number of final convergence is a happy number.

 

My code:

def happy(n):
try:
if n==1:
print('True')
else:
new = str(n)
sum = 0
for c in new:
sum += int(c)**2
return happy(sum)
except Exception as e:
print('False')
# print(e)

n = eval(input())
happy(n)

 

 

 

6, inverse code A string

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

Definition string is inverted: the inverted character string consisting of character string included.

Anti-character code is defined as:

(1) The lower-case characters, its inverse is also a lower-case characters, and the characters 'a' is equal to the distance of the inverted 'z' distance;

(2) For the capital English characters, its inverse is also a capital English characters, and the characters from 'A' is equal to the distance which the inverted and 'Z' is;

It refers to a difference from the two characters corresponding unicode encoded.

 

My code:

while True:
try:
string=input()
if string!="!":
res=""
for i in string:
if i.isupper():
res += chr(ord("Z")-(ord(i)-ord("A")))
elif i.islower():
res += chr(ord("z") - (ord(i) - ord("a")))
else:
res+=i
print(res)
except:
break

 

 

 

7, the rectangular area is calculated

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

The user input length and width of the rectangle, the area is calculated, and outputs the result rounded to 2 decimal places.

 

My code:

P = float(input(""))
Q = float(input(""))
S = P*Q
print(format(S,'.2f'))

 

 

8, formatted output

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

A user input a decimal format by output format, three decimal places. 

My code:

num = float(input())
print("{:.3f}".format(num))

 

 

9, the output string in reverse

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

description

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

A user input string, descending output. 

My code:

str1 = input()
print(str1[::-1])

 

 

 

10, Zhaomaohuahu factorial

description

 

Is the factorial Keystone Carman (Christian Kramp) 1808 of the operational sign of the invention, a mathematical terms.
A positive integer factorial (factorial) is the product of all positive integers less than and equal to the number, and 0 is 1 factorial. Writing the factorial of a natural number n n !.
Any natural number greater than or equal to n factorial representation 1:

 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

Reference example code, any program seeking factorial integer greater than zero.

My code:

P = int(input())
Q = 1
for i in range(1, P+1):
Q = Q*i
print("%d" % float(Q))

 

Guess you like

Origin www.cnblogs.com/Adaran/p/12546847.html