Skip to content

Instantly share code, notes, and snippets.

@jacquesattack
Last active August 29, 2015 14:18
Show Gist options
  • Save jacquesattack/b4995b4fb7fd8cbea60b to your computer and use it in GitHub Desktop.
Save jacquesattack/b4995b4fb7fd8cbea60b to your computer and use it in GitHub Desktop.
Generate sequences from fibmorse substitution
gen.fibmorse = function(start = c(0), depth = 1){
# check inputs
if(class(start) != "numeric"){
print("Input not a numeric vector!")
stop();
}
for(val in start){
if(!(val %in% c(0,1)) ){
print(paste("Encountered non 0/1 value in vector:",val))
stop()
}
}
if(class(depth) != "numeric"){
print("'depth' must be numeric")
stop()
}
if(depth < 1){
print("'depth' must be positive")
stop()
}
# main loop
for(i in 1:depth){
new.vector = c()
print(paste(start,collapse="",sep=""))
for(val in start){
if(val == 0){
new.vector = c(new.vector,0,1)
}
if(val == 1){
r = runif(1)
if(r >= 0.5){
new.vector = c(new.vector,0)
} else {
new.vector = c(new.vector,1,0)
}
}
}
start = new.vector
}
new.vector = paste(new.vector,collapse="",sep="")
return(new.vector)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment