SlideShare a Scribd company logo
> 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 問題

More Related Content

What's hot

Feature Erasing and Diffusion Network for Occluded Person Re-Identification
Feature Erasing and Diffusion Network for Occluded Person Re-IdentificationFeature Erasing and Diffusion Network for Occluded Person Re-Identification
Feature Erasing and Diffusion Network for Occluded Person Re-Identification
harmonylab
 
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいことMLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
Rakuten Group, Inc.
 
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
Fixstars Corporation
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
Roberto Agostino Vitillo
 
Édipo Rei
Édipo ReiÉdipo Rei
Édipo Rei
iedalf
 
AWSでGPUも安く大量に使い倒せ
AWSでGPUも安く大量に使い倒せ AWSでGPUも安く大量に使い倒せ
AWSでGPUも安く大量に使い倒せ
Yasuhiro Matsuo
 
RPAってなあに?
RPAってなあに?RPAってなあに?
RPAってなあに?
Serverworks Co.,Ltd.
 
動画配信プラットフォーム on AWS
動画配信プラットフォーム on AWS動画配信プラットフォーム on AWS
動画配信プラットフォーム on AWS
Amazon Web Services Japan
 
Git LFSを触ってみた
Git LFSを触ってみたGit LFSを触ってみた
Git LFSを触ってみた
Yuto Suzuki
 
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
Tatsuya (達也) Katsuhara (勝原)
 
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
西岡 賢一郎
 
人と人の相性を考慮したシフトスケジューラ
人と人の相性を考慮したシフトスケジューラ人と人の相性を考慮したシフトスケジューラ
人と人の相性を考慮したシフトスケジューラ
鈴木 庸氏
 
2020年分子生物学会_留学のすゝめ_イギリス大学院
2020年分子生物学会_留学のすゝめ_イギリス大学院2020年分子生物学会_留学のすゝめ_イギリス大学院
2020年分子生物学会_留学のすゝめ_イギリス大学院
KonanIshida
 
MLOpsはバズワード
MLOpsはバズワードMLOpsはバズワード
MLOpsはバズワード
Tetsutaro Watanabe
 
バス業界のデジタル化の現在地
バス業界のデジタル化の現在地バス業界のデジタル化の現在地
バス業界のデジタル化の現在地
Yohei Mizuno
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話します
Moriyoshi Koizumi
 
CloudFormation/SAMのススメ
CloudFormation/SAMのススメCloudFormation/SAMのススメ
CloudFormation/SAMのススメ
Eiji KOMINAMI
 
$30で始めるFPGA
$30で始めるFPGA$30で始めるFPGA
$30で始めるFPGA
Yukitaka Takemura
 
Amazon SageMaker ML Governance 3つの機能紹介
Amazon SageMaker ML Governance 3つの機能紹介Amazon SageMaker ML Governance 3つの機能紹介
Amazon SageMaker ML Governance 3つの機能紹介
西岡 賢一郎
 
Amazon SageMaker で始める機械学習
Amazon SageMaker で始める機械学習Amazon SageMaker で始める機械学習
Amazon SageMaker で始める機械学習
Amazon Web Services Japan
 

What's hot (20)

Feature Erasing and Diffusion Network for Occluded Person Re-Identification
Feature Erasing and Diffusion Network for Occluded Person Re-IdentificationFeature Erasing and Diffusion Network for Occluded Person Re-Identification
Feature Erasing and Diffusion Network for Occluded Person Re-Identification
 
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいことMLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
MLOps Yearning ~ 実運用システムを構築する前にデータサイエンティストが考えておきたいこと
 
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
ソフト高速化の専門家が教える!AI・IoTエッジデバイスの選び方
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
Édipo Rei
Édipo ReiÉdipo Rei
Édipo Rei
 
AWSでGPUも安く大量に使い倒せ
AWSでGPUも安く大量に使い倒せ AWSでGPUも安く大量に使い倒せ
AWSでGPUも安く大量に使い倒せ
 
RPAってなあに?
RPAってなあに?RPAってなあに?
RPAってなあに?
 
動画配信プラットフォーム on AWS
動画配信プラットフォーム on AWS動画配信プラットフォーム on AWS
動画配信プラットフォーム on AWS
 
Git LFSを触ってみた
Git LFSを触ってみたGit LFSを触ってみた
Git LFSを触ってみた
 
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
20180914 security iotlt#1_ほんとうにあった怖い話_aws_iot編
 
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
Amazon SageMakerでscikit-learnで作ったモデルのEndpoint作成
 
人と人の相性を考慮したシフトスケジューラ
人と人の相性を考慮したシフトスケジューラ人と人の相性を考慮したシフトスケジューラ
人と人の相性を考慮したシフトスケジューラ
 
2020年分子生物学会_留学のすゝめ_イギリス大学院
2020年分子生物学会_留学のすゝめ_イギリス大学院2020年分子生物学会_留学のすゝめ_イギリス大学院
2020年分子生物学会_留学のすゝめ_イギリス大学院
 
MLOpsはバズワード
MLOpsはバズワードMLOpsはバズワード
MLOpsはバズワード
 
バス業界のデジタル化の現在地
バス業界のデジタル化の現在地バス業界のデジタル化の現在地
バス業界のデジタル化の現在地
 
HLSについて知っていることを話します
HLSについて知っていることを話しますHLSについて知っていることを話します
HLSについて知っていることを話します
 
CloudFormation/SAMのススメ
CloudFormation/SAMのススメCloudFormation/SAMのススメ
CloudFormation/SAMのススメ
 
$30で始めるFPGA
$30で始めるFPGA$30で始めるFPGA
$30で始めるFPGA
 
Amazon SageMaker ML Governance 3つの機能紹介
Amazon SageMaker ML Governance 3つの機能紹介Amazon SageMaker ML Governance 3つの機能紹介
Amazon SageMaker ML Governance 3つの機能紹介
 
Amazon SageMaker で始める機械学習
Amazon SageMaker で始める機械学習Amazon SageMaker で始める機械学習
Amazon SageMaker で始める機械学習
 

Similar to R で解く FizzBuzz 問題

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
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
 

Similar to 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);