Skip to content

Instantly share code, notes, and snippets.

@dggoldst
Created January 19, 2012 16:25
Show Gist options
  • Save dggoldst/1640988 to your computer and use it in GitHub Desktop.
Save dggoldst/1640988 to your computer and use it in GitHub Desktop.
mstr = '123456789' #Enter string to translate here
wordNum <- read.delim("~/bin/memory/wordNum.txt",
colClasses=c("character","character"),header=F)
dict<-new.env()
buildDict=function(word,num){
dict[[num]]<- paste(word,dict[[num]],sep=" ")
}
foo=mapply(buildDict,wordNum$V1,wordNum$V2)
rec <- function(x, y) {
if (is.null(x)) return;
result=dict[[x]]
if (length(result>0)) {
print(x)
print(result)
}
else if(y == 1) {
p1 = substr(x,0,nchar(x) / 2)
p2 = substr(x,(nchar(x) / 2)+1,nchar(x))
rec(p1, y)
rec(p2, y)
} else {
p1 = substr(x,0,(nchar(x) / 2+1))
p2 = substr(x,(nchar(x) / 2)+2,nchar(x))
rec(p1, y)
rec(p2, y)
}
}
print('-----------------------')
rec(mstr, 1)
print('-----------------------')
rec(mstr, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment