LED is my new Hello World – R Time

(This article was first published on Blag's bag of rants, and kindly contributed to R-bloggers)

Yep…as I have said many times before, the LED Number application has become my new Hello World…whenever I learn a new programming language I try to build this, because it compromises several language constructs and makes you learn more by trying to figure out how to build it again…

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.

To leave a comment for the author, please follow the link and comment on their blog: Blag's bag of rants.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)