-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathep42.R
58 lines (53 loc) · 1.27 KB
/
ep42.R
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
pic <- matrix(round(runif(9,0,1),2), ncol = 3)
stfy <- function(s){
s <- gsub('\n',"",s)
s <- gsub('[[:punct:] ]+',' ',s)
s <- gsub('[[:digit:]]+', '', s)
s <- gsub(" ","",s)
s <- tolower(s)
return(s)
}
rstr <- function(n){
a <- c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
futstr <- vector(mode = "numeric", length = n)
for (i in 1:n){
k <- runif(1,1,26)
futstr[i] <- a[k]
}
r <- paste(futstr, sep = "", collapse = "")
return(r)
}
rin <- function(n,k){
fn = "https://raw.githubusercontent.com/aparrish/rwet-examples/master/austen.txt"
ws <- url(fn,open = "rb")
l <- readChar(ws,nchars = 1e6)
l <- l[[1]][[1]][1]
l <- stfy(l)
rs <- vector(mode="numeric", length=k)
jas <- vector(mode="numeric", length=k)
for (i in 1:k){
r <- runif(1,1, nchar(l)-(2*n+1))
la <- substr(l,r+1,r+n)
lb <- substr(l, r+n+1 ,r+(2*n))
if(k <5){
print(la)
print(lb)
}
a1s <- strsplit(la,"")
b1s <- strsplit(lb,"")
ja <- sum(a1s[[1]] == b1s[[1]])
jas[i] <- ja
c1 <- rstr(n)
c2 <- rstr(n)
if(k < 5){
print(c1)
print(c2)
}
c1s <- strsplit(c1,"")
c2s <- strsplit(c2,"")
ran <- sum(c1s[[1]] == c2s[[1]])
rs[i] <- ran
}
return(c(mean(jas),mean(rs)))
}
rin(100,1000)