전체 글 247

[Python] 문자열 치환하는 방법 replace

replace str.replace(old, new[, count]) 모든 부분 문자열 old 가 new 로 치환된 문자열의 복사본을 돌려줍니다. 선택적 인자 count 가 주어지면, 앞의 count 개만 치환됩니다. old : 바꾸고 싶은 문자열 new : 새로 바꾸고 싶은 문자열 count : 앞에서 부터 count 개수 만큼 문자 바꾸기 예시 ) a = "hello world" b = "abcd****efgh" print(a.replace("hello", "hi")) print(b.replace("*", "0")) # 전부 변경 print(b.replace("*", "0", 2)) # 부분 변경 (앞에서 부터) """ *output* hi world abcd0000efgh abcd00**efgh """

Algorithm 2023.04.06

[프로그래머스] 비밀지도 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/17681 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 def solution(n, arr1, arr2): answers = [bin(arr1[i] | arr2[i])[2:] for i in range(n)] for i, answer in enumerate(answers): if len(answer) < n: answers[i] = '0'*(n - len(answer)) + answer result = [] for i in answers: i..

프로그래머스 2023.04.05

[Python] 문자열에 다른 문자열 채우는 방법 rjust, ljust, zfill

rjust str.rjust(width[,fillchar]) 오른쪽으로 정렬된 문자열을 길이 width 인 문자열로 돌려줍니다. 지정된 fillchar (default: 스페이스)을 사용하여 채웁니다. ( fillchar 는 선택 ) width 가 len(str) 보다 작거나 같은 경우 원래 문자열이 반환됩니다. 예시) n = 5 # n은 반환되는 문자열의 총 길이 print("12".rjust(n)) print("12".rjust(n, "0")) # fillchar 는 선택 print("12345".rjust(n, "0")) print("12".rjust(n, "A")) """ output 12 00012 12345 AAA12 """ ljust str.ljust(width[,fillchar]) 왼쪽으로..

Algorithm 2023.04.04

백준 11003번 최솟값 찾기 파이썬

https://www.acmicpc.net/problem/11003 11003번: 최솟값 찾기 N개의 수 A1, A2, ..., AN과 L이 주어진다. Di = Ai-L+1 ~ Ai 중의 최솟값이라고 할 때, D에 저장된 수를 출력하는 프로그램을 작성하시오. 이때, i ≤ 0 인 Ai는 무시하고 D를 구해야 한다. www.acmicpc.net 내 코드 import sys from collections import deque input = sys.stdin.readline N, L = map(int, input().split()) A = list(map(int, input().split())) dq = deque() for i in range(N): while dq and (dq[-1][1] > A[i])..

BOJ/Python 2023.02.17

백준 12891번 DNA 비밀번호 파이썬

https://www.acmicpc.net/problem/12891 12891번: DNA 비밀번호 평소에 문자열을 가지고 노는 것을 좋아하는 민호는 DNA 문자열을 알게 되었다. DNA 문자열은 모든 문자열에 등장하는 문자가 {‘A’, ‘C’, ‘G’, ‘T’} 인 문자열을 말한다. 예를 들어 “ACKA” www.acmicpc.net 내 답안 import sys input = sys.stdin.readline S, P = map(int, input().split()) DNA = input().rstrip() DNA_dict = {'A': 0, 'C': 1, 'G': 2, 'T': 3} ACGT = list(map(int, input().split())) result = 0 start_index = 0 e..

BOJ/Python 2023.02.14

[프로그래머스] 개인정보 수집 유효기간 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/150370 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 def todict(a): dictionary = {} for i in a: x, y = i.split() dictionary[x] = int(y)*28 return dictionary def solution(today, terms, privacies): cnt = 1 answer = [] type_dict = todict(terms) today_to_day = (int(today[:4..

프로그래머스 2023.02.05

[프로그래머스] 평행 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/120875 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 def gradient(a, b): # 기울기 계산 return (a[1] - b[1]) / (a[0] - b[0]) def solution(dots): p1, p2, p3, p4 = dots[:4] if gradient(p3, p1) == gradient(p4, p2) or gradient(p4, p3) == gradient(p2, p1): return 1 else: return 0 ..

프로그래머스 2023.02.01

[프로그래머스] 옹알이 (1) 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/120956 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 def solution(babbling): result = 0 for i in babbling: cnt = 0 word = '' for j in i: word += j if word in ['aya', 'ye', 'woo', 'ma']: word = '' cnt += 1 if len(word) == 0 and cnt > 0: result += 1 return result Review 문..

프로그래머스 2023.02.01

[프로그래머스] 두 큐 합 같게 만들기 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/118667 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 from collections import deque def solution(queue1, queue2): cnt = 0 q1 = deque(queue1) q2 = deque(queue2) q1_sum = sum(q1) q2_sum = sum(q2) for _ in range(int(len(q1)*3)): if q1_sum > q2_sum: q1_sum -= q1[0] q2_sum +=..

프로그래머스 2023.01.30

백준 25305번 커트라인 파이썬

https://www.acmicpc.net/problem/25305 25305번: 커트라인 시험 응시자들 가운데 1등은 100점, 2등은 98점, 3등은 93점이다. 2등까지 상을 받으므로 커트라인은 98점이다. www.acmicpc.net 내 답안 import sys input = sys.stdin.readline N, K = map(int, input().split()) score = sorted(list(map(int, input().split()))) print(score[N-K]) Review 이 문제는 구현과, 정렬 보다는 N, K = map(int, input().split()) score = sorted(list(map(int, input().split()))) 입력을 어떤 식으로 받는지가..

BOJ/Python 2023.01.19