Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- IPv4 주소체계
- 원형 연결 구조 연결된 큐
- LAN의 분류
- 유형 변환
- l-value참조자
- 문제해결 단계
- 백준 파이썬
- 괄호 검사 프로그램
- 네트워크 결합
- 값/참조/주소에 의한 전달
- C언어 스택 연산
- 범위 기반 for문
- c언어 괄호검사
- string유형
- const화
- getline()함수
- 문자형 배열
- const l-value참조자
- 입출력 관리자
- 운영체제 기능
- 프로그래머스 푸드 파이트 대회
- 알고리즘 조건
- 주기억장치
- auto 키워드
- 회전 및 자리 이동 연산
- 논리 연산
- r-value참조자
- 프로그래머스 배열만들기4
- C언어 덱
- C언어 계산기 프로그램
Archives
- Today
- Total
chyam
[프로그래머스 Lv2,python]- 파일명 본문
https://school.programmers.co.kr/learn/courses/30/lessons/17686
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
def f(files): # head,number,tail 나누기
head,number,tail=[],[],[]
for name in files:
idx,cnt=-1,0
for i in range(len(name)):
if name[i].isdigit(): # 숫자이면
if idx==-1: # 처음 나온 숫자일때
idx=i # 인덱스 저장해주기
cnt+=1 # 숫자 몇자리인지
if name[i]==".": # .뒤에도 숫자가 있을수있으므로 멈춰주기
break
head.append(name[:idx]) # 숫자 전까지
number.append(name[idx:idx+cnt]) # 숫자
tail.append(name[idx+cnt:]) # 숫자 이후
return head,number,tail
def solution(files):
le=len(files)
head,number,tail=f(files)
for i in range(le-1):
for j in range(le-1):
if head[j].lower()>head[j+1].lower(): # 먼저 파일 이름과 비교하여 정렬
head[j],head[j+1]=head[j+1],head[j]
number[j],number[j+1]=number[j+1],number[j]
tail[j],tail[j+1]=tail[j+1],tail[j]
elif head[j].lower()==head[j+1].lower(): # 파일 이름이 같으면 숫자비교하기
if int(number[j])> int(number[j+1]):
head[j],head[j+1]=head[j+1],head[j]
number[j],number[j+1]=number[j+1],number[j]
tail[j],tail[j+1]=tail[j+1],tail[j]
for i in range(le):
files[i]=head[i]+number[i]+tail[i] # 합쳐주기
return files
위의 풀이는 버블정렬을 사용하여 푼 내 코드이고, 아래는 다른분이 푼 코드이다.
def head_compare(data):
route = 0
answer = ['', '']
for i, v in enumerate(data):
if (route == 0):
if (v.isnumeric() == True): # 숫자일때
answer[0] = data[:i].lower() # 이전까지는 파일 이름
route = 1
answer[1] += v
elif (route == 1): # 숫자일때
if (v.isnumeric() == True):
answer[1] += v
else: # 숫자가 아니면 끝내기
break
answer[1] = int(answer[1]) # int형으로 변환해주기
return answer
def solution(files):
answer = []
files.sort(key = lambda x:head_compare(x)) # answer을 기준으로 정렬하기
return files
'프로그래머스 > LV2' 카테고리의 다른 글
[프로그래머스 Lv2,python]- 오픈채팅방 (1) | 2025.02.05 |
---|---|
[프로그래머스 Lv2, python] - 2 x n 타일링 (0) | 2025.02.04 |
[프로그래머스 Lv2,python]- 땅따먹기 (0) | 2025.01.31 |
[프로그래머스 Lv2,python] - 모음 사전 (3) | 2025.01.28 |
[프로그래머스 Lv2,python]- 주차 요금 계산 (1) | 2025.01.27 |