4:Bee 2023. 12. 29. 15:11
728x90

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉ์‹์€ ์ฒ˜์Œ ๋ถ€ํ„ฐ sort๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  (sort๋ฅผ len๊ธฐ์ค€์œผ๋กœ ๋ณ€ํ˜•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๊ธด ํ–ˆ์Œ) ์ด์— ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ์›ํ• ํ•˜์ง€ ์•Š์•„ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด์„œ ํ’€์ด๋ฒ•์„ ํ™•์ธํ•ด ๋ณด์•˜๋‹ค.


n = int(input())
lst = []

for i in range(n):
    lst.append(input())
set_lst = set(lst)  ## set์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ค‘๋ณต๊ฐ’์„ ์ œ๊ฑฐ
print(type(set_lst))
lst = list(set_lst) ## ๋‹ค์‹œ list๋กœ ๋ณ€ํ™˜
lst.sort(key = len)

for i in lst:
    print(i)
 
<class 'set'>
i
im
it
no
but
more
wait
wont
yours
cannot
hesitate 
 

sort๋ฅผ ๋‹จ์ˆœํžˆ sortํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ธฐ์ค€์„ ์ •ํ•ด์„œ sortํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ set์œผ๋กœ ๋ณ€ํ™˜์„ ํ•˜๋ฉด type์ด set์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ list๋กœ ๋ณ€ํ™˜์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ๋ฌธ์ž์—ด์ด ์•ฝํ•ด์„œ ๋ฌธ์ž์—ด ๋ฌธ์ œ๋ฅผ ์œ„์ฃผ๋กœ ํ’€์–ด๋ณด๊ณ  ์žˆ์—ˆ๋‹ค.

๋‚ด๊ฐ€ ์˜๋„ํ•œ sort๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ์งฐ์„ ๋•Œ์˜ ๋ชจ์Šต์€ ์•„๋ž˜์™€๊ฐ™๋‹ค.

N = ['but', 'i', 'wont', 'hesitate', 'no', 'more',
     'no', 'more', 'it', 'cannot', 'wait', 'im', 'yours']

# ์„ ํƒ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ง์ ‘ ๊ตฌํ˜„
for i in range(len(N)):  # N์˜ ๋ฐฐ์—ด ๊ธธ์ด๋ฅผ i๋กœ ๋ฐ˜ํ™˜
    min_index = i
    for j in range(i + 1, len(N)):  # 1์—์„œ๋ถ€ํ„ฐ N์˜ ๋ฐฐ์—ด ๊ธธ์ด ๋งŒํผ
        #์ฒซ๋ฒˆ์งธ for๋ฌธ์ด i(min_index)์ผ๋•Œ i๋ฅผ ๊ธฐ์ค€์œผ๋กœ j๋กœ ๋ฐ˜ํ™˜๋œ ์ „์ฒด ๊ธธ์ด๋งŒํผ ๋ฃจํ”„๊ฐ€ ๋ˆ๋‹ค.
        print(j, end=' ')
        # min_index๋ฅผ ์ •ํ•˜๋Š” if๋ฌธ์ด๋‹ค.
        # 1 ๊ณผ 0 index๋ฅผ ๋น„๊ต
        if len(N[j]) < len(N[min_index]) or (len(N[j]) == len(N[min_index]) and N[j] < N[min_index]):
            # N[j] < N[min_index] ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œํฌ๊ธฐ ๋น„๊ต
            min_index = j

    N[i], N[min_index] = N[min_index], N[i]  # ์„œ๋กœ switchํ•˜๋Š” ๊ตฌ๋ฌธ
    print(N)
 
# ์ค‘๋ณต ์ œ๊ฑฐ
unique_N = []
for item in N:
    if item not in unique_N:  # unique_N list์— ์š”์†Œ๊ฐ€ ์—†์„ ๋•Œ
        unique_N.append(item)

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
for item in unique_N:
    print(item)
 
['i', 'but', 'wont', 'hesitate', 'no', 'more', 'no', 'more', 'it', 'cannot', 'wait', 'im', 'yours']
['i', 'im', 'wont', 'hesitate', 'no', 'more', 'no', 'more', 'it', 'cannot', 'wait', 'but', 'yours']
['i', 'im', 'it', 'hesitate', 'no', 'more', 'no', 'more', 'wont', 'cannot', 'wait', 'but', 'yours']
['i', 'im', 'it', 'no', 'hesitate', 'more', 'no', 'more', 'wont', 'cannot', 'wait', 'but', 'yours']
['i', 'im', 'it', 'no', 'no', 'more', 'hesitate', 'more', 'wont', 'cannot', 'wait', 'but', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'hesitate', 'more', 'wont', 'cannot', 'wait', 'more', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'hesitate', 'wont', 'cannot', 'wait', 'more', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wont', 'cannot', 'wait', 'hesitate', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wait', 'cannot', 'wont', 'hesitate', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wait', 'wont', 'cannot', 'hesitate', 'yours']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wait', 'wont', 'yours', 'hesitate', 'cannot']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wait', 'wont', 'yours', 'cannot', 'hesitate']
['i', 'im', 'it', 'no', 'no', 'but', 'more', 'more', 'wait', 'wont', 'yours', 'cannot', 'hesitate']

์™„๋ฒฝํžˆ ๋‚˜์˜ ๋จธ๋ฆฌ์—์„œ ๋‚˜์˜จ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋‹ค. gpt์—๊ฒŒ ์–ด๋А ์ •๋„ ๋„์›€์„ ๋ฐ›์•˜๋‹ค. ์šฐ์„  ์„ ํƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ด์œ ๋Š” ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์—ฌ๊ธฐ์„œ min_index ์ฆ‰,์ž‘์€ index๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋น„๊ตํ•˜๊ณ  ์žˆ๋‹ค. ๋ช…ํ™•ํžˆ ๋จธ๋ฆฌ์—์„œ ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ง€์ง€๋Š” ์•Š์ง€๋งŒ for๋ฌธ์„ ๋Œ๋ฉด์„œ  switchํ•˜๋Š” ๊ตฌ๋ฌธ์—์„œ ๊ฐ’๋“ค์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

๋ฌธ์ž์—ด๋„ ์•ฝํ•˜์ง€๋งŒ ๊ฐœ์ธ์ ์œผ๋กœ for๋ฌธ๋„ ์•ฝํ•˜์ง€ ์•Š๋‚˜๋ผ๋Š” ์ƒ๊ฐ๋„ ์–ด๋ ดํ’‹์ด ์ƒ๊ฐํ•ด๋ณธ๋‹ค.

728x90