SlideShare uma empresa Scribd logo
1 de 51
Baixar para ler offline
> fizzbuzz <- function(n) {
     # Write your code here
  }

> fizzbuzz(20)
 [1] "1"         "2"          "Fizz"
 [4] "4"         "Buzz"       "Fizz"
 [7] "7"         "8"          "Fizz"
[10] "Buzz"      "11"         "Fizz"
[13] "13"        "14"         "FizzBuzz"
[16] "16"        "17"         "Fizz"
[19] "19"        "Buzz"
fb1 <- function(n) {
   fb <- character(n)
   for (i in 1:n) {
      if (i %% 3 == 0 && i %% 5 == 0)
         fb[i] <- "FizzBuzz"
      else if (i %% 3 == 0)
         fb[i] <- "Fizz"
      else if (i %% 5 == 0)
         fb[i] <- "Buzz"
      else
         fb[i] <- i
   }
   fb
}
fb2 <- function(n) {
   sapply(1:n, function(i) {
      if (i %% 3 == 0 && i %% 5 == 0)
         "FizzBuzz"
      else if (i %% 3 == 0)
         "Fizz"
      else if (i %% 5 == 0)
         "Buzz"
      else
         i
   })
}
fb3 <- function(n) {
   isFizz <- rep(c(F,F,T), length=n)
   isBuzz <- rep(c(F,F,F,F,T), length=n)
   isNumber <- !isFizz & !isBuzz

    fizz <- ifelse(isFizz, "Fizz", "")
    buzz <- ifelse(isBuzz, "Buzz", "")
    number <- ifelse(isNumber, 1:n, "")

    paste(fizz, buzz, number, sep="")
}
> N <- 100
> benchmark(fb1(N), fb2(N), fb3(N),
            order="relative",
            replications=10000)

    test elapsed relative
3 fb3(N)   4.172 1.000000
2 fb2(N)   6.669 1.598514
1 fb1(N)   7.779 1.864573
> N <- 10000
> benchmark(fb1(N), fb2(N), fb3(N),
             order="relative",
             replications=100)

    test elapsed relative
3 fb3(N)   2.907 1.000000
2 fb2(N)   7.823 2.691090
1 fb1(N)   8.200 2.820777
> N <- 1000000
> benchmark(fb1(N), fb2(N), fb3(N),
            order="relative",
            replications=5)

    test elapsed relative
3 fb3(N) 28.287 1.000000
1 fb1(N) 46.198 1.633188
2 fb2(N) 52.223 1.846184
library(compiler)      # Installed by default
library(inline)        # Needs manual installation


fbcpp <- cxxfunction(
   signature(ns="integer"),
   body=src, # Shown in the next slide
   plugin="Rcpp")

fb1.c <- cmpfun(fb1)
fb2.c <- cmpfun(fb2)
fb3.c <- cmpfun(fb3)
fbcpp.c <- cmpfun(fbcpp)
int n = Rcpp::as<int>(ns);
Rcpp::CharacterVector fb(n);
for (int index = 0; index < n; index++) {
   int i = index + 1;
   if (i % 3 == 0 && i % 5 == 0)
      fb[index] = "FizzBuzz";
   else if (i % 3 == 0)
      fb[index] = "Fizz";
   else if (i % 5 == 0)
      fb[index] = "Buzz";
   else {
      char s[11]; // Integer is 10 or less digit
      sprintf(s, "%d", i);
      fb[index] = s;
   }
}
return(fb);
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題

Mais conteúdo relacionado

Mais procurados

OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
Sentiment Analysis with Azure Machine Learning
Sentiment Analysis with Azure Machine LearningSentiment Analysis with Azure Machine Learning
Sentiment Analysis with Azure Machine LearningStefano Tempesta
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
Slides do Treinamento - OWASP TOP 10 (Em português)
Slides do Treinamento - OWASP TOP 10 (Em português)Slides do Treinamento - OWASP TOP 10 (Em português)
Slides do Treinamento - OWASP TOP 10 (Em português)Julio Cesar Stefanutto
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...Edureka!
 
What Is Kali Nethunter?
What Is Kali Nethunter?What Is Kali Nethunter?
What Is Kali Nethunter?Simplilearn
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arraysHassan Dar
 
Developing a Threat Modeling Mindset
Developing a Threat Modeling MindsetDeveloping a Threat Modeling Mindset
Developing a Threat Modeling MindsetRobert Hurlbut
 
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015CODE BLUE
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCTomohiro Kumagai
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Edureka!
 
durga python full.pdf
durga python full.pdfdurga python full.pdf
durga python full.pdfssuser476810
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | EdurekaWhat Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | EdurekaEdureka!
 

Mais procurados (20)

OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
Padrão Command
Padrão CommandPadrão Command
Padrão Command
 
Sentiment Analysis with Azure Machine Learning
Sentiment Analysis with Azure Machine LearningSentiment Analysis with Azure Machine Learning
Sentiment Analysis with Azure Machine Learning
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
Java Security Framework's
Java Security Framework'sJava Security Framework's
Java Security Framework's
 
Slides do Treinamento - OWASP TOP 10 (Em português)
Slides do Treinamento - OWASP TOP 10 (Em português)Slides do Treinamento - OWASP TOP 10 (Em português)
Slides do Treinamento - OWASP TOP 10 (Em português)
 
Java method
Java methodJava method
Java method
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
What Is Kali Nethunter?
What Is Kali Nethunter?What Is Kali Nethunter?
What Is Kali Nethunter?
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Developing a Threat Modeling Mindset
Developing a Threat Modeling MindsetDeveloping a Threat Modeling Mindset
Developing a Threat Modeling Mindset
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
XSS Attacks Exploiting XSS Filter by Masato Kinugawa - CODE BLUE 2015
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
 
durga python full.pdf
durga python full.pdfdurga python full.pdf
durga python full.pdf
 
Java interface
Java interfaceJava interface
Java interface
 
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | EdurekaWhat Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
 
Working with JSON
Working with JSONWorking with JSON
Working with JSON
 

Semelhante a R で解く FizzBuzz 問題

ATS language overview
ATS language overviewATS language overview
ATS language overviewKiwamu Okabe
 
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programmingChiwon Song
 
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM LanguagesCorneil du Plessis
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Esehara Shigeo
 
7 Python udf.pptx
7 Python udf.pptx7 Python udf.pptx
7 Python udf.pptxSUJALORAON
 
Fourier project presentation
Fourier project  presentationFourier project  presentation
Fourier project presentation志璿 楊
 
FUNctional programming in Python
FUNctional programming in PythonFUNctional programming in Python
FUNctional programming in Pythonknifeofdreams
 

Semelhante a R で解く FizzBuzz 問題 (10)

ATS language overview
ATS language overviewATS language overview
ATS language overview
 
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programming
 
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM Languages
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.
 
7 Python udf.pptx
7 Python udf.pptx7 Python udf.pptx
7 Python udf.pptx
 
Basics
BasicsBasics
Basics
 
Get Kata
Get KataGet Kata
Get Kata
 
Fourier project presentation
Fourier project  presentationFourier project  presentation
Fourier project presentation
 
Where do Rubyists go?
 Where do Rubyists go?  Where do Rubyists go?
Where do Rubyists go?
 
FUNctional programming in Python
FUNctional programming in PythonFUNctional programming in Python
FUNctional programming in Python
 

R で解く FizzBuzz 問題

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. > fizzbuzz <- function(n) { # Write your code here } > fizzbuzz(20) [1] "1" "2" "Fizz" [4] "4" "Buzz" "Fizz" [7] "7" "8" "Fizz" [10] "Buzz" "11" "Fizz" [13] "13" "14" "FizzBuzz" [16] "16" "17" "Fizz" [19] "19" "Buzz"
  • 27.
  • 28. fb1 <- function(n) { fb <- character(n) for (i in 1:n) { if (i %% 3 == 0 && i %% 5 == 0) fb[i] <- "FizzBuzz" else if (i %% 3 == 0) fb[i] <- "Fizz" else if (i %% 5 == 0) fb[i] <- "Buzz" else fb[i] <- i } fb }
  • 29.
  • 30. fb2 <- function(n) { sapply(1:n, function(i) { if (i %% 3 == 0 && i %% 5 == 0) "FizzBuzz" else if (i %% 3 == 0) "Fizz" else if (i %% 5 == 0) "Buzz" else i }) }
  • 31.
  • 32.
  • 33.
  • 34.
  • 35. fb3 <- function(n) { isFizz <- rep(c(F,F,T), length=n) isBuzz <- rep(c(F,F,F,F,T), length=n) isNumber <- !isFizz & !isBuzz fizz <- ifelse(isFizz, "Fizz", "") buzz <- ifelse(isBuzz, "Buzz", "") number <- ifelse(isNumber, 1:n, "") paste(fizz, buzz, number, sep="") }
  • 36.
  • 37.
  • 38.
  • 39.
  • 40. > N <- 100 > benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=10000) test elapsed relative 3 fb3(N) 4.172 1.000000 2 fb2(N) 6.669 1.598514 1 fb1(N) 7.779 1.864573
  • 41. > N <- 10000 > benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=100) test elapsed relative 3 fb3(N) 2.907 1.000000 2 fb2(N) 7.823 2.691090 1 fb1(N) 8.200 2.820777
  • 42. > N <- 1000000 > benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=5) test elapsed relative 3 fb3(N) 28.287 1.000000 1 fb1(N) 46.198 1.633188 2 fb2(N) 52.223 1.846184
  • 43.
  • 44.
  • 45.
  • 46.
  • 47. library(compiler) # Installed by default library(inline) # Needs manual installation fbcpp <- cxxfunction( signature(ns="integer"), body=src, # Shown in the next slide plugin="Rcpp") fb1.c <- cmpfun(fb1) fb2.c <- cmpfun(fb2) fb3.c <- cmpfun(fb3) fbcpp.c <- cmpfun(fbcpp)
  • 48. int n = Rcpp::as<int>(ns); Rcpp::CharacterVector fb(n); for (int index = 0; index < n; index++) { int i = index + 1; if (i % 3 == 0 && i % 5 == 0) fb[index] = "FizzBuzz"; else if (i % 3 == 0) fb[index] = "Fizz"; else if (i % 5 == 0) fb[index] = "Buzz"; else { char s[11]; // Integer is 10 or less digit sprintf(s, "%d", i); fb[index] = s; } } return(fb);