๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test Practice

์ด์ง„ ๋ณ€ํ™˜ ๋ฐ˜๋ณตํ•˜๊ธฐ

by Whiimsy 2024. 4. 8.

 

 

๐Ÿ’ก ์ƒ๊ฐ

๋ฌธ์ž์—ด์—์„œ 0 ์ œ๊ฑฐํ•˜๋ฉด์„œ count๋กœ ๊ฐœ์ˆ˜ ์„ธ๋†“๊ธฐ
์ „์ฒด ์ด์ง„๋ณ€ํ™˜ํ•œ ํšŸ์ˆ˜๋„ ์„ธ๊ธฐ
๊ธธ์ด c๋ฅผ 2์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ๋ฌธ์ž์—ด
2์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ bin์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด 0b... ํ˜•ํƒœ๋กœ ๋‚˜์˜ค๋ฏ€๋กœ [2:] ์š”๊ฑธ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ์ˆซ์ž ๋ถ€๋ถ„๋งŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

 

๐Ÿ“– ๋‚ด ์ฝ”๋“œ

def solution(s):
    total_count = 0
    zero_count = 0

    while s != "1":
        total_count += 1
        zero_count += s.count("0")
        s = str(bin(len(s.replace("0",""))))[2:]

    return total_count, zero_count

 

๐Ÿ“‘ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

def solution(s):
    a, b = 0, 0
    while s != '1':
        a += 1
        num = s.count('1')
        b += len(s) - num
        s = bin(num)[2:]
    return [a, b]

 

์ด๋ฒˆ ๊ฑด ๋ฌด๋‚œํ•˜๊ฒŒ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™๋‹ค

'Coding Test Practice' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ  (1) 2024.04.08
์ตœ์†Ÿ๊ฐ’ ๋งŒ๋“ค๊ธฐ  (0) 2024.04.08
์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ  (1) 2024.04.08
์ˆซ์ž์˜ ํ‘œํ˜„  (0) 2024.04.08
ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜  (0) 2024.04.08