The second past question that I have already solved
Whether it is divisible by 4 is OK with the last 2 digits
answerA.py
r,g,b=map(int,input().split())
if (10*g+1*b)%4==0:
print("YES")
else:
print("NO")
If you also use the ternary operator
answerA_better.py
r,g,b=map(int,input().split())
print("YES" if (10*g+1*b)%4==0 else "NO")
Minimum coordinates → Maximum coordinates are naturally minimum
answerB.py
n=input()
a=[int(i) for i in input().split()]
print(max(a)-min(a))
It can be simpler than when I wrote it before. You can usually write the corresponding array in order from the front. However, if all are 3200 or more, the minimum will be 1, so be careful only there.
answerC.py
x=[0]*9
n=int(input())
a=[int(i) for i in input().split()]
for i in range(n):
for j in range(8):
if a[i]<400*(j+1):
x[j]=1
break
else:
x[8]+=1
if x[:-1].count(1)!=0:
print(x[:-1].count(1),end=" ")
else:
print(1,end=" ")
print(x[:-1].count(1)+x[-1])
When I solved it for the first time, I found it really difficult. In short, it suffices if a pair of ** "(" and ")" exists as a pair **. In order for a pair of "(" and ")" to exist as a pair, the number of "(" must always be greater than ")" and the number of "(" and ")" must be the same. When you start thinking about nesting, it gets complicated. In order to make the number of parentheses ** consistent like this, you should think like a stack **! !! ** If you think like a stack, you can check with +1 and -1 without having to pack it in the stack ** (the number corresponds to the size of the stack.)
answerD.py
n=int(input())
s=input()
if s[0]=="(":
c=1
d=1
else:
c=-1
d=-1
for i in range(1,n):
if s[i]=="(":
d+=1
else:
d-=1
c=min(c,d)
if c<0:
s="("*(-c)+s
print(s+")"*(s.count("(")-s.count(")")))
Recommended Posts