Skip to content

Commit b3d2add

Browse files
authored
Create s.r
Signed-off-by: MolfarUA <52976230+MolfarUA@users.noreply.github.com>
1 parent d1c8e41 commit b3d2add

File tree

1 file changed

+23
-0
lines changed
  • 5 kyu/Count IP Addresses

1 file changed

+23
-0
lines changed

5 kyu/Count IP Addresses/s.r

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ips_between <- function(start, end) {
2+
sum(Reduce(function(a, b) as.integer(a) - as.integer(b),
3+
strsplit(c(end, start), "\\.")) * 256^(3:0))
4+
}
5+
____________
6+
ips_between <- function(start, end) {
7+
s <- as.integer(unlist(strsplit(start, ".", fixed = TRUE)))
8+
e <- as.integer(unlist(strsplit(end, ".", fixed = TRUE)))
9+
difference <- rev(e-s)
10+
sum(sapply(seq_along(difference), function(i){
11+
difference[i]*256^(i-1)
12+
}))
13+
14+
}
15+
__________________
16+
ips_between <- function(start, end) {
17+
toBase256 <- function(ip) {
18+
ip <- as.numeric(strsplit(ip,"\\.")[[1]])
19+
sum(mapply(`*`, ip, 256^(3:0)))
20+
}
21+
22+
toBase256(end) - toBase256(start)
23+
}

0 commit comments

Comments
 (0)