Yesterday I blog about how to build it using Haskell...and...I'm still learning Haskell...so I thought..."It's cool that I'm using this for my new programming languages...but what about the old ones?"
I suddenly realized that I had never wrote this using R...and that's really sad...I love R -:)
Anyway...here it goes -;)
LED.R |
---|
line<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3) index<-c(0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9) map<-c(" _ ","| | ","|_| "," ","| ","| "," _ "," _| ","|_ ", " _","_| ","_| "," ","|_| "," | "," _ ","|_ "," _| ", " _ ","|_ ","|_| "," _ "," | "," | "," _ ","|_| ","|_| ", " _ ","|_| "," _| ") Lines<-cbind(line,index,map) DF_Lines<-data.frame(Lines,stringsAsFactors=F) line1<-"" line2<-"" line3<-"" p_number<-readline("Enter a number?: ") l_number<-strsplit(p_number,"") for(i in 1:nchar(p_number)){ num_map<-DF_Lines[DF_Lines$index == as.numeric(l_number[[1]][i]),] line1<-paste(line1,num_map$map[1]) line2<-paste(line2,num_map$map[2]) line3<-paste(line3,num_map$map[3]) } lines = paste(line1,"\n",line2,"\n",line3,"\n") cat(lines) |
Check the picture -;)
Greetings,
Blag.
Development Culture.
8 comentarios:
Looks like it should work but I get the following when I copy your code and run verbatim:
cat(lines)
n |_| |_| n | | n
Is there some setting in RStudio that I should use?
William:
Not at all...should work fine on either RStudio or Command Line R...in your case it looks like "\n" is being replaced by a single "n"...if you're using Linux I guess you might need to change the code to this...
lines = paste(line1,"\\n",line2,"\\n",line3,"\\n")
Will try it myself as soon as I can...
Greetings,
Blag.
Development Culture.
Try
map <- list(
c(" _ ", " ", " _ ", " _ ", " ", " _ ", " _ ", " _ ", " _ ", " _ "),
c("| |", " |", " _|", " _|", "|_|", "|_ ", "|_ ", " |", "|_|", "|_|"),
c("|_|", " |", "|_ ", " _|", " |", " _|", "|_|", " |", "|_|", " _|"))
led <- function(numstr) {
digits <- as.numeric(strsplit(numstr,"")[[1]])
cat(sapply(map,function(m) paste(m[digits+1],collapse=" ")),sep="\n")
}
led(readline("Enter a number?: "))
Hey there, I findyour LED generating script very interesting. But on the second glance i saw that you missed some very useful R functions. further you should use a second for loop. this would improve the generalisation to more lines.
Best
Matthias
---------------------
line<- rep(1:9, times = 10)
index<-rep(0:9, each = 3)
map<-c(" _ ","| | ","|_| "," ","| ","| "," _ "," _| ","|_ ",
" _","_| ","_| "," ","|_| "," | "," _ ","|_ "," _| ",
" _ ","|_ ","|_| "," _ "," | "," | "," _ ","|_| ","|_| ",
" _ ","|_| "," _| ")
DF_Lines<-data.frame(line = line, index = index, map = map)
lines <- character(3)
p_number<-readline("Enter a number?: ")
l_number<-strsplit(p_number,"")
for(i in 1:nchar(p_number)){
num_map<-subset(DF_Lines, subset = index == as.numeric(l_number[[1]][i]))
for(j in 1:3){
lines[j]<-paste(lines[j],num_map$map[j])
}
}
cat(paste(lines, collapse= "\n "))
Thanks Simon! That's awesome and really short -:) Really...I didn't thought on using Map and SApply...great tip!
Greetings,
Blag.
Development Culture.
Matthias:
Thanks -:) I don't use R professionally or very often...so surely I will miss some nice functions...I thought about using another for but for's are not very good in R...so I just leave it like that...
Greetings,
Blag.
Development Culture.
Yet another version...
printLED <- function(n) {
txt <- c(" ", " |", " _ ", " _|", "| ", "| |", "|_ ", "|_|")
Layout <- matrix( c(3,6,8,1,2,2,3,4,7,3,4,4,1,8,2,3,7,4,3,7,8,3,2,2,3,8,8,3,8,2), nrow=3 )
p_number <- formatC(n)
digs <- as.integer(strsplit(p_number,"")[[1]])
out <- Layout[ , digs+1 ]
lines <- sapply(1:3
,function(i){paste(txt[out[i,]], collapse=" ")})
cat(paste(lines,collapse="\n"),"\n")
invisible(n)
}
printLED(1977)
Yet another version...
It's not very well aligned...but other than that...nice code...thanks -:)
Greetings,
Blag.
Development Culture.
Publicar un comentario