Created
January 19, 2012 16:25
-
-
Save dggoldst/1640988 to your computer and use it in GitHub Desktop.
R version of find.py, per http://www.decisionsciencenews.com/2012/01/17/some-code-to-help-you-remember-numbers/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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