-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathass6_sol2.R
More file actions
38 lines (37 loc) · 998 Bytes
/
ass6_sol2.R
File metadata and controls
38 lines (37 loc) · 998 Bytes
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
wordlist_global<-unlist(read.table('wordlist.txt', header = FALSE, sep = "", dec = "."))
start_index<-1
length_wordlist<-0
iterations<-0
#wordlist_global<-c('a','b','c','d','e')
wordlist<-wordlist_global
word_search<-function(word,wordlist)
{
iterations<<-iterations+1
length_wordlist<<-length(wordlist)
half_length<-as.integer(length_wordlist/2)+1
word_half_length<-wordlist[half_length]
#print(paste(length_wordlist,start_index,end_index,wordlist_global[end_index]))
#if(length_wordlist==2){print(wordlist)}
if(word==word_half_length)
{
return(TRUE)
}
if(length_wordlist==1)
{
if(word==wordlist[1])
{
return(TRUE)
}
return(FALSE)
}else{
if(word<word_half_length){
word_search(word,wordlist[1:(half_length-1)])
}else{
start_index<<-start_index+half_length-1
word_search(word,wordlist[half_length:length(wordlist)])
}
}
}
word_search('agreement',wordlist_global)
iterations
start_index+as.integer(length_wordlist/2)