๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Programmers] [์—ฐ์Šต๋ฌธ์ œ] [Python] Level1_๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ

Dbswnstjd 2022. 4. 5. 09:32

https://programmers.co.kr/learn/courses/30/lessons/12903?language=python3 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ

๋‹จ์–ด s์˜ ๊ฐ€์šด๋ฐ ๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด ๊ฐ€์šด๋ฐ ๋‘๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์žฌํ•œ์‚ฌํ•ญ s๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ, 100์ดํ•˜์ธ ์ŠคํŠธ๋ง์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ s ret

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ - ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ
def solution(s):
    answer = ''
    length = len(s)
    if length % 2 == 0:
        answer = s[length//2-1:length//2+1]
    else:
        answer = s[length // 2]
    return answer