> fizzbuzz <- function(n) {     # Write your code here  }> fizzbuzz(20) [1] "1"         "2"          "Fizz" [4] "4"       ...
fb1 <- function(n) {   fb <- character(n)   for (i in 1:n) {      if (i %% 3 == 0 && i %% 5 == 0)         fb[i] <- "FizzBu...
fb2 <- function(n) {   sapply(1:n, function(i) {      if (i %% 3 == 0 && i %% 5 == 0)         "FizzBuzz"      else if (i %...
fb3 <- function(n) {   isFizz <- rep(c(F,F,T), length=n)   isBuzz <- rep(c(F,F,F,F,T), length=n)   isNumber <- !isFizz & !...
> N <- 100> benchmark(fb1(N), fb2(N), fb3(N),            order="relative",            replications=10000)    test elapsed ...
> N <- 10000> benchmark(fb1(N), fb2(N), fb3(N),             order="relative",             replications=100)    test elapse...
> N <- 1000000> benchmark(fb1(N), fb2(N), fb3(N),            order="relative",            replications=5)    test elapsed ...
library(compiler)      # Installed by defaultlibrary(inline)        # Needs manual installationfbcpp <- cxxfunction(   sig...
int n = Rcpp::as<int>(ns);Rcpp::CharacterVector fb(n);for (int index = 0; index < n; index++) {   int i = index + 1;   if ...
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 問題
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 問題
Upcoming SlideShare
Loading in …5
×

R で解く FizzBuzz 問題

2,320 views

Published on

FizzBuzz question in R language.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,320
On SlideShare
0
From Embeds
0
Number of Embeds
169
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

R で解く FizzBuzz 問題

  1. 1. > 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"
  2. 2. 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}
  3. 3. 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 })}
  4. 4. 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="")}
  5. 5. > N <- 100> benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=10000) test elapsed relative3 fb3(N) 4.172 1.0000002 fb2(N) 6.669 1.5985141 fb1(N) 7.779 1.864573
  6. 6. > N <- 10000> benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=100) test elapsed relative3 fb3(N) 2.907 1.0000002 fb2(N) 7.823 2.6910901 fb1(N) 8.200 2.820777
  7. 7. > N <- 1000000> benchmark(fb1(N), fb2(N), fb3(N), order="relative", replications=5) test elapsed relative3 fb3(N) 28.287 1.0000001 fb1(N) 46.198 1.6331882 fb2(N) 52.223 1.846184
  8. 8. library(compiler) # Installed by defaultlibrary(inline) # Needs manual installationfbcpp <- 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)
  9. 9. 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);

×