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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
[3107] IPv6

๐Ÿ’› ๋ฌธ์ œ
IPv6์€ ๊ธธ์ด๊ฐ€ 128๋น„ํŠธ์ธ ์ฐจ์„ธ๋Œ€ ์ธํ„ฐ๋„ท ํ”„๋กœํ† ์ฝœ์ด๋‹ค.
IPv6์˜ ์ฃผ์†Œ๋Š” 32์ž๋ฆฌ์˜ 16์ง„์ˆ˜๋ฅผ 4์ž๋ฆฌ์”ฉ ๋Š์–ด ๋‚˜ํƒ€๋‚ธ๋‹ค.
์ด๋•Œ, ๊ฐ ๊ทธ๋ฃน์€ ์ฝœ๋ก  (:)์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
2001:0db8:85a3:0000:0000:8a2e:0370:7334
32์ž๋ฆฌ์˜ 16์ง„์ˆ˜๋Š” ์‚ฌ๋žŒ์ด ์ฝ๊ณ  ์“ฐ๊ธฐ์— ๋ถˆํŽธํ•˜๊ณ ,
๋Œ€๋ถ€๋ถ„์˜ ์ž๋ฆฌ๊ฐ€ 0์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ•์•ฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ ๊ทธ๋ฃน์˜ ์•ž์ž๋ฆฌ์˜ 0์˜ ์ „์ฒด ๋˜๋Š” ์ผ๋ถ€๋ฅผ ์ƒ๋žต ํ•  ์ˆ˜ ์žˆ๋‹ค.
์œ„์˜ IPv6์„ ์ถ•์•ฝํ•˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค
2001:db8:85a3:0:00:8a2e:370:7334

๋งŒ์•ฝ 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ๊ทธ๋ฃน์ด ์žˆ์„ ๊ฒฝ์šฐ ๊ทธ ์ค‘ ํ•œ ๊ฐœ ์ด์ƒ ์—ฐ์†๋œ ๊ทธ๋ฃน์„ ํ•˜๋‚˜ ๊ณจ๋ผ ์ฝœ๋ก  2๊ฐœ(::)๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.
2001:db8:85a3::8a2e:370:7334

2๋ฒˆ์งธ ๊ทœ์น™์€ ๋ชจํ˜ธํ•จ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์˜ค์ง ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
์˜ฌ๋ฐ”๋ฅธ ์ถ•์•ฝํ˜• IPv6์ฃผ์†Œ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,

์ด๋ฅผ ์›๋ž˜ IPv6 (32์ž๋ฆฌ์˜ 16์ง„์ˆ˜)๋กœ ๋ณต์›ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— ์˜ฌ๋ฐ”๋ฅธ IPv6 ์ฃผ์†Œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ฃผ์†Œ๋Š” ์ตœ๋Œ€ 39๊ธ€์ž์ด๋‹ค.
๋˜ํ•œ, ์ฃผ์†Œ๋Š” ์ˆซ์ž 0-9, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž a-f, ์ฝœ๋ก  :์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ์งธ ์ค„์—, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ IPv6์˜ ์ถ•์•ฝ๋˜์ง€ ์•Š์€ ํ˜•ํƒœ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
"""

IPv6 = input()
IPv6 = IPv6.split(':')

# # ['25', '09', '1985', 'aa', '091', '4846', '374', 'bb']
# print(IPv6)

temp = []

for i in range(len(IPv6)):
    if len(IPv6[i]) == 0:
        temp.append(i)
        continue

    if len(IPv6[i]) < 4:
        # 0 ์—†๋Š” ๋งŒํผ ์ฑ„์›Œ์คŒ
        IPv6[i] = "0" * (4 - len(IPv6[i])) + IPv6[i]
# print(IPv6)
# print(temp)

# 0์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง
if temp:
    for _ in temp:
        del IPv6[temp[0]]

    # IPv6์˜ ๊ธธ์ด๊ฐ€ 8์ด ๋  ๋•Œ๊นŒ์ง€
    while len(IPv6) != 8:
        IPv6.insert(temp[0], "0000")

print(":".join(IPv6))


3. ํ•ด์„ค

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