본문 바로가기

Python

(68)
[Python/level 1] 정수 내림차순으로 배치하기 - 12933, sorted(reverse = True) 문제 설명함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다. code - 1차def solution(n): result = [i for i in str(n)] result.sort() result.reverse() return int(''.join(result)) code - 2차def solution(n): result = sorted([s for s in str(n)], reverse=True) return int(''.join(result))https://github.com/seonmin5/codingtest_Python Gi..
[Python/level 0] 문자열 잘라서 정렬하기 - 181866, filter(function, iterable) 문제 설명문자열 myString이 주어집니다. "x"를 기준으로 해당 문자열을 잘라내 배열을 만든 후 사전순으로 정렬한 배열을 return 하는 solution 함수를 완성해 주세요.단, 빈 문자열은 반환할 배열에 넣지 않습니다. filter(function, iterable) Parameter Description functionA Function to be run for each item in the iterableiterableThe iterable to be filteredThe filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or n..
[Python/level 0] 문자열 정렬하기 (2) - 120911, sorted 문제 설명영어 대소문자로 이루어진 문자열 my_string이 매개변수로 주어질 때, my_string을 모두 소문자로 바꾸고 알파벳 순서대로 정렬한 문자열을 return 하도록 solution 함수를 완성해보세요. code - 1차def solution(my_string): lowerString = sorted(s for s in my_string.lower()) return (''.join(lowerString)) code - 2차def solution(my_string): return ''.join(sorted((my_string).lower())) https://github.com/seonmin5/codingtest_Python GitHub - seonmin5/codingtest_P..
[Python/level 0] 369게임 - 120891, str, count 문제 설명머쓱이는 친구들과 369게임을 하고 있습니다. 369게임은 1부터 숫자를 하나씩 대며 3, 6, 9가 들어가는 숫자는 숫자 대신 3, 6, 9의 개수만큼 박수를 치는 게임입니다. 머쓱이가 말해야하는 숫자 order가 매개변수로 주어질 때, 머쓱이가 쳐야할 박수 횟수를 return 하도록 solution 함수를 완성해보세요. code - 1차def solution(order): count = 0 for n in str(order): if n == "3" or n == "6" or n == "9": count += 1 return count code - 2차def solution(order): order_count = str(order).coun..
[Python/level 1] 모의고사 - 42840, enumerate(iterable, start=0) 문제 설명수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다.1번 수포자가 찍는 방식: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...2번 수포자가 찍는 방식: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...3번 수포자가 찍는 방식: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...1번 문제부터 마지막 문제까지의 정답이 순서대로 들은 배열 answers가 주어졌을 때, 가장 많은 문제를 맞힌 사람이 누구인지 배열에 담아 return 하도록 solution 함수를 작성해주세요..
[Python/level 0] 인덱스 바꾸기 - 120895, 리스트 문제 설명문자열 my_string과 정수 num1, num2가 매개변수로 주어질 때, my_string에서 인덱스 num1과 인덱스 num2에 해당하는 문자를 바꾼 문자열을 return 하도록 solution 함수를 완성해보세요. code - 1차def solution(my_string, num1, num2): myList = list(my_string) answer = [] for i in range(len(myList)): if i == num1: answer.append(myList[num2]) elif i == num2: answer.append(myList[num1]) else: an..
[Python/level 0] 문자열의 뒤의 n글자 - 181910, len, 리스트 슬라이싱 문제 설명문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 뒤의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요. code - 1차def solution(my_string, n): answer = '' myList = list(my_string) startPoint = len(my_string)-n for i in myList[startPoint:]: answer += i return answer code - 2def solution(my_string, n): return (my_string[len(my_string)-n:]) https://github.com/seonmin5/cod..
[Python/level 0] 배열의 유사도 - 120903, len, 리스트 컴프리헨션 문제 설명두 배열이 얼마나 유사한지 확인해보려고 합니다. 문자열 배열 s1과 s2가 주어질 때 같은 원소의 개수를 return하도록 solution 함수를 완성해주세요. code - 1차def solution(s1, s2): count = 0 s1.sort() s2.sort() if len(s1) >= len(s2): for i in s1: if i in s2: count += 1 if len(s1)  code - 2차def solution(s1, s2): return len([s for s in s1 if s in s2])코드 리팩토링 내역불필요한 sort를 제거하였습니다.조건에 맞는 s를 list로 받아, le..

반응형