본문 바로가기

Python

(68)
[Python/Bronze V] 팩토리얼 3 - 27434, math.factorial 문제 설명0보다 크거나 같은 정수 N이 주어진다. 이때, N!을 출력하는 프로그램을 작성하시오. codeimport sysimport mathinput = sys.stdin.readlinen = int(input().strip())print(math.factorial(n))정수 n이 최대 100,000까지 주어질 수 있기 때문에 for문을 써서 factorial 함수를 구현하면 시간 초과가 뜹니다.따라서 math의 factorial 함수를 이용하여 문제를 풀었습니다.https://github.com/seonmin5/codingtest_Python GitHub - seonmin5/codingtest_PythonContribute to seonmin5/codingtest_Python development b..
[Python/Bronze II] 알파벳 찾기 - 10809, find 문제 설명알파벳 소문자로만 이루어진 단어 S가 주어진다. 각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를, 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오. code  - 1차import stringimport syslowerList = list(string.ascii_lowercase)input = sys.stdin.readlineword = input()answer = []for l in lowerList: if l not in word: answer.append("-1") else: answer.append(str(word.index(l)))print(' '.join(answer))  code  - 2차import s..
[Python/level 0] 세로 읽기 - 181904, 리스트 슬라이싱 문제 설명문자열 my_string과 두 정수 m, c가 주어집니다. my_string을 한 줄에 m 글자씩 가로로 적었을 때 왼쪽부터 세로로 c번째 열에 적힌 글자들을 문자열로 return 하는 solution 함수를 작성해 주세요. code - 1차def solution(my_string, m, c): result = '' for s in range(c-1, len(my_string), m): result += my_string[s] return result  code - 2차def solution(my_string, m, c): return (my_string[c-1::m])리스트 슬라이싱 문법을 생각하여 코드 리팩토링하였습니다.https://github.com/se..
[Python/level 0] 날짜 비교하기 - 181838, int(date1 > date2) 문제 설명정수 배열 date1과 date2가 주어집니다. 두 배열은 각각 날짜를 나타내며 [year, month, day] 꼴로 주어집니다. 각 배열에서 year는 연도를, month는 월을, day는 날짜를 나타냅니다.만약 date1이 date2보다 앞서는 날짜라면 1을, 아니면 0을 return 하는 solution 함수를 완성해 주세요. code - 1차def solution(date1, date2): if date2[0] > date1[0]: return 1 elif date2[0] >= date1[0] and date2[1] > date1[1]: return 1 elif date2[0] >= date1[0] and da..
[Python/level 0] 빈 배열에 추가, 삭제하기 - 181860, enumerate, extend, del 문제 설명아무 원소도 들어있지 않은 빈 배열 X가 있습니다. 길이가 같은 정수 배열 arr과 boolean 배열 flag가 매개변수로 주어질 때, flag를 차례대로 순회하며 flag[i]가 true라면 X의 뒤에 arr[i]를 arr[i] × 2 번 추가하고, flag[i]가 false라면 X에서 마지막 arr[i]개의 원소를 제거한 뒤 X를 return 하는 solution 함수를 작성해 주세요. code  - 1차def solution(arr, flag): result = [] for i in range(len(arr)): if flag[i]: result += [arr[i]]*2*arr[i] else: result ..
[Python/level 1] 두 정수 사이의 합 - 12912, sum + 리스트 컴프리헨션 vs. for 반복문 문제 설명두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. code - 1차def solution(a, b): result = 0 if b >= a: for i in range(a, b+1): result += i return result else: for i in range(b, a+1): result += i return result  code - 2차# sum + 리스트 컴프리헨션def solution(a, b): if a > b:..
[Python/level 0] 배열의 원소만큼 추가하기 - 181861 문제 설명아무 원소도 들어있지 않은 빈 배열 X가 있습니다. 양의 정수 배열 arr가 매개변수로 주어질 때, arr의 앞에서부터 차례대로 원소를 보면서 원소가 a라면 X의 맨 뒤에 a를 a번 추가하는 일을 반복한 뒤의 배열 X를 return 하는 solution 함수를 작성해 주세요. code - 1차def solution(arr): result = [] for i in arr: for _ in range(i): result.append(i) return result code - 2차def solution(arr): x = [] for i in arr: x += [i] * i return x이중 for문을 하지 않는 방식으로 코..
[Python/level 0] 숫자 찾기 - 120904, enumerate 문제 설명정수 num과 k가 매개변수로 주어질 때, num을 이루는 숫자 중에 k가 있으면 num의 그 숫자가 있는 자리 수를 return하고 없으면 -1을 return 하도록 solution 함수를 완성해보세요. code - 1차def solution(num, k): if str(k) in str(num): return (str(num).index(str(k))+1) else: return (-1) code - 2차def solution(num, k): for idx, n in enumerate(str(num)): if n == str(k): return idx+1 return -1https://github.com/seonm..

반응형