본문 바로가기
Problem Solving/SWEA

[ SWEA ] D3 - 1217, 1230, 3431, 4406 - python 문제풀이

by IM조이 2021. 6. 29.

1217 거듭제곱

def mul(num,k):
    global n,cnt
    num *= n
    if k == cnt:
        return num
    return mul(num,k+1)

for tc in range(10):
    N = int(input())
    n,cnt = map(int, input().split())
    print("#{} {}".format(N,mul(1,1)))

또 다른 방식

def mul(n,m):
    if m == 1:
        return n
    return mul(n,m-1)*n

for tc in range(10):
    a = int(input())
    b,c = map(int, input().split())
    print('#{} {}'.format(a,mul(b,c)))

 

1230 암호문3
오래 걸리지만, 문제를 잘 이해하고 차근차근 짜면 쉽게 풀리는 문제. 심지어 맨 마지막 조건은 구현하지 않아도 통과가 되버리는 문제.

for tc in range(1,11):
    N = int(input())
    pw = list(map(int, input().split()))
    C = int(input())
    commands = list(input().split())
    for i in range(len(commands)):
        if commands[i] == "I":
            idx = int(commands[i+1])
            cnt = int(commands[i+2])
            for j in range(cnt):
                pw.insert(idx+j,int(commands[i+3+j]))
        elif commands[i] == "D":
            idx = int(commands[i+1])
            cnt = int(commands[i+2])
            for j in range(cnt):
                del pw[idx]
        elif commands[i] == "A":
            cnt = int(commands[i+1])
            for j in range(1,cnt+1):
                pw.append(commands[i+j+1])
    print("#{} {}".format(tc," ".join(map(str,pw[:10]))))

 

3431 준환이의 운동관리

for tc in range(int(input())):
    L,U,X = map(int,input().split())
    print("#{} {}".format(tc+1, -1 if X>U else(L-X if X<L else 0)))

 

 

4406 모음이 보이지 않는 사람
다양한 반법으로 풀 수 있는 문제
- 방법 1 : lambda, filter 식을 사용해서 해당되는 것들만 리스트로 만들어 묶어주는 방법
- 방법 2 : 정규표현식을 사용해 조건에 맞는 값들만 가져오는 방법(단, re 불러와야 함)

방법1 - 짧은 코드지만 가독성이 그리 좋지는 않음

for tc in range(int(input())):
    s = list(input())
    print("#{} {}".format(tc+1, "".join(list(filter(lambda x : x not in ["a","e","i","o","u"],s)))))

 

방법 2 - re불러오는 시간은 걸려도 가독성이 좋고 이해하기 쉬움

import re
for tc in range(int(input())):
    s = input()
    print("#{} {}".format(tc+1,re.sub('a|e|i|o|u','',s)))
    # 참고 - print(s) // congratulations로 원본 그대로 유지됨

 

cf. 그냥 문제 조건대로 차근차근 푸는 방법도 있음

for tc in range(int(input())):
    a = list(map(str,input()))
    b = []
    aeiou = ['a','e','i','o','u']
    for alphabet in a:
        if alphabet in aeiou:
            continue
        else:
            b.append(alphabet)
    print('#{} {}'.format(tc+1,''.join(b)))

댓글