목록Coding test(Python3)/Programmers (115)
NIRVANA
문제 대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다. 예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다. 문제풀이 접근법1 1) 문자열 s를 한글자씩 쪼개어 리스트에 저장 2) 반복문을 돌면서 리스트 내에 있는 p와 y의 개수를 count 3) p와 y의 개수가 같다면 true를 반환, 아니면 false를 반환 def solution(s): answer = True count_p = 0 ..
문제 함수 solution은 정수 x와 자연수 n을 입력 받아, x부터 시작해 x씩 증가하는 숫자를 n개 지니는 리스트를 리턴해야 합니다. 다음 제한 조건을 보고, 조건을 만족하는 함수, solution을 완성해주세요. 문제 풀이 접근법1 def solution(x, n): answer = [] if x > 0: for i in range(x, n*x+1, x): answer.append(i) else: for i in range(x, n*x-1, x): answer.append(i) return answer 다음과 같은 방법으로 접근하였는데 런타임 에러가 발생해서 틀렸다. 아마도 x가 0일때는 코드가 예상대로 작동하지 않아서 그런 것 같다. 문제 풀이 접근법2 def solution(x, n): ans..
문제 정수를 담고 있는 배열 arr의 평균값을 return하는 함수, solution을 완성해보세요. 문제 풀이 접근법1 def solution(arr): answer = 0 for i in arr: answer = answer + i answer = answer / len(arr) return answer 다른 분들 풀이 # 문제가 개편되었습니다. 이로 인해 함수 구성이나 테스트케이스가 변경되어, 과거의 코드는 동작하지 않을 수 있습니다. # 새로운 함수 구성을 적용하려면 [코드 초기화] 버튼을 누르세요. 단, [코드 초기화] 버튼을 누르면 작성 중인 코드는 사라집니다. def average(list): # 함수를 완성해서 매개변수 list의 평균값을 return하도록 만들어 보세요. if len(li..
문제 정수 num이 짝수일 경우 "Even"을 반환하고 홀수인 경우 "Odd"를 반환하는 함수, solution을 완성해주세요. 문제 풀이 접근법1 def solution(num): answer = '' if num % 2 == 0: answer='Even' else: answer='Odd' return answer 양심상 한문제 더 풀어야지..^~^ 다른 사람들 풀이 def evenOrOdd(num): #함수를 완성하세요 if num%2: return "Odd" return "Even" print("결과 : " + evenOrOdd(3)) print("결과 : " + evenOrOdd(2)) 너무 깔끔해서 놀랐다. return 값에 바로 문자열이 들어갈 수 있다는 걸 처음 알았다..
문제 자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요. 예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다. 문제 풀이 접근법1 1) 자연수 n을 입력 받기 2) 자연수 n을 int → str로 형변환 3) str로 바뀐 n을 쪼개어서 리스트로 저장 4) map함수를 사용하여서 리스트의 원소를 str→int로 형변환 5) 반복문으로 리스트의 원소 각각을 덧셈 def solution(n): answer = 0 number = [] for i in str(n): number.append(i) number_int = list(map(int, number)) for i in number_int: answer = answer ..
문제 정수 n을 입력받아 n의 약수를 모두 더한 값을 리턴하는 함수, solution을 완성해주세요. 문제 풀이 접근법1 약수는 n으로 나눌 때, 나누어 떨어지는 자연수 x를 의미함으로 다음과 같이 해당 문제를 풀 수 있다. 1) N을 입력 받음 2) 반복문으로 N % i == 0 인 i를 리스트에 저장. 3) 다시 반복문을 통해 리스트의 i를 더한 뒤, sum값을 return def solution(n): divisor = [] answer = 0 for i in range(1, n+1): if n % i == 0: divisor.append(i) for i in range(len(divisor)): answer = answer + divisor[i] return answer 계속 틀렸다고 해서 다시 ..