This is the second past question.
It wasn't enough time last night, so I filled in a simple time.
For ternary continuation, you only have to compare two consecutive ones twice (if you change the comparison method and use a ternary operator, it will be as follows).
answerA.py
n=input()
x=[int(i) for i in n]
if (x[0]==x[1] and x[1]==x[2]) or (x[1]==x[2] and x[2]==x[3]):
print("Yes")
else:
print("No")
answerA_better.py
n=input()
x=[int(i) for i in n]
print("Yes" if x[1]==x[2] and (x[0]==x[1] or x[2]==x[3]) else "No")
Just turn the loop and think in order from the front. The second one is a shorter code
answerB.py
n=int(input())
k,l=2,1
for i in range(n-1):
m=l
l=k+l
k=m
print(l)
answerB_better.py
k,l=-1,2
for i in [0]*int(input()):k,l=l,k+l
print(l)
I wrote a very long code, but in the end there are only 8 ways ...
answerC.py
a,b,c,d=[int(i) for i in input()]
l=7-a
f=[-1,-1,-1]
for i in range(2):
for j in range(2):
for k in range(2):
l_sub=l
if i==0:
l_sub-=(b)
else:
l_sub-=(-b)
if j==0:
l_sub-=(c)
else:
l_sub-=(-c)
if k==0:
l_sub-=(d)
else:
l_sub-=(-d)
if l_sub==0:
f=[i,j,k]
if f!=[-1,-1,-1]:
break
if f!=[-1,-1,-1]:
break
s=str(a)
if f[0]==0:
s+="+"
else:
s+="-"
s+=str(b)
if f[1]==0:
s+="+"
else:
s+="-"
s+=str(c)
if f[2]==0:
s+="+"
else:
s+="-"
s+=str(d)
s+="=7"
print(s)
answerC_better.py
a,b,c,d=[int(i) for i in input()]
if a+b+c+d==7:
print("{}+{}+{}+{}=7".format(a,b,c,d))
elif a+b+c-d==7:
print("{}+{}+{}-{}=7".format(a,b,c,d))
elif a+b-c+d==7:
print("{}+{}-{}+{}=7".format(a,b,c,d))
elif a-b+c+d==7:
print("{}-{}+{}+{}=7".format(a,b,c,d))
elif a+b-c-d==7:
print("{}+{}-{}-{}=7".format(a,b,c,d))
elif a-b+c-d==7:
print("{}-{}+{}-{}=7".format(a,b,c,d))
elif a-b-c+d==7:
print("{}-{}-{}+{}=7".format(a,b,c,d))
else:
print("{}-{}-{}-{}=7".format(a,b,c,d))
When I solved it before, I made a lot of mistakes, so I was rushing to solve it. The mistake I made at that time was ** indexing error ** and ** [] * n to make all nested arrays the same object **. I couldn't make a mistake if I noticed both of them, so I was able to solve them smoothly this time. Also, since this problem is to find the minimum magical power ** when each number is finally changed to 1, it means that you can go through some numbers on the way. In other words, ** it can be reduced to the problem of finding the shortest path from a certain number i (0 <= i <= 9) to 1, ** and it should lead to the idea of using the WF method easily. If you use the WF method, you can rewrite the numbers in each cell. Also, at this time, each cell can be rewritten ** independently **, which is thought to be useful for coming up with the idea that the WF method should be used first.
answerD.py
h,w=map(int,input().split())
#Initialization of 2D array used in WF
WF=[list(map(int,input().split())) for i in range(10)]
#Given wall
a=[list(map(int,input().split())) for i in range(h)]
#WF method
for k in range(10):
for i in range(10):
for j in range(10):
WF[i][j]=min(WF[i][j],WF[i][k]+WF[k][j])
#Rewrite to 1 and give ans magical power+To do
ans=0
for i in range(h):
for j in range(w):
if a[i][j]!=-1:
ans+=WF[a[i][j]][1]
print(ans)
Recommended Posts