1. Square Matrix II
It didn’t feel like it was written in this way before, but I think this idea is pretty good
m=[[0 for i in range(105)]for j in range(105)]
while True:
n=int(input())
if n==0:
exit()
for i in range(0,n):
m[i][i]=1
for i in range(n):
for j in range(i+1,n):#因为会把另外一边操作了,所以不用管另一边
m[i][j]=m[i][j-1]+1
m[j][i]=m[j-1][i]+1
for i in range(0,n):
for j in range(0,n):
print(m[i][j],end=' ')
print()
print()
2. The right area of the array
Writing method one:
I feel that python is really flexible
method = input()
res = 0
count = 0
input()
for i in range(1,11):
data = list(map(lambda x:float(x),input().split(" ")))
if i<6:
res += sum(data[12-i:])
count += len(data[12-i:])
else:
res += sum(data[i+1:])
count += len(data[i+1:])
if method == "S":
print("{:.1f}".format(res))
else:
print("{:.1f}".format(res/count))
Writing method two:
It's really simple! ! learn! get it!
t, i = input(), [[float(y) for y in input().split()] for x in range(12)]
ans=sum([sum([i[x][y] for y in range(12-x if x<=5 else 12-(5-(x-6)),12)]) for x in range(1,11)])
print("%.1f"%(ans if t=="S" else ans/30))
3. Square matrix I
Thinking questions, hmm! Think of it and make it!
n = int(input())
while n != 0:
for i in range(1, n + 1):
for j in range(1, n + 1):
print(min(i, n - i + 1, j, n -j + 1), end=" ")
print()
print()
n = int(input())
Fourth, the number of numbers in the string
i.isdigit() is a string method in Python, used to determine whether all characters in a string are digits. If so, return True; otherwise, return False.
s = input()
c = 0
for i in s:
if i.isdigit():
c+=1
print(c)
5. Circular restraint
I was stunned by the flexibility of python again! !
n = int(input())
p1 = [["Hunter","Gun"],["Gun","Bear"],["Bear","Hunter"]]
for i in range(n):
n = input().split(" ")
if n[0] == n[-1]:
print("Tie")
elif n in p1:
print("Player1")
else:
print("Player2")
6. String plus spaces
Writing method one:
s=input()
for i in s:
print(i,end=' ')
Writing method two:
print(" ".join(input()))
Writing method three:
for c in input():
print(c, end = ' ')
Seven, replace the string
Writing method one:
print(input().replace(input(),'#'))
Writing method two:
s, t = input(), input()
for c in s:
if c == t:
print('#', end = '')
else:
print(c, end = '')
Eight, string insertion
Writing method one:
try except because he did not specify the number of inputs
while True:
try:
str,substr=input().split()
_max=max(str)
i=str.index(_max)
s=str[:i+1]+substr+str[i+1:]
print(s)
except EOFError:
break
Writing method two:
sys.stdin.readlines() reads multiple lines of input into
import sys
lines=sys.stdin.readlines()
for i in range(len(lines)):
str_, substr_ = lines[i].strip().split()[0], lines[i].strip().split()[1]
max_ = max(str_)
index = str_.index(max_)
print(str_[:index+1] + substr_ + str_[index+1:])
Writing method three:
enumerate
Use the enumerate() function to traverse the string, and each cycle will return the current character and its corresponding subscript.
ord()
Use the ord() function to convert it to an ASCII code value and compare it with the current maximum value
while True:
try:#跟我自己的写法是一样的
str,substr = input().split(" ")
max = 0
index = 0
for i,s in enumerate(str):
if (ord(s) > max):
max = ord(s)
index = i
str_list = list(str)
str_list.insert(index + 1 ,substr)
str = "".join(str_list)
print(str)
except:
break