Headline: Determine whether it is an IP address

Headline: Determine whether it is an IP address

This question has a tricky part. It asks you to determine whether a string is an IP address, but does not specify whether it is IPv4 or IPv6. If you only consider IPv4 when writing the algorithm but ignore IPv6, the interviewer may ask you to write out the IPv6 solution process.

Before writing an algorithm, you need to understand the concepts of IPv4 and IPv6. If the interviewer does not tell you the definitions of these two, it may be to test your basic computer knowledge and the definition of IP addresses. Let's take a look at the definitions of IPv4 and IPv6 on LeetCode.

[[351741]]

IPv4

IPv4 addresses are represented by decimal numbers and points. Each address contains 4 decimal numbers ranging from 0 to 255, separated by ("."). For example, 172.16.254.1;

Also, numbers in IPv4 addresses do not start with 0. For example, the address 172.16.254.01 is illegal.

IPv6

An IPv6 address is represented by 8 groups of hexadecimal numbers, each group represents 16 bits. These groups of numbers are separated by (":"). For example:

2001:0db8:85a3:0000:0000:8a2e:0370:7334

is a valid address. Moreover, we can add some numbers starting with 0, and letters can be uppercase or lowercase. So,

2001:db8:85a3:0:0:8A2E:0370:7334

It is also a valid IPv6 address (ie, ignoring the leading 0 and ignoring case).

However, we cannot use an empty group just because the value of a group is 0, which will result in ::. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.

Code implementation:

Determine whether it is an IPv4 address. It is easy to make mistakes when using JS to solve this problem. Convert the string directly to a number and then determine whether it is between 0 and 255. However, parseInt('1e1', 10) is not the result you expect. For a detailed explanation, refer to Converting a string to an integer. I was taught a lesson. IPv4 problem-solving ideas:

The length of the split must be 4;

The value of each node can only be an integer between 0 and 255:

  1. const isIPv4 = ipStr = > {
  2. const nodes = ipStr .split('.');
  3. // Must be 4 digits
  4. if (nodes.length !== 4) {
  5. return false;
  6. }
  7. for(let i = 0 ; i <   nodes.length ; i++) {
  8. //Convert to an integer and then to a string to see if they are equal
  9. // This can filter out the case of 1e1, 00
  10. if (nodes[i] !== parseInt(nodes[i], 10).toString()) {
  11. return false;
  12. }
  13. // Check if it is in the range of 0-255
  14. if (+nodes[i] <   0 || +nodes[i] > 255) {
  15. return false
  16. }
  17. }
  18. return true;
  19. }

Determine whether it is an IPv6 address. Solution:

  • The length of the split must be 4=8;
  • The length of each node must be less than 4 and greater than 0;
  • Each character must be composed of 0-9, af, AF
  1. const isIPv6 = ipStr = > {
  2. const nodes = ipStr .split(':');
  3. // Must be 8 nodes
  4. if (nodes.length !== 8) {
  5. return false;
  6. }
  7. for(let i = 0 ; i <   nodes.length ; i++) {
  8. // The length cannot be greater than 4, nor can it be empty
  9. if (nodes[i].length > 4 || nodes[i] .length === 0) {
  10. return false;
  11. }
  12. // Iterate through the characters in the string
  13. for (const c of nodes[i]) {
  14. // The digital code corresponding to the character
  15. let value = c .charCodeAt(0);
  16. // 97-102 af
  17. // 65-70 AF
  18. // 48-57 0-9
  19. if (isNaN(value) || !(
  20. value > 96 && value <   103 ||
  21. value > 64 && value <   71 ||
  22. value > 47 && value <   58 )
  23. ) {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }

Key points

When implementing this problem with JavaScript, you need to pay attention to the "pitfall" when converting strings to integers. Characters can be compared based on the value of the character encoding.

<<:  “Double Eleven” flash sale strategy: Which is faster, 5G or Wi-Fi 6?

>>:  Hard-core dry goods: HTTP timeout, repeated requests must see the pitfalls and solutions

Blog    

Recommend

How many optical modules does a GPU need?

1. Network card model There are mainly two types ...

Interview Question Series: 12 Deadly Questions on Network

1. What is your understanding of the TCP/IP four-...

Aruba Again Named a Leader in Gartner Magic Quadrant for WAN Edge Infrastructure

September 22, 2021 – Aruba, a Hewlett Packard Ent...

my country has built the world's largest 4G network

[[181278]] On January 6, the Ministry of Science ...

Beware of walls! Can Wi-Fi become a paparazzi and also a holographic image?

In the wilderness, two nimble figures move left a...

Network security knowledge: View the user license agreement

It is very necessary to understand the user licen...

What is the difference between WiFi and Ethernet connections?

In today's networking world, Wifi and Etherne...