🔅코딩테스트 공부🔅/❗프로그래머스(Lv.1)
[프로그래머스] Level1 푸드 파이트 대회(python)
윤무무
2023. 2. 15. 15:30
https://school.programmers.co.kr/learn/courses/30/lessons/134240
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 내가 작성한 코드
def solution(food):
result = []
for i in range(1,len(food)):
for j in range(food[i]//2):
result.append(str(i))
result.append("0")
for i in range(len(food)-1,0,-1):
for j in range(food[i]//2):
result.append(str(i))
return ''.join(result)
2중 for문이 두 번이나 나오는 비효율적인 코드
그냥 "0" 문자열 양 옆에 더해주는 방법이 있구만요~
2. 수정 코드
def solution(food):
answer ="0"
for i in range(len(food)-1, 0,-1):
cnt = int(food[i]/2)
while cnt>0:
answer = str(i) + answer + str(i)
cnt -= 1
return answer