본문 바로가기

Python

(68)
[Python/level 0] 꼬리 문자열 - 181841, continue 문제 설명문자열들이 담긴 리스트가 주어졌을 때, 모든 문자열들을 순서대로 합친 문자열을 꼬리 문자열이라고 합니다. 꼬리 문자열을 만들 때 특정 문자열을 포함한 문자열은 제외시키려고 합니다. 예를 들어 문자열 리스트 ["abc", "def", "ghi"]가 있고 문자열 "ef"를 포함한 문자열은 제외하고 꼬리 문자열을 만들면 "abcghi"가 됩니다.문자열 리스트 str_list와 제외하려는 문자열 ex가 주어질 때, str_list에서 ex를 포함한 문자열을 제외하고 만든 꼬리 문자열을 return하도록 solution 함수를 완성해주세요. code - 1차def solution(str_list, ex): answer = '' for i in str_list: if ex in i:..
[Python/level 0] 부분 문자열 - 181842, int(True or False) 문제 설명 어떤 문자열 A가 다른 문자열 B안에 속하면 A를 B의 부분 문자열이라고 합니다. 예를 들어 문자열 "abc"는 문자열 "aabcc"의 부분 문자열입니다.문자열 str1과 str2가 주어질 때, str1이 str2의 부분 문자열이라면 1을 부분 문자열이 아니라면 0을 return하도록 solution 함수를 완성해주세요. code - 1차def solution(str1, str2): if str1 in str2: return 1 else: return 0if str1 in str2: # str1이 str2의 부분 문자열이라면return 1 # 1을 반환합니다.else: # 부분 문자열이 아니라면return 0 # 0을 반환합니다. code - 2차def so..
[Python/level 0] 조건에 맞게 수열 변환하기 3 - 181835, 리스트 컴프리헨션 문제 설명정수 배열 arr와 자연수 k가 주어집니다.만약 k가 홀수라면 arr의 모든 원소에 k를 곱하고, k가 짝수라면 arr의 모든 원소에 k를 더합니다.이러한 변환을 마친 후의 arr를 return 하는 solution 함수를 완성해 주세요. code - 1차def solution(arr, k): for i in range(len(arr)): if k % 2 == 0: arr[i] += k else: arr[i] *= k return arrk가 짝수라면 arr의 모든 원소에 k를 더합니다.for i in range(len(arr)): # arr의 길이만큼 i를 반복합니다.if k%2==0: # k가 짝수라면arr[i] += ..
[Python/Silver II] DFS와 BFS - 1260, 스택, 재귀함수, deque, queue 문제 설명그래프를 DFS로 탐색한 결과와 BFS로 탐색한 결과를 출력하는 프로그램을 작성하시오. 단, 방문할 수 있는 정점이 여러 개인 경우에는 정점 번호가 작은 것을 먼저 방문하고, 더 이상 방문할 수 있는 점이 없는 경우 종료한다. 정점 번호는 1번부터 N번까지이다. codeimport sysfrom collections import dequeinput = sys.stdin.readlinen, m, v = map(int, input().split())graph = [[] for _ in range(n+1)]for _ in range(m): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a)for i in ran..
[Python/level 0] 원하는 문자열 찾기 - 181878, lower(), in, int 문제 설명알파벳으로 이루어진 문자열 myString과 pat이 주어집니다. myString의 연속된 부분 문자열 중 pat이 존재하면 1을 그렇지 않으면 0을 return 하는 solution 함수를 완성해 주세요.단, 알파벳 대문자와 소문자는 구분하지 않습니다. code - 1차def solution(myString, pat): lowerString = myString.lower() lowerPat = pat.lower() if lowerPat in lowerString: return 1 else: return 0lower~문제에서 알파벳 대문자와 소문자를 구분하지 않는다고 했기에, 모두 소문자로 변환합니다.lowerString, lowerPatif문low..
[Python] append() vs. extend() 공통점과 차이점 한 눈에 보기 공통점append(), extend() 모두 목록의 끝에 무언가를 추가하는 메서드입니다. 차이점a list에 b list를 추가하고 싶을 때 어떤 메서드를 사용하느냐에 따라 결과는 상이합니다.append: ['apple', 'banana', 'cherry', ["Ford", "BMW", "Volvo"]]extend: ['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo'] append()The append() method appends an element to the end of the list.목록의 끝에 요소를 추가합니다.list.append(elmnt)ParameterDescriptionelmntRequired. An element of any ..
[Python/level 0] 제곱수 판별하기 - 120909, isqrt(), is_integer() 문제 설명어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요. isqrt()The math.isqrt() method rounds a square root number downwards to the nearest integer.제곱근을 가장 가까운 정수로 반올림합니다.Note: The number must be greater than or equal to 0.참고로 숫자는 0보다 크거나 같아야 합니다.math.isqrt(x)ParameterDescriptionxRequired. The number to round the square root of. If x is negati..
[Python/level 0] A 강조하기 - 181874, lower(), replace() 문제 설명문자열 myString이 주어집니다. myString에서 알파벳 "a"가 등장하면 전부 "A"로 변환하고, "A"가 아닌 모든 대문자 알파벳은 소문자 알파벳으로 변환하여 return 하는 solution 함수를 완성하세요. code - 1차def solution(myString): answer = '' replaceString = (myString).replace("a", "A") for i in replaceString: if i != "A" and i.isupper(): answer += i.lower() else: answer += i return answerreplaceStringmyString의 a를 ..

반응형