목록Coding test(Python3)/Programmers (115)
NIRVANA
문제 배열 arr가 주어집니다. 배열 arr의 각 원소는 숫자 0부터 9까지로 이루어져 있습니다. 이때, 배열 arr에서 연속적으로 나타나는 숫자는 하나만 남기고 전부 제거하려고 합니다. 단, 제거된 후 남은 수들을 반환할 때는 배열 arr의 원소들의 순서를 유지해야 합니다. 예를 들면, arr = [1, 1, 3, 3, 0, 1, 1] 이면 [1, 3, 0, 1] 을 return 합니다. arr = [4, 4, 4, 3, 3] 이면 [4, 3] 을 return 합니다. 배열 arr에서 연속적으로 나타나는 숫자는 제거하고 남은 수들을 return 하는 solution 함수를 완성해 주세요. 문제 풀이 접근법1 1) arr의 i번째 원소와 i+1번째 원소를 비교하여 같으면 리스트에서 i번째 원소를 삭제한다..
문제 두 수를 입력받아 두 수의 최대공약수와 최소공배수를 반환하는 함수, solution을 완성해 보세요. 배열의 맨 앞에 최대공약수, 그다음 최소공배수를 넣어 반환하면 됩니다. 예를 들어 두 수 3, 12의 최대공약수는 3, 최소공배수는 12이므로 solution(3, 12)는 [3, 12]를 반환해야 합니다. 문제 풀이 접근법1 처음에는 유클리드 알고리즘을 사용해서 문제를 푸는 방법을 생각했다. 1) n % m == r(단, n > m)일때, n과 m의 최대 공약수는 m과 r의 최대 공약수와 같고 만약 m == 0이면 m이 n과 m의 최대 공약수가 된다. 2) 두 수의 곱은 최대 공약수와 최대 공배수의 곱이므로 두수의 곱을 최대 공약수로 나누면 최대 공배수를 구할 수 있다. def solution(n..
문제 이 문제에는 표준 입력으로 두 개의 정수 n과 m이 주어집니다. 별(*) 문자를 이용해 가로의 길이가 n, 세로의 길이가 m인 직사각형 형태를 출력해보세요. 문제 풀이 접근법1 1) 세로의 개수 m만큼 반복 2) 가로의 개수 n * "*" 개 출력 a, b = map(int, input().strip().split(' ')) for i in range(b): print("*"*a) 다른 분들 풀이 a, b = map(int, input().strip().split(' ')) answer = ('*'*a +'\n')*b print(answer) 와 생각해보니 그냥 b(m)도 곱해주면 되는거였잖아...? 무조건 for문 사용하는 습관 진짜 버려야할 것 같다!! 참, 기존에 주어진 코드에서 strip()..
문제 행렬의 덧셈은 행과 열의 크기가 같은 두 행렬의 같은 행, 같은 열의 값을 서로 더한 결과가 됩니다. 2개의 행렬 arr1과 arr2를 입력받아, 행렬 덧셈의 결과를 반환하는 함수, solution을 완성해주세요. 문제 풀이 접근법1 참고: https://yjs-program.tistory.com/324 [python] list 요소 2개씩 묶기 다음과 같이 짝수개 요소가 들어있는 list를 보고, 두개씩 짝지어서 return하고 싶을 때 간단한 기록용 #for문을 쓰면 직관적이지만, 깔끔하지 못함 test = [1, 2, 3, 4, 5, 6, 7, 8] new = [] for i in range(0, len(te yjs-program.tistory.com def solution(arr1, arr2..
문제 문자열 s의 길이가 4 혹은 6이고, 숫자로만 구성돼있는지 확인해주는 함수, solution을 완성하세요. 예를 들어 s가 "a234"이면 False를 리턴하고 "1234"라면 True를 리턴하면 됩니다. 문제 접근법1 1) 문자열의 길이를 확인 한 뒤, 조건에 맞지 않으면 False를 return한다. 2) 알파벳 리스트를 만든 뒤, for문을 통해 알파벳이 문자열에 있는지 count 변수를 통해 확인한다. 3) count가 -1보다 크면 문자열에 알파벳이 있는 것이므로 False를 return한다. import string def solution(s): answer = True alpa = [i for i in string.ascii_letters ] if len(s) < 4 or len(s) ..
문제 새로 생긴 놀이기구는 인기가 매우 많아 줄이 끊이질 않습니다. 이 놀이기구의 원래 이용료는 price원 인데, 놀이기구를 N 번 째 이용한다면 원래 이용료의 N배를 받기로 하였습니다. 즉, 처음 이용료가 100이었다면 2번째에는 200, 3번째에는 300으로 요금이 인상됩니다. 놀이기구를 count번 타게 되면 현재 자신이 가지고 있는 금액에서 얼마가 모자라는지를 return 하도록 solution 함수를 완성하세요. 단, 금액이 부족하지 않으면 0을 return 하세요.제한사항 놀이기구의 이용료 price : 1 ≤ price ≤ 2,500, price는 자연수 처음 가지고 있던 금액 money : 1 ≤ money ≤ 1,000,000,000, money는 자연수 놀이기구의 이용 횟수 count..