본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 90

반응형

Q>

An IP network is a set of routers that communicate routing information using a protocol.

(IP 네트워크는 프로토콜을 사용하여 라우팅 정보를 전달하는 라우터 세트입니다.)

A router is uniquely identified by an IP address. 

(라우터는 IP 주소로 고유하게 식별됩니다. )
In IPv4, an IP address consists of 32 bits, canonically represented as 4 decimal numbers of 8 bits each. The decimal numbers range from 0 (00000000) to 255 (11111111).

(IPv4에서 IP 주소는 정식으로 8 비트의 4 진수로 표현되는 32 비트로 구성됩니다. 십진수 범위는 0 (00000000)에서 255 (11111111)까지입니다. )
Each router has a "routing table" that contains a list of IP addresses, for the router to know where to send IP packets.

(각 라우터에는 라우터가 IP 패킷을 보낼 위치를 알 수 있도록 IP 주소 목록을 포함하는 "라우팅 테이블"이 있습니다.)

Route summarization in IP networks

(IP 네트워크에서 경로 요약)

As the network grows large (hundreds of routers), the number of IP addresses in the routing table increases rapidly.

(네트워크가 커지면 (수백 개의 라우터) 라우팅 테이블의 IP 주소 수가 빠르게 증가합니다.)

Maintaining a high number of IP addresses in the routing table would result in a loss of performances (memory, bandwidth and CPU resources limitation).

(라우팅 테이블에서 많은 수의 IP 주소를 유지하면 성능 손실 (메모리, 대역폭 및 CPU 리소스 제한)이 발생합니다. )
Route summarization, also called route aggregation, consists in reducing the number of routes by aggregating them into a "summary route".
(경로 집계라고도하는 경로 요약은 경로를 "요약 경로"로 집계하여 경로 수를 줄이는 데 있습니다. )
Let's consider the following example:

(다음 예제를 살펴 보겠습니다.)

We have 4 routers connected to A. A is aware about all 4 IP addresses, because it has a direct interface to each of them. However, A will not send them all to B.

(우리는 4 개의 라우터를 A에 연결했습니다. A는 4 개의 IP 주소 모두에 대해 직접 인터페이스를 가지고 있기 때문에 4 개의 IP 주소 모두를 알고 있습니다. 그러나 A는 B에게 모두 보내지 않을 것입니다. )
Instead, it will aggregate the addresses into a summary route, and send this new route to B. 

(대신 주소를 요약 경로로 집계하고이 새로운 경로를 B로 보냅니다.)


This implies that: 

(이는 다음을 의미합니다.)

  1. - Less bandwidth is used on the link between A and B.
  2. - B saves memory: it has only one route to store in its routing table
  3. - B saves CPU resources: there are less entries to consider when handling incoming IP packets

-보다 적은 대역폭이 A와 B 사이의 링크에 사용됩니다.

- B는 메모리를 저장합니다 : 라우팅 테이블에 저장할 경로는 하나뿐입니다

- B가 CPU 리소스를 절약합니다. 들어오는 IP 패킷을 처리 할 때 고려할 항목이 적습니다.

Computing a summary route

(요약 경로 계산)

A has all 4 addresses stored in its routing table. 

(A는 라우팅 테이블에 4 개의 주소를 모두 저장합니다.)

Address 1 172.16.12.0
Address 2 172.16.13.0
Address 3 172.16.14.0
Address 4 172.16.15.0


A will convert these IP addresses to binary format, align them and find the boundary line between the common prefix on the left (highlighted in red), and the remaining bits on the right. 
(A는이 IP 주소를 바이너리 형식으로 변환하고 정렬하여 왼쪽에있는 공통 접두어 (빨간색으로 강조 표시)와 오른쪽에있는 나머지 비트 사이의 경계선을 찾습니다.)

Address 1 10101100 00010000 00001100 00000000
Address 2 10101100 00010000 00001101 00000000
Address 3 10101100 00010000 00001110 00000000
Address 4 10101100 00010000 00001111 00000000


A creates a new IP address made of the common bits, and all other bits set to "0".

(A는 공통 비트로 구성되고 다른 모든 비트는 "0"으로 설정된 새 IP 주소를 작성합니다. )
This new IP address is converted back to decimal numbers.

(이 새 IP 주소는 다시 10 진수로 변환됩니다. )
Finally, A computes the number of common bits, also called "subnet".

(마지막으로 A는 "서브넷"이라고도하는 공통 비트 수를 계산합니다. )
The summary route is this new IP address, followed by a slash and the subnet: 172.16.12.0/22

(요약 경로는이 새 IP 주소이고 그 뒤에 슬래시와 서브넷이옵니다 : 172.16.12.0/22)

 

Input: A list of strings containing the IP addresses

        (IP 주소가 포함 된 문자열 목록)

 

Output: A string containing the summary route, represented as an IP address, followed by a slash and the subnet.

           (요약 경로가 포함 된 문자열로, IP 주소로 표시되며 그 뒤에 슬래시와 서브넷이옵니다.)

 

Example:

checkio(["172.16.12.0", "172.16.13.0", "172.16.14.0", "172.16.15.0"]) == "172.16.12.0/22"
checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9"]) == "172.0.0.0/8"
checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9", "146.11.2.2"]) == "128.0.0.0/2"

Preconditions: 
all(re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",d) for d in data))
all(-1 < int(n) < 256 for n in d.split(".") for d in data)
len(data) == len(set(data)) and len(data) > 1

 

A>

def checkio(data):
    #replace this for solution
    ip = [''.join('{0:08b}'.format(int(s)) for s in a.split('.')) for a in data]

    subnet = 32

    while len(set(i[:subnet] for i in ip)) > 1:
        subnet -= 1

    # ljust : 왼쪽 정렬
    route = ip[0][:subnet].ljust(32, '0')
    route = '.'.join(str(int(route[i:][:8], 2)) for i in range(0, 32, 8))

    return '{r}/{s}'.format(r=route, s=subnet)

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert (checkio(["172.16.12.0", "172.16.13.0", "172.16.14.0", "172.16.15.0"]) == "172.16.12.0/22"), "First Test"
    assert (checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9"]) == "172.0.0.0/8"), "Second Test"
    assert (checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9", "146.11.2.2"]) == "128.0.0.0/2"), "Third Test"

 

O>

 Your result: "172.16.0.0/19"

 

S>

https://py.checkio.org/

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 92  (0) 2019.06.22
Python Learn the basics Quiz 91  (0) 2019.06.21
Python Learn the basics Quiz 89  (0) 2019.06.21
Python Learn the basics Quiz 88  (0) 2019.06.21
Python Learn the basics Quiz 87  (0) 2019.06.21