-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisIPv4Address.R
More file actions
executable file
·84 lines (81 loc) · 2.82 KB
/
isIPv4Address.R
File metadata and controls
executable file
·84 lines (81 loc) · 2.82 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address.
#
# Given a string, find out if it satisfies the IPv4 address naming rules.
#
# Example
#
# For inputString = "172.16.254.1", the output should be
# isIPv4Address(inputString) = true;
#
# For inputString = "172.316.254.1", the output should be
# isIPv4Address(inputString) = false.
#
# 316 is not in range [0, 255].
#
# For inputString = ".254.255.0", the output should be
# isIPv4Address(inputString) = false.
#
# There is no first number.
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] string inputString
#
# A string consisting of digits, full stops and lowercase English letters.
#
# Guaranteed constraints:
# 1 ≤ inputString.length ≤ 30.
#
# [output] boolean
# true if inputString satisfies the IPv4 address naming rules, false otherwise.
#special caution: double zeros not allowed. only single zero.
library(stringr)
library(data.table)
isIPv4Address <- function(inputString) {
#get all data between dots
dotpos <- as.data.table(str_locate_all(inputString,"\\.")[[1]])
dotpos[,end := NULL]
# initial <- inputString
# inputString <- strsplit(inputString,"")[[1]]
if (nrow(dotpos) == 3) {
firstdigit <- substr(inputString,1,dotpos[1,start-1])
if ((is.na(as.numeric(firstdigit))) | (nchar(firstdigit) > nchar(as.numeric(firstdigit)))) {
return(FALSE)
}
firstdigit <- as.numeric(firstdigit)
if (is.na(as.numeric(firstdigit)) | (firstdigit < 0) | (firstdigit > 255)) {
return(FALSE)
}
seconddigit <- substr(inputString,dotpos[1,start+1],dotpos[2,start-1])
if ((is.na(as.numeric(seconddigit))) | (nchar(seconddigit) > nchar(as.numeric(seconddigit)))) {
return(FALSE)
}
seconddigit <- as.numeric(seconddigit)
if (is.na(seconddigit) | (seconddigit < 0) | (seconddigit > 255)) {
return(FALSE)
}
thirddigit <- substr(inputString,dotpos[2,start+1],dotpos[3,start-1])
if ((is.na(as.numeric(thirddigit))) | (nchar(thirddigit) > nchar(as.numeric(thirddigit)))) {
return(FALSE)
}
thirddigit <- as.numeric(thirddigit)
if (is.na(thirddigit) | (thirddigit < 0) | (thirddigit > 255)) {
return(FALSE)
}
fourthdigit <- substr(inputString,dotpos[3,start+1],nchar(inputString))
if ((is.na(as.numeric(fourthdigit))) | (nchar(fourthdigit) > nchar(as.numeric(fourthdigit)))) {
return(FALSE)
}
fourthdigit <- as.numeric(fourthdigit)
if (is.na(fourthdigit) | (fourthdigit < 0) | (fourthdigit > 255)) {
return(FALSE)
} else {
return(TRUE)
}
} else {
return(FALSE)
}
}
#not the most elegant code.