Post

๐Ÿน IPv6

1. ๋ฌธ์ œ ๋งํฌ

3107๋ฒˆ: IPv6


2. ์ฝ”๋“œ

Python3 31120KB 40ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
split_ip = input().split(':')

# ๊ทœ์น™ 1
for i in range(len(split_ip)):
    # ::์œผ๋กœ ์ถ•์•ฝ๋œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๊ณ , ์ถ•์•ฝ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ
    if split_ip[i] != '' and len(split_ip[i]) != 4:
        # 0์„ ์ถ”๊ฐ€ํ•ด ์›์ƒํƒœ๋กœ ๋ณต๊ตฌ
        split_ip[i] = '0' * (4 - len(split_ip[i])) + split_ip[i]

# ๊ทœ์น™ 2(ํ•ด๋‹นํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ)
# 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ํ˜•ํƒœ๊ฐ€ ::์œผ๋กœ ์ถ•์•ฝ๋œ ๊ฒฝ์šฐ ์žˆ๋Š”์ง€ ํ™•์ธ
num = 0 # ์ถ•์•ฝ๋˜์ง€ ์•Š์€ ๋ถ€๋ถ„์˜ ๊ฐœ์ˆ˜
if '' in split_ip:
    for i in range(len(split_ip)):
        if split_ip[i] != '':
            num += 1
        else:
            idx = i # ๊ทœ์น™2๊ฐ€ ์‚ฌ์šฉ๋œ ์œ„์น˜
    split_ip[idx] = '0000' * (8 - num)
    
ip = ''.join(split_ip)
answer = ''
for i in range(0, len(ip), 4):
    answer += ip[i:i+4]
    # ๋งˆ์ง€๋ง‰์—๋งŒ : ์•ˆ ๋ถ™์ž„
    if i != 28:
        answer += ':'
print(answer)


3. ํ•ด์„ค

๊ทœ์น™1์€ ๋ชจ๋‘ ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ์ด๊ณ , ๊ทœ์น™ 2๋Š” 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน์ด 1๊ฐœ์ด์ƒ ์žˆ์„ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ๊ทœ์น™ 1์€ ๋ชจ๋‘์—๊ฒŒ ์ ์šฉํ•ด์ค€ ๋’ค, ๊ทœ์น™ 2๋Š” ํ•ด๋‹น๋˜๋Š” ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰์— ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

:์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์„ ๋‚˜๋ˆˆ ํ›„ ๋นˆ ์นธ์ด ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ทœ์น™ 2์˜ ๊ฒฝ์šฐ์ด๋‹ค.

๋นˆ์นธ์ด ์•„๋‹Œ ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ  8์—์„œ ๋นผ์ฃผ๋ฉด 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ ๋นˆ ์นธ ์œ„์น˜์— โ€˜0000โ€™ ๊ทธ๋ฃน์„ ๋„ฃ์–ด์ค€๋‹ค.

๋‚˜๋ˆ ๋†“์€ ๊ฐ’์„ ๋ชจ๋‘ ํ•ฉ์ณ์ค€ ํ›„ 4์นธ๋งˆ๋‹ค :์„ ๋„ฃ์–ด์ค€๋‹ค. (์ด๋•Œ ๋งˆ์ง€๋ง‰์—๋Š” ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.)

This post is licensed under CC BY 4.0 by the author.