250x250
Notice
Recent Posts
Recent Comments
Link
상봉동개발자
[프로그래머스] - 이상한 문자 만들기, 최대공약수와 최소공배수, [1차] 캐시, [3차] 파일명 정렬 본문
728x90
이상한 문자 만들기
- 사이트/난이도: 프로그래머스 / 1
- 코드
def solution(s):
arr = s.split()
result = ""
for a in arr:
for i in range(len(a)):
if i % 2 == 0:
result += a[i].upper()
else:
result += a[i].lower()
idx = 0
for i in range(len(s)):
if s[i].isalnum():
s = s[:i] + result[idx] + s[i+1:]
idx += 1
return s
# 다른 사람 풀이
def solution(s):
return " ".join(map(lambda x: "".join([a.lower() if i % 2 else a.upper() for i, a in enumerate(x)]), s.split(" ")))
- 느낀점
- 쉬운 문자열 문제인줄 알았는데 생각보다 엣지 케이스가 많아서 고생한 문제
- 엣지 케이스 종류로는 아래와 같다고 한다.
- 단어 사이 공백이 여러개
- 문자의 시작과 끝에 공백
- 단어에 대문자가 섞여있는 경우
- 한줄로 끝낸 풀이도 있는데…. split(" ") 이렇게 해야지 공백을 하나만 제거해서 나중에 다시 원상태로 할때 제대로 나온다.
최대공약수와 최소공배수
- 사이트/난이도: 프로그래머스 / 1
- 코드
import math
def solution(n, m):
return [math.gcd(n,m), (n*m)//math.gcd(n,m)]
# 유클리드 호제법
def gcd(a, b):
while b > 0:
a, b = b, a % b
return a
def lcm(a, b):
return a * b / gcd(a, b)
- 느낀점
- 간단하게 파이썬 라이브러리를 이용해 푼 풀이
- 최소공배수도 math.lcm 이 있는데 여기서 막은거 같다.
- 유클리드 호제법으로도 풀이 가능하다.
[1차] 캐시
- 사이트/난이도: 프로그래머스 / 2
- 코드
from collections import deque
def solution(cacheSize, cities):
answer = 0
q = deque([])
for city in cities:
city = city.lower()
if city not in q:
answer += 5
q.append(city)
if len(q) > cacheSize:
q.popleft()
else:
answer += 1
q.remove(city)
q.append(city)
return answer
# 다른 사람 풀이
def solution(cacheSize, cities):
import collections
cache = collections.deque(maxlen=cacheSize)
time = 0
for i in cities:
s = i.lower()
if s in cache:
cache.remove(s)
cache.append(s)
time += 1
else:
cache.append(s)
time += 5
return time
- 느낀점
- 큐를 이용해 구현한 문제
- cities의 길이가 10만이지만 cacheSize가 30까지라서 in, remove 등의 함수를 사용해도 시간복잡도가 발생하지 않는다.
- 다른사람은 maxlen 을 사용했는데 이를 사용하면 큐의 길이가 정해져서 오버해서 추가하면 맨 앞원소가 사라진다고 한다.
[3차] 파일명 정렬
- 사이트/난이도: 프로그래머스 / 2
- 코드
def solution(files):
answer = []
for file in files:
num_idx, tail_idx = 0, 0
for i in range(len(file)):
if file[i].isnumeric() and num_idx == 0:
num_idx = i
if not file[i].isnumeric() and num_idx != 0:
tail_idx = i
break
head = file[:num_idx]
if tail_idx != 0:
number = file[num_idx:tail_idx]
tail = file[tail_idx:]
answer.append((head, number, tail))
else:
number = file[num_idx:]
answer.append((head, number))
answer.sort(key = lambda x: (x[0].upper(), int(x[1])))
result = []
for a in answer:
result.append(''.join(a))
return result
# 다른사람 풀이
import re
def solution(files):
a = sorted(files, key=lambda file : int(re.findall('\\d+', file)[0])) # number에 대한 정렬
b = sorted(a, key=lambda file : re.split('\\d+', file.lower())[0]) # head에 대한 정렬
return b
- 느낀점
- 문자열 구현 문제
- head, number, tail 로 나누기 위해서 각각의 인덱스를 기록하는 로직을 짜서 풀었다.
- 근데 다른 사람 풀이 보니 re 정규식을 이용했는데 진짜 짧다…. 나도 공부해야 겠다.
728x90
'코테준비' 카테고리의 다른 글
| [백준] 1912, 1699, 2225 (0) | 2022.10.20 |
|---|---|
| [백준] - 1182, 14391, 11727, 11052, 15990, 10844, 2193, 11053, 14002 (0) | 2022.10.19 |
| [프로그래머스] SQL 난이도 3, 4 문제들 (0) | 2022.10.13 |
| [프로그래머스] - 고득점 Kit 레벨 1~2 문제 (0) | 2022.10.12 |
| [프로그래머스] - 시저 암호, 스킬트리, 후보키, 타겟 넘버, N진수 게임 (0) | 2022.10.11 |
Comments