狠狠撸

狠狠撸Share a Scribd company logo
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
> 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"
R で解く FizzBuzz 問題
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
}
R で解く FizzBuzz 問題
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
   })
}
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
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="")
}
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
> 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
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
R で解く FizzBuzz 問題
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 問題

More Related Content

What's hot (6)

x86
x86x86
x86
Wei-Bo Chen
?
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
Deep Learning JP
?
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
Taku Yoshida
?
颁谤蹿と素性テンプレート
颁谤蹿と素性テンプレート颁谤蹿と素性テンプレート
颁谤蹿と素性テンプレート
Kei Uchiumi
?
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
Yosuke Shinya
?
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
Deep Learning JP
?
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
[DL輪読会]EfficientDet: Scalable and Efficient Object Detection
Deep Learning JP
?
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
StudyCo_DocumentAI による OCR と LLM て?紙文書をテ?ータ化する(試み)
Taku Yoshida
?
颁谤蹿と素性テンプレート
颁谤蹿と素性テンプレート颁谤蹿と素性テンプレート
颁谤蹿と素性テンプレート
Kei Uchiumi
?
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
【宝くじ仮説】The Lottery Ticket Hypothesis: Finding Small, Trainable Neural Networks
Yosuke Shinya
?
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
【DL輪読会】Unbiased Gradient Estimation for Marginal Log-likelihood
Deep Learning JP
?

Similar to R で解く FizzBuzz 問題 (10)

ATS language overview
ATS language overviewATS language overview
ATS language overview
Kiwamu Okabe
?
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programming
Chiwon Song
?
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM Languages
Corneil 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.pptx
SUJALORAON
?
Basics
BasicsBasics
Basics
Logan Campbell
?
Get Kata
Get KataGet Kata
Get Kata
Kevlin Henney
?
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?
Tobias Pfeiffer
?
FUNctional programming in Python
FUNctional programming in PythonFUNctional programming in Python
FUNctional programming in Python
knifeofdreams
?
ATS language overview
ATS language overviewATS language overview
ATS language overview
Kiwamu Okabe
?
20180310 functional programming
20180310 functional programming20180310 functional programming
20180310 functional programming
Chiwon Song
?
Performance Comparison JVM Languages
Performance Comparison JVM LanguagesPerformance Comparison JVM Languages
Performance Comparison JVM Languages
Corneil 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.pptx
SUJALORAON
?
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 Python
knifeofdreams
?

R で解く FizzBuzz 問題

  • 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"
  • 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 }
  • 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 }) }
  • 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="") }
  • 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
  • 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);