https://app.codesignal.com/interview-practice/task/aRwxhGcmvhf6vKPCp/description
simplifyPath | CodeSignal
Press space bar to start a drag. When dragging you can use the arrow keys to move the item around and escape to cancel. Some screen readers may require you to be in focus mode or to use your pass through key
app.codesignal.com
✔️문제 요약
1.파일의 경로를 정리하는 문제
2. ../ 은 앞으로 가기 , .는 현재 디렉토리 //도 비슷한 것 같다.
✔️풀이 방법
1. /를 다 날려줌 (파이썬 replace split등이 생각났다)
2. 경로를 계속 쌓아준뒤, ..을 만나면 queue 요소를 살펴보고 경우에 따라 pop해준다.
def solution(path):
spl=path.split('/')
stack=[]
for i in range(len(spl)):
queue=spl.pop(0)
if queue == '..':
stack.append(queue)
stack.pop()
if not stack:
pass
else:
stack.pop()
elif queue == '' or queue == '.':
stack.append(queue)
stack.pop()
else:
stack.append(queue)
ans='/'.join(stack)
ans='/'+ans
return ans
✔️고찰
-피드백 : if랑 else 순서를 바꿔서 훨씬 꺠끗하게 할 수 있을 것임
- stack.append() 랑 stack.pop() 뭐하러 두번씀! 안쓰면됨 ! 해서
수정해 보았다 .
def solution(path):
spl=path.split('/')
stack=[]
for i in range(len(spl)):
queue=spl.pop(0)
if queue == '' or queue == '.':
continue
elif queue == '..':
if stack:
stack.pop()
continue
stack.append(queue)
ans='/'.join(stack)
ans='/'+ans
return ans
*pass랑 continue랑 달랐다니!!
'코딩테스트 > 코딩테스트 Python' 카테고리의 다른 글
[프로그래머스] 구명보트(Python) (0) | 2023.04.06 |
---|---|
[CodeSignal] isTreeSymmetric (Python) (0) | 2022.10.07 |
[CodeSignal] sudoku2 (Python) (1) | 2022.09.27 |
[CodeSignal] firstNotRepeatingCharacter (Python) (0) | 2022.09.19 |
[CodeSignal] findProfession (Python) (0) | 2022.09.19 |
댓글