๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต | ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณ ๋“์  KIT > ์ •๋ ฌ > ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๊ธฐ์ดˆ๋‹ค์ง€๊ธฐ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต | ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณ ๋“์  KIT > ์ •๋ ฌ > ๊ฐ€์žฅ ํฐ ์ˆ˜

๋˜ํšจ๋‹ˆ 2024. 10. 23. 17:46

https://school.programmers.co.kr/learn/courses/30/lessons/42746

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

LV2. ๊ฐ€์žฅ ํฐ ์ˆ˜

๐Ÿ’ก์•„์ด๋””์–ด

์˜ˆ์ œ) numbers = [3, 30, 34, 5, 9]

๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ sort() ์‚ฌ์šฉํ•  ์ƒ๊ฐ์€ ํ–ˆ๋Š”๋ฐ 9 > 5> 3 ์ดํ›„ 3, 30, 34 ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ์ง€๊ฐ€ ๊ณ ๋ฏผ์ด์—ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ ๊ฐ™์€ ์ˆซ์ž์ผ ๋•Œ, ๋‹ค์Œ๋ฒˆ์งธ ์ž๋ฆฌ์ˆ˜๋กœ ๋น„๊ตํ• ๊นŒ๋„ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ตฌํ˜„์ด ๊นŒ๋‹ค๋กญ๊ณ  ๋ณต์žกํ•˜๋‹ค.

์ด ๋ฌธ์ œ๋Š” ์ž๋ฆฌ์ˆ˜๋ฅผ ๋งž์ถฐ์„œ ๋น„๊ตํ•˜๋Š”๊ฒŒ ์•„์ด๋””์–ด์˜ ํ•ต์‹ฌ์ด์—ˆ๋‹ค.

๋ฌธ์ œ์—์„œ numbers๋Š” 0 ์ด์ƒ 1,000 ์ดํ•˜์ด๋ฏ€๋กœ ์ตœ๋Œ€ 4์ž๋ฆฌ๋ฅผ ๊ฐ–๋Š”๋‹ค.

๋”ฐ๋ผ์„œ, 4์ž๋ฆฌ์ˆ˜ ๋งŒํผ ์ˆซ์ž๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ž๋ฆฌ์ˆ˜๋ฅผ ๋งž์ถ˜ ํ›„ ๋น„๊ตํ•œ๋‹ค.

ex. 3 -> 3333, 30 -> 3030,  34 -> 3434, 5 -> 5555, 9 -> 9999 ๋ณ€ํ™˜ํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉด 9534330 ์˜ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

๐Ÿ’ป ์ฝ”๋“œ์ž‘์„ฑ

# ex. 6, 10, 2
# 6666, 1010, 2222
# ex. 3, 30, 34, 5, 9
# 3333, 3030, 3434, 5555, 9999
def solution(numbers):
    answer = ''
    # 1. ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
    numbers = list(map(str, numbers))
    # print(numbers)
    # 2. 4์ž๋ฆฌ์ˆ˜๋กœ ๋ณ€๊ฒฝ
    # sort()๋Š” ์›๋ณธ ์ž์ฒด๋ฅผ ์ •๋ ฌ์‹œ์ผœ ์ค€๋‹ค.     
    # numbers.sort(key=lambda x: (x * 4)[:4], reverse=True)
    # sorted()๋Š” ์›๋ณธ์„ ๋ณ€ํ˜•์‹œํ‚ค์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด list๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    numbers = sorted(numbers, key=lambda x: (x *4)[:4], reverse=True)
    # 3. ๋ฌธ์ž ํ•ฉ์นœ๋‹ค
    # 0000์˜ ๊ฒฝ์šฐ 0 ์ถœ๋ ฅ. int๋ณ€ํ™˜ ํ›„ ๋‹ค์‹œ str๋กœ ํ˜•๋ณ€ํ™˜    
    answer = str(int("".join(numbers)))
    
    return answer
๋ฐ˜์‘ํ˜•
Comments