[伯俊]Pythonアルゴリズム練習Day 14


今日はかなり時間がかかりました.連休なので、一人で思い出す時間が多いので、もっといい感じです.以前は難しいと思っていたコードを使って新しい答えを出す姿を見て、実力が上がったと思います.それでも渋滞が多いので、もっと頑張ります.
白駿17299号五等大数
from sys import stdin
N = int(stdin.readline())
num = [i for i in map(int,stdin.readline().split())]
num_fre = dict()
for j in num:
    num_fre[j] = num_fre.get(j,0) + 1
stack = list()
answer =[-1] * N
for i in range(N):
    while stack and num_fre[num[stack[-1]]]<num_fre[num[i]]:
        answer[stack[-1]] = num[i]
        stack.pop()
    stack.append(i)
print(*answer)
白駿1935号後列表記式2
from sys import stdin
N = int(stdin.readline())
sic = stdin.readline().rstrip()
num_list = list()
stack = list()
for i in range(N):
    num_list.append(int(stdin.readline()))
for j in sic:
    if j.isupper():
        stack.append(num_list[ord(j)-65])
    else:
        str2 = stack.pop()
        str1 = stack.pop()
        if j == '+':
            stack.append(str1+str2)
        elif j == '-':
            stack.append(str1-str2)
        elif j == '*':
            stack.append(str1*str2)
        elif j == '/':
            stack.append(str1/str2)
print('%.2f' %stack[0])
白駿1918号後列表記式
from sys import stdin
sic = stdin.readline().rstrip()
stack = list()
ans = ''
for j in sic:
    if j.isupper():
        ans+=j
    else:
        if j == '(':
            stack.append(j)
        elif j=='*' or j=='/': # 우선순위가 낮은 +,- 출력
            while stack and (stack[-1]=='*' or stack[-1]=='/'):
                ans += stack.pop()
            stack.append(j)
        elif j=='+' or j=='-': # ( 나올때까지 출력
            while stack and stack[-1] != '(':
                ans += stack.pop()
            stack.append(j)
        elif j == ')': # ( 나올때까지 출력
            while stack and stack[-1] !='(':
                ans += stack.pop()
            stack.pop()
while stack:
    ans += stack.pop()
print(ans)
白駿10808号アルファベット数
from sys import stdin
S = stdin.readline().rstrip()
num = {'a': 0,'b': 0,'c': 0,'d': 0,'e': 0,'f': 0,'g': 0,'h': 0,'i': 0,'j': 0,'k': 0,'l': 0,'m': 0,'n': 0,'o': 0,'p': 0,'q': 0,'r': 0,'s': 0,'t': 0,'u': 0,'v': 0,'w': 0,'x': 0,'y': 0,'z': 0}
stack = list()
for i in S:
    num[i] = num.get(i,0)+1
for j in range(26):
    stack.append(num[chr(j+97)])
print(*stack)
白駿10809号のアルファベットを検索
from sys import stdin
S = stdin.readline().rstrip()
num = {'a': -1,'b': -1,'c': -1,'d': -1,'e': -1,'f': -1,'g': -1,'h': -1,'i': -1,'j': -1,'k': -1,'l': -1,'m': -1,'n': -1,'o': -1,'p': -1,'q': -1,'r': -1,'s': -1,'t': -1,'u': -1,'v': -1,'w': -1,'x': -1,'y': -1,'z': -1}
stack = list()
stack_num = list()
count = 0
for i in S:
    if i not in stack:
        num[i] = count
    stack.append(i)
    count += 1
for j in range(26):
    stack_num.append(num[chr(j+97)])
print(*stack_num)
白駿10820号文字列分析
from sys import stdin
while True:
    S = stdin.readline().rstrip('\n') # 공백을 다 없애버려서 초반에 오류
    up_count, down_count,num_count,blank_count = 0,0,0,0
    if not S:
        break
    for i in S:
        if i.isupper():
            up_count +=1
        elif i.islower():
            down_count +=1
        elif i.isdigit():
            num_count +=1
        elif i.isspace():
            blank_count+=1
    print(down_count, up_count, num_count, blank_count)
白駿2743号単語長測定
from sys import stdin
S = stdin.readline().rstrip()
print(len(S))
白駿11655 ROT 13
from sys import stdin
S = stdin.readline().rstrip()
ans = ''
for i in S:
    if i.isupper() and i <= 'M':
        ROT = chr(ord(i)+13)
    elif i.isupper() and i > 'M':
        ROT = chr(ord(i)-13)
    elif i.islower() and i <= 'm':
        ROT = chr(ord(i)+13)
    elif i.islower() and i > 'm':
        ROT = chr(ord(i)-13)
    elif i.isspace:
        ROT = i
    ans += ROT
print(ans)
白俊10824番4曲.
from sys import stdin
A,B,C,D = stdin.readline().split()
print(int(A+B)+int(C+D))
白駿11656号接尾辞配列
from sys import stdin
S = stdin.readline().rstrip()
stack = list()
for i in range(len(S)):
    stack.append(S[i:])
stack.sort()
for i in stack:
    print(i)
白準2609号最大公倍数と最小公倍数
import math
num1,num2 = map(int,input().split())
ans1 = math.gcd(num1,num2)
ans2 = int(num1*num2/ans1)
print(ans1)
print(ans2)
白駿6588号金バッハの推測
from sys import stdin
def prime(num):
    if num==1:
        return False
    else:
        for i in range(2, int(num**0.5)+1):
            if num%i==0:
                return False
        return True

while True:
    N = int(stdin.readline())
    if N==0:
        break
    a = N
    b = 0
    while True:
        if prime(a)==True and prime(b)==True:
            print(N,'=',int(b),'+',int(a))
            break
        else:
            a -= 1
            b += 1