Chapter12. ๊ตฌํ
[๋ฌธ์ 53] 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ - Level2
์์ ์ ์ x์ ๋ํ ํจ์ f(x)๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํฉ๋๋ค.
- x๋ณด๋ค ํฌ๊ณ x์ ๋นํธ๊ฐ 1~2๊ฐ ๋ค๋ฅธ ์๋ค ์ค์์ ์ ์ผ ์์ ์
์๋ฅผ ๋ค์ด,
- f(2) = 3์
๋๋ค. ๋ค์ ํ์ ๊ฐ์ด 2๋ณด๋ค ํฐ ์๋ค ์ค์์ ๋นํธ๊ฐ ๋ค๋ฅธ ์ง์ ์ด 2๊ฐ ์ดํ์ด๋ฉด์ ์ ์ผ ์์ ์๊ฐ 3์ด๊ธฐ ๋๋ฌธ
์ | ๋นํธ | ๋ค๋ฅธ ๋นํธ์ ๊ฐ์ |
---|
2 | 000...0010 | - |
3 | 000...0011 | 1 |
- f(7) = 11์
๋๋ค. ๋ค์ ํ์ ๊ฐ์ด 7๋ณด๋ค ํฐ ์๋ค ์ค์์ ๋นํธ๊ฐ ๋ค๋ฅธ ์ง์ ์ด 2๊ฐ ์ดํ์ด๋ฉด์ ์ ์ผ ์์ ์๊ฐ 11์ด๊ธฐ ๋๋ฌธ
์ | ๋นํธ | ๋ค๋ฅธ ๋นํธ์ ๊ฐ์ |
---|
7 | 000...0111 | - |
8 | 000...1000 | 4 |
9 | 000...1001 | 3 |
10 | 000...1010 | 3 |
11 | 000...1011 | 2 |
์ ์๋ค์ด ๋ด๊ธด ๋ฐฐ์ด numbers๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค. numbers์ ๋ชจ๋ ์๋ค์ ๋ํ์ฌ ๊ฐ ์์ f ๊ฐ์ ๋ฐฐ์ด์ ์ฐจ๋ก๋๋ก ๋ด์ returnํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
[์ ํ์ฌํญ]
- 1 =< numbers์ ๊ธธ์ด =< 100,000
- 0 =< numbers์ ๋ชจ๋ ์ =< 1015
[์ฝ๋์์ฑ]
1. f(x) ํจ์ ๋ง๋ค๊ธฐ
def f(x):
if x % 2 == 0: return x + 1
x = f'0{bin(x)[:2]}'
x = f"{x[:x.rindex('0')]}10{x[x.rindex('0') + 2:]}"
return int(x, 2)
2. ๋จ์ด๋ฅผ ๋ฐ์ ํธ๋ผ์ด์ ๋ฐ์ดํฐ ์ฑ์ฐ๊ธฐ
def solution(numbers):
return [f(number) for number in numbers]
[์ ์ฒด์ฝ๋]
def f(x):
if x % 2 == 0: return x + 1
x = f'0{bin(x)[:2]}'
x = f"{x[:x.rindex('0')]}10{x[x.rindex('0') + 2:]}"
return int(x, 2)
def solution(numbers):
return [f(number) for number in numbers]