[๐Ÿฅ‡ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”ํ…Œ ์—ฐ์Šต] Lv.0 ์ค‘์•™๊ฐ’ ๊ตฌํ•˜๊ธฐ- sor()์™€ sorted() ์ฐจ์ด

์•ˆ์ง€์ˆ˜ยท2023๋…„ 2์›” 18์ผ
0
post-custom-banner

๐Ÿ“— ๋ฌธ์ œ ์„ค๋ช…:

๐ŸŽ ๋‚˜์˜ ์ฝ”๋“œ์™€ ํ’€์ด:

def solution(array):
    array.sort()
    return array[len(array)//2]

-> array๋ฅผ sortํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•œ ํ›„, ๊ธธ์ด๊ฐ’์˜ ์ค‘๊ฐ„ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โญ• ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด:


-> sortedํ•จ์ˆ˜์™€ sortํ•จ์ˆ˜์˜ ์ฐจ์ด ์•Œ๊ธฐ!!

๐Ÿ‘ What I learned

  1. array.sort(): ๋ฐฐ์—ด ์ž์ฒด๋ฅผ ์ •๋ ฌ, none๊ฐ’์„ ๋ฆฌํ„ด! ๊ทธ ์ž์ฒด๋กœ ์ •๋ ฌ

  2. array2 = sorted(array): ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•ด์„œ ์ •๋ ฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜ (์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ), ์ •๋ ฌํ•ด์„œ ๋ฆฌํ„ด์‹œ์ผœ์คŒ

some_list = [5, 7, 2, 3, 1]

print(sorted(some_list))
print(some_list.sort())
>> [1,2,3,5,7]
>> None
profile
์ง€์ˆ˜์˜ ์ทจ์ค€, ๊ฐœ๋ฐœ์ผ๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€