Python 2

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ ์˜ ์œ„์น˜ ๊ตฌํ•˜๊ธฐ

์•„๋ž˜๋Š” ๋‚ด ์ฝ”๋“œ. ๋‚ด ์ฝ”๋“œ์˜ ์žฅ์ ์€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ํ•œ ๋ˆˆ์— ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฐ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ์น˜๋งŒ ๊ธฐ๋ณธ์— ์ถฉ์‹คํ•œ ์ฝ”๋“œ๊ธฐ์— ๊ทธ๋Ÿฐ๊ฑฐ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ธธ๊ณ , ์‹ ๋ฐ•ํ•จ์ด ๋–จ์–ด์ง„๋‹ค๋Š” ๋‹จ์ , ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. def solution(dot): if dot[0]>0 and dot[1]>0: return 1 elif dot[0]0: return 2 elif dot[0] 0] 0๊ณผ 1๋กœ ๊ฒฐ๊ณผ ๊ฐ’์ด ์ •ํ•ด์ง€๋Š” ๊ฒฝ์šฐ boolean์„ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด key point ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. Boolean์„ ์•ž์œผ๋กœ ์ฝ”๋“œ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ๊ฒ ๋‹ค.

Python/Programmers 2024.01.30

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์ˆ˜ ํ™€์ˆ˜ ๊ฐœ์ˆ˜

์ด๊ฒŒ ๋‚ด ์ฝ”๋“œ. def solution(num_list): even = 0 odd = 0 for i in num_list: if i%2==0: even+=1 else: odd+=1 return [even, odd] ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ. ์ด๊ฑฐ ๋ณด๊ณ  ๊ฒฝ์•…ํ•ด์„œ ๋ธ”๋กœ๊ทธ ์“ธ ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค. def solution(num_list): answer = [0,0] for n in num_list: answer[n%2]+=1 return answer ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์‹ฑ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Œ.. n%2๋ฅผ ํ–ˆ์„๋•Œ ์ง์ˆ˜์ด๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋‹ˆ answer[0]์— 1์„ ์ถ”๊ฐ€ํ•˜๊ณ , ํ™€์ˆ˜์ด๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ 1์ด๋‹ˆ answer[1]์— 1์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์ž„. ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ๊น”๋”ํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ฏผํ•˜๋Š” ๊ณผ์ •์ด..

Python/Programmers 2024.01.30