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
import sys

input = sys.stdin.readline
address = input().rstrip()
origin = [[] for _ in range(8)]
sep_cnt = address.count(':')
idx = 0

for i in range(len(address)):
    if address[i] == ':':
        if i > 0 and address[i - 1] == ':': # ์—ฐ์† ':' ๋“ฑ์žฅ ์‹œ
            while idx + sep_cnt < 8:
                origin[idx] = ['0', '0', '0', '0'] # ์•ž์„ '0'์œผ๋กœ ์ฑ„์›Œ์คŒ
                idx += 1
        else:
            while len(origin[idx]) < 4:
                origin[idx].insert(0, '0') # ์•ž์„ '0'์œผ๋กœ ์ฑ„์›Œ์คŒ
            idx += 1
        sep_cnt -= 1
    else:
        origin[idx].append(address[i])
while len(origin[-1]) < 4:
    origin[-1].insert(0, '0') # ์•ž์„ '0'์œผ๋กœ ์ฑ„์›Œ์คŒ

[print(''.join(origin[i]), ':', sep='', end='') for i in range(7)]
print(''.join(origin[-1]))



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